DS Module 3 GRAPH
DS Module 3 GRAPH
DS Module 3 GRAPH
MODULE 4
GRAPH
A graph data structure is a collection of nodes that have data and are
connected to other nodes.
A graph is a pictorial representation of a set of objects where some pairs
of objects are connected by links. The interconnected objects are represented by
points termed as vertices, and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices
and E is the set of edges, connecting the pairs of vertices. Take a look at the
following graph −
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
Before we proceed further, let's familiarize ourselves with some important terms
−
• Vertex − Each node of the graph is represented as a vertex. Generally, a
labelled circle represents vertices.
• Edge − Edge represents a path between two vertices or a line between two
vertices.
• Path - A path can be defined as the sequence of vertices that are followed
in order to reach some terminal node V from the initial node U.
• Closed Path - A path will be called as closed path if the initial node is
same as terminal node. A path will be closed path if V0=VN.
• Simple Path - If all the nodes of the graph are distinct with an exception
V0=VN, then such path P is called as closed simple path.
• Cycle - A cycle can be defined as the path which has no repeated edges or
vertices except the first and last vertices.
• Loop - An edge that is associated with the similar end points can be called
as Loop.
• Degree of the Node - A degree of a node is the number of edges that are
connected with that node. A node with degree 0 is called as isolated node.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
Graph Representations
In graph theory, a graph representation is a technique to store graph into
the memory of computer.
To represent a graph, we just need the set of vertices, and for each vertex
the neighbors of the vertex (vertices which is directly connected to it by an edge).
If it is a weighted graph, then the weight will be associated with each edge.
There are different ways to optimally represent a graph, depending on the
density of its edges, type of operations to be performed and ease of use.
1. Adjacency Matrix
• Adjacency matrix is a sequential representation.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
• It is used to represent which nodes are adjacent to each other. i.e. is there
any edge connecting nodes to a graph.
• In this representation, we have to construct a nXn matrix A. If there is any
edge from a vertex i to vertex j, then the corresponding element of A, ai,j =
1, otherwise ai,j= 0.
• If there is any weighted graph then instead of 1s and 0s, we can store the
weight of the edge.
Note, even if the graph on 100 vertices contains only 1 edge, we still have to have
a 100x100 matrix with lots of zeroes.
Example
2. Incidence Matrix
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
Example
3. Adjacency List
• Adjacency list is a linked representation.
• In this representation, for each vertex in the graph, we maintain the list of
its neighbors. It means, every vertex of the graph contains list of its
adjacent vertices.
• We have an array of vertices which is indexed by the vertex number and
for each vertex v, the corresponding array element points to a singly linked
list of neighbors of v.
Example
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
SHORT QUESTIONS
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
They can be used to model many types of relations and process dynamics
in computer science, physical, biological and social systems. They can be
used to model pair wise relations between objects from a certain collection.
Some of its applications are
• Transportation networks: Highway network, Flight network.
• Computer networks: Local area network, Internet, Web.
• Representing relationships between components in electronic circuits.
• Databases: For representing ER(Entity Relationship) diagrams in databases,
for representing dependency of tables in databases.
• Connecting with friends on social media, where each user is a vertex, and
when users connect they create an edge.
• Using GPS/Google Maps/Yahoo Maps, to find a route based on shortest
route.
• Google, to search for webpages, where pages on the internet are linked to
each other by hyperlinks; each page is a vertex and the link between two
pages is an edge.
• On eCommerce websites relationship graphs are used to show
recommendations.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
LONG QUESTIONS
1. what is a graph?
A graph is a pictorial representation of a set of objects where some pairs of
objects are connected by links. The interconnected objects are represented by
points termed as vertices, and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices
and E is the set of edges, connecting the pairs of vertices. Take a look at the
following graph −
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
Basic Operations
Following are basic primary operations of a Graph −
• Add Vertex − Adds a vertex to the graph.
• Add Edge − Adds an edge between the two vertices of the graph.
• Display Vertex − Displays a vertex of the graph.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
• Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It
will pop up all the vertices from the stack, which do not have adjacent
vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
Back tracking is coming back to the vertex from which we came to current
vertex.
1.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
As C does not have any unvisited adjacent node so we keep popping the
stack until we find a node that has an unvisited adjacent node. In this case, there's
none and we keep popping until the stack is empty.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it.
Insert it in a queue.
• Rule 2 − If no adjacent vertex is found, remove the first vertex from the
queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
Step 2: Select any vertex as starting point for traversal. Visit that vertex and
insert it into the Queue.
Step 3: Visit all the adjacent vertices of the vertex which is at front of the
Queue which is not visited and insert them into the Queue.
Step 4: When there is no new vertex to be visit from the vertex at front of the
Queue then delete that vertex from the Queue.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
Step 6: When queue becomes Empty, then produce final spanning tree by
removing unused edges from the graph
At this stage, we are left with no unmarked (unvisited) nodes. But as per
the algorithm we keep on dequeuing in order to get all unvisited nodes. When the
queue gets emptied, the program is over.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
1. Social network graphs: to tweet or not to tweet. Graphs that represent who
knows whom, who communicates with whom, who influences whom or other
relationships in social structures. An example is the twitter graph of who follows
whom. These can be used to determine how information flows, how topics
become hot, how communities develop, or even who might be a good match for
who, or is that whom.
3. Utility graphs. The power grid, the Internet, and the water network are all
examples of graphs where vertices represent connection points, and edges the
wires or pipes between them. Analyzing properties of these graphs is very
important in understanding the reliability of such utilities under failure or attack,
or in minimizing the costs to build infrastructure that matches required demands.
4. Document link graphs. The best known example is the link graph of the web,
where each web page is a vertex, and each hyperlink a directed edge. Link graphs
are used, for example, to analyze relevance of web pages, the best sources of
information, and good link sites.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
7. Scene graphs. In graphics and computer games scene graphs represent the
logical or spacial relationships between objects in a scene. Such graphs are very
important in the computer games industry.
9. Robot planning. Vertices represent states the robot can be in and the edges
the possible transitions between the states. This requires approximating
continuous motion as a sequence of discrete steps. Such graph plans are used, for
example, in planning paths for autonomous vehicles.
10. Neural networks. Vertices represent neurons and edges the synapses
between them. Neural networks are used to understand how our brain works and
how connections change when we learn. The human brain has about 1011 neurons
and close to 1015 synapses.
[email protected]
Asst. Prof. Praveen Sebastian Paul, Don Bosco College, Kottiyam
14. Graphs in compilers. Graphs are used extensively in compilers. They can
be used for type inference, for so called data flow analysis, register allocation
and many other purposes. They are also used in specialized compilers, such as
query optimization in database languages.
15. Constraint graphs. Graphs are often used to represent constraints among
items. For example the GSM network for cell phones consists of a collection of
overlapping cells. Any pair of cells that overlap must operate at different
frequencies. These constraints can be modeled as a graph where the cells are
vertices and edges are placed between cells that overlap.