Unit IV
Unit IV
Graph: - A graph is a collection of two sets V and E. Where V is a finite non empty set of vertices
and E is a finite non-empty set of edges.
A
• Vertices are the nodes in the graph.
• Two adjacent vertices are joined by edges B C D
• Any graph is denoted as G = {V, E}
For Example: E F
Graph Tree
Non-linear data structure. Non-linear data structure.
Collection of vertices and edges. Collection of vertices and edges.
Each node can have any number of edges. In binary trees every node can have at the most
2 child nodes.
There is no unique node like trees. There is a unique node called root node.
A cycle can be formed. There will not be any cycle.
Applications: used for finding shortest path in Applications: used for expression trees, and
networking graph. decision tree.
• Directed graph: - In the directed graph the directions are shown on the edges. As shown in
figure, the edges between the vertices are ordered. In this type of the graph, the edge between
the vertices A and B is set of (A, B). The vertex A is called head and the vertex B is called tail.
We can say edge is set of (A, B) and not of (B, A).
DATA STRUCTURES
UNIT IV GRAPHS
• Undirected graph: - In an undirected graph, the edges are not ordered. In the fig, the edge
between A and B is set of (A, B) and (B, A).
A A
B C D B C D
E F E F
(a) Directed Graph (b) Undirected Graph
Complete Graph: - If an undirected graph of n vertices consists of n (n-1)/2 number of edges then
it is called as complete graph.
A path is a sequence of vertices in which each vertex is adjacent to the next one. In directed graph
{A, B, C, F} is one path and {A, B, E, F} is another.
Two vertices in a graph are said to be adjacent vertices (or) neighbors if there is a path of length
1 connecting them. In directed graph: B is adjacent to A, where as E is not adjacent to D. In
undirected graph: E and F are adjacent, but D and F are not.
A cycle is a path consisting of at least three vertices that starts and ends with the same vertex.
A loop is a special case of a cycle in which a single arc begins and ends with the same vertex.
A B
A directed graph is strongly connected if there is a path from each vertex to every other vertex in
the digraph.
A directed graph is weakly connected if at least two vertices are not connected.
DATA STRUCTURES
UNIT IV GRAPHS
B E F G
C D H I
B E F G
C D H I
B E F G
In undirected graph, the degree of a vertex is the number of lines incident to it. In fig (b), the degree
of vertex B is 3 and the degree of vertex E is 2.
The out degree of a vertex in a digraph is the number of arcs leaving the vertex. The in degree of
a vertex is the number of arcs entering the vertex. In fig (d) for the vertex B the in degree is 1 and
the out degree is 2.
DATA STRUCTURES
UNIT IV GRAPHS
→ Adjacency Matrix Representation: Matrix representation is the most commonly useful way
of representing any graph. This representation uses a square matrix of order NXN, where N is the
number of vertices in the graph.
The matrix is also known as adjacency matrix, because an entry shows the information whether
the two vertices are adjacent (or) not. It is also termed as bit matrix (or) boolean matrix as its
entries are either 1 (or) 0.
V1
V V B E
V V V V C D
1234567 ABCD
1 0110000 A0 0 3 0
2 1001100 B5 0 1 7
3 1000011 C2 0 0 4
4 0100000 D0 6 8 0
6 0010000
DATA STRUCTURES
UNIT IV GRAPHS
The disadvantage of adjacency matrix representation is that it always requires a nXn matrix with
n vertices, regardless of the number of edges. The adjacency matrix for an undirected graph is
symmetric. The adjacency matrix for a directed graph need not be symmetric.
V1 V2 V3 \0
V2 V1 V4 V5 \0
V3 V1 V6 V7 \0
V4 V2 \0
V5 V2 \0
V6 V3 \0
V7 \0 V3 \0
Fig (i): Representation of Graph G1.
This is purely the adjacency list graph. The down pointer helps us to go to each next node in the
graph whereas the next node is for going to adjacent node of each of the head node. All the
advantages of linked list can be obtained in this type of graph.
DATA STRUCTURES
UNIT IV GRAPHS
1. Both of these algorithms work on directed or undirected graphs to determine the whether the
graph is connected or not.
2. Many advanced graph algorithms are based on the ideas of BFS or DFS.
3. Each of these algorithms traverses edges in the graph, discovering new vertices as it proceeds.
4. The difference is in the order in which each algorithm discovers the edges.
V1
Consider an example in Graph G which is represented by its adjacency lists.
V2 V3
V1 V2 V3 \0 V4 V5 V6 V7
V2 V1 V4 V5 \0
V8
(a) Graph G
V3 V1 V6 V7 \0
V4 V2 V8 \0
V5 V2 V8 \0
V6 V3 V8 \0
V7 V3 V8 \0
V8 \0 V4 V5 V6 V7 \0
→ BFS:
DATA STRUCTURES
UNIT IV GRAPHS
BFS starts at vertex V and mark it as visited. It then visits each of the vertices on V’s adjacency
list. When we have visited all the vertices on V’s adjacency list, we visit all the unvisited vertices
that are adjacent to the first vertex on V’s adjacency list. To implement this scheme, as we visit
each vertex we place the vertex in a queue. When we have exhausted an adjacency list, we remove
a vertex from the queue and proceed by examining each of the vertices on its adjacency list.
Unvisited vertices are visited and then placed on the queue; visited vertices are ignored. The search
is finished when the queue is empty.
➔ V1
Example: Graph G. Source vertex: V1
• Visit V1, mark it as visited and Enqueue to Queue. Queue after Enqueue
➢ Dequeue V1 and call BFS on V1. ➔
• Find V1 adjacent unvisited node i.e. V2 Queue after Dequeue
• Visit V2, mark it as visited and Enqueue to Queue. ➔ V2
• Since all adjacent nodes of V2 are visited Dequeue from Queue. Queue after Enqueue
➢ Dequeue V3 and call BFS on V3. ➔ V4 V5
• Since all adjacent nodes of V3 are visited Dequeue from Queue. Queue after Enqueue
V5 V6 V7
➢ Dequeue V4 and call BFS on V4. ➔
• Find V4 adjacent unvisited node i.e. V8. Queue after Dequeue
V5 V6 V7 V8
DATA STRUCTURES
UNIT IV GRAPHS
• Since all adjacent nodes of V5 are visited Dequeue from Queue. Queue after Dequeue
➢ Dequeue V6 and call BFS on V6. ➔ V7 V8
• Since all adjacent nodes of V6 are visited Dequeue from Queue. Queue after Dequeue
➢ Dequeue V7 and call BFS on V7. ➔ V8
• Since all adjacent nodes of V7 are visited Dequeue from Queue. Queue after Dequeue
➢ Dequeue V8 and call BFS on V8. ➔
• Since all adjacent nodes of V8 are visited Dequeue from Queue. Queue after Dequeue
➢ Since Queue is Empty end of BFS on Graph G. ➔ Queue is Empty.
If a BFS is initiated from vertex V1, then the vertices of G are visited in the order V1, V2, V3, V4,
V5, V6, V7, and V8.
→ DFS:
DFS is similar to a preorder traversal in trees. The DFS begins by visiting the start vertex V. Next
we select an unvisited vertex, W, from V’s adjacency list and carry out a DFS on W. We preserve
our current position in V’s adjacency list by placing it on a stack. Eventually our search reaches a
vertex, U, which has no unvisited vertices on its adjacency list. At this point, we remove a vertex
from the stack and continue processing its adjacency list. Previously visited vertices are discarded;
unvisited vertices are placed on the stack. The traversal terminates when the stack is empty.
DATA STRUCTURES
UNIT IV GRAPHS
• Find Adjacent unvisited node of stack top (V4) i.e. V8. stack
• Find Adjacent unvisited node of stack top (V3) i.e. V7. stack
DATA STRUCTURES
UNIT IV GRAPHS
If a DFS is initiated from vertex V1, then the vertices of G are visited in the order V1, V2, V4, V8,
V5, V6, V3 and V7.
DATA STRUCTURES
UNIT IV GRAPHS
A connected graph has only one connected component, which is the graph itself, while
unconnected graphs have more than one component. For example, the graph shown below has
three components, (0, 1, 2, 3), (4, 5, 6), and (7, 8).
Figure: - Graph
The connected components of a graph can be found using a depth-first search (DFS). We start at
an arbitrary vertex, and visit every vertex adjacent to it recursively, adding them to the first
component. Once this search has finished, we have visited all of the vertices in the first connected
component, so we choose another unvisited vertex (if any) and perform the same search starting
from it, adding the vertices we find to the second component. This process continues until all
vertices have been visited, at which point we know the number of connected components in the
graph, and which vertices they contain. The following are the steps based on DFS
DFSUtil(v)
1) Mark 'v' as visited.
2) Print 'v'
3) Do following for every adjacent 'u' of 'v'.
If 'u' is not visited, then recursively call DFSUtil(u)
→ Spanning Tree: A spanning tree has a property that for any pair of vertices there exists only
one path between them, and the insertion of any edge to a spanning tree does not form a cycle. The
spanning tree resulting from a call to depth first tree is known as depth first spanning tree. The
spanning tree resulting from a call to breadth first tree is known as a breadth first spanning tree.
DATA STRUCTURES
UNIT IV GRAPHS
The minimum spanning tree for a graph is the spanning tree of minimum cost for that graph.
Spanning tree has n-1 edges, where n is the number of nodes (vertices).
Algorithm:
1. Insert the first vertex into the tree.
2. From every vertex already in the tree, examine the total path length to all adjacent vertices not
in the tree. Select the edge with the minimum total path weight and insert it into the tree.
3. Repeat step 2 until all vertices are in the tree.
By this definition, we can draw a conclusion that every connected and undirected Graph G has at
least one spanning tree. A disconnected graph does not have any spanning tree, as it cannot be
spanned to all its vertices.
→→ Biconnected Components: -
A graph is said to be Biconnected graph if:
1. It is connected, i.e. it is possible to reach every vertex from every other vertex, by a simple
path.
2. Even after removing any vertex the graph remains connected.
For example, consider the graph in the following figure
DATA STRUCTURES
UNIT IV GRAPHS
The given graph is clearly connected. Now try removing the vertices one by one and observe.
Removing any of the vertices should not increase the number of connected components. So the
given graph is Biconnected Graph.
Now consider the following graph which is a slight modification in the previous graph.
In the above graph if the vertex 2 is removed, then here's how it will look:
DATA STRUCTURES
UNIT IV GRAPHS
Clearly the number of connected components has increased. Similarly, if vertex 3 is removed there
will be no path left to reach vertex 0 from any of the vertices 1, 2, 4 or 5. And same goes for vertex
4 and 1. Removing vertex 4 will disconnect 1 from all other vertices 0, 2, 3 and 4. So the graph is
not Biconnected.
It is said that a graph is Biconnected graph if it has no vertex such that its removal increases the
number of connected components in the graph. And if there exists such a vertex then it is not
Biconnected graph. A vertex whose removal increases the number of connected components is
called an Articulation Point.
If there is no Articulation Point in graph, then graph is biconnected graph and so there will be one
biconnected component which is the graph itself.
→→ Minimum Spanning Tree: - The cost of the spanning tree is the sum of the weights of all
the edges in the tree. There can be many spanning trees. Minimum spanning tree is the spanning
tree where the cost is minimum among all the spanning trees. There also can be many minimum
spanning trees.
→Kruskal’s Algorithm: - Kruskal’s Algorithm builds the spanning tree by adding edges one by
one into a growing spanning tree. Kruskal's algorithm follows greedy approach as in each iteration
it finds edges which has least weight and adds it to the growing spanning tree.
Algorithm Steps:
• Sort the graph edges with respect to their weights.
• Start adding edges to the MST from the edge with the smallest weight until the edge of the
largest weight.
• Only add edges which doesn't form a cycle, edges which connect only disconnected
components.
Consider following example: In Kruskal’s algorithm, at each iteration we will select the edge with
the lowest weight. So, we will start with the lowest weighted edge first i.e., the edges with weight
1. After that we will select the second lowest weighted edge i.e., edge with weight 2. Notice these
two edges are totally disjoint. Now, the next edge will be the third lowest weighted edge i.e., edge
with weight 3, which connects the two disjoint pieces of the graph. Now, we are not allowed to
pick the edge with weight 4, that will create a cycle and we can’t have any cycles. So we will select
the fifth lowest weighted edge i.e., edge with weight 5. Now the other two edges will create cycles
DATA STRUCTURES
UNIT IV GRAPHS
so we will ignore them. In the end, we end up with a minimum spanning tree with total cost 11 (
= 1 + 2 + 3 + 5).
DATA STRUCTURES
UNIT IV GRAPHS
→ Prims algorithm: In the prims algorithm, minimum spanning tree grows in successive stages.
At each stage in the algorithm, we see that whether any of vertices have already been included in
the tree, prims algorithm then finds a new vertex to add it to the tree by choosing the edge <V i,
Vj>, the smallest among all edges, where Vi is the tree and Vj is yet to be included in the tree. The
algorithm starts by selecting a vertex arbitrarily, and then at each stage, we add an edge to the tree.
This algorithm can easily be implemented using adjacency matrix representation of a graph.
Suppose there is a nXn adjacency matrix for a given undirected weighted graph and the vertices
are labeled as V1, V2, V3, ….., Vi, Vj, and Vn. Start from Vertex V1. Get its nearest neighbor, which
is the vertex that has the smallest entry in the row of V1, Let it be Vj. Now consider Vj and V1 as
one sub-graph, i.e., a vertex other than V1 and Vj that has the smallest entry among all the entries
in the rows of V1 and Vj, let his new vertex be V. Therefore, the tree now grows with vertex V1,
Vj, and V as one sub-graph. This process is repeated until all N vertices have been connected by
n-1 edges.
Consider the following example to find minimum spanning tree: An undirected weighted graph
and its weighted adjacency matrix is shown in following figure:
V1 1 2 3 4 5 6 7
1 - 4 5 2 - - -
V2 V3
2 4 - - 1 2 - -
3 5 - - 8 - 1 -
V4 4 2 1 8 - 3 4 7
5 - 2 - 3 - - 9
V5 V6
6 - - 1 4 - - 6
V7 7 - - - 7 9 6 -
Consider vertex V1 and pick the smallest entry in row 1 which is 2 in column 4; this V4 is the
nearest neighbor to V1, the closest neighbor of sub-graph V1 and V4 is V2 that can be seen by
examining all entries in rows 1 and 4. The remaining four edges relate to the above procedure.
DATA STRUCTURES
UNIT IV GRAPHS
V1 V1 V1 V1
Initial Tree V2 V2
V4 V4 V4
stage 1 stage 2 stage 3
V5
V1 V1 V1
V2 V2 V3 V2 V3
V4 V4 V4
V5 V6 V5 V6 V5 V6
V7
Stage 4 stage 5 Final stage
DATA STRUCTURES
UNIT IV GRAPHS
Initially MST is empty. Every vertex is singe component as highlighted in blue color in below
diagram.
For every component, find the cheapest edge that connects it to some other component.
Component Cheapest Edge that connects it to some other component
{0} 0-1
{1} 0-1
{2} 2-8
{3} 2-3
{4} 3-4
{5} 5-6
{6} 6-7
{7} 6-7
{8} 2-8
The cheapest edges are highlighted with green color. Now MST becomes {0-1, 2-8, 2-3, 3-4, 5-6,
6-7}.
After above step, components are {{0,1}, {2,3,4,8}, {5,6,7}}. The components are encircled with
blue color.
DATA STRUCTURES
UNIT IV GRAPHS
We again repeat the step, i.e., for every component, find the cheapest edge that connects it to
some other component.
Component Cheapest Edge that connects it to some other component
{0,1} 1-2 (or 0-7)
{2,3,4,8} 2-5
{5,6,7} 2-5
The cheapest edges are highlighted with green color. Now MST becomes {0-1, 2-8, 2-3, 3-4, 5-6,
6-7, 1-2, 2-5}
At this stage, there is only one component {0, 1, 2, 3, 4, 5, 6, 7, 8} which has all edges. Since there
is only one component left, we stop and return MST.
→→ Shortest Path and Transitive Closure: -
→ Single Source/All Destination: Nonnegative Edge Cost: - Dijkstra's developed an algorithm,
published in 1959, which grows paths from a given source vertex using a "greedy approach" and
finds all shortest paths to this source vertex.
Dijkstra’s Algorithm is an algorithm for finding the shortest paths between nodes in a graph. For
a given source node in the graph, the algorithm finds the shortest path between that node and every
other node. It can also be used for finding the shortest paths from a single node to a single
DATA STRUCTURES
UNIT IV GRAPHS
destination node by stopping the algorithm once the shortest path to the destination node has been
determined.
Dijkstra’s Algorithm is based on the principle of relaxation, in which an approximation to the
correct distance is gradually replaced by more accurate values until shortest distance is reached.
The approximate distance to each vertex is always an overestimate of the true distance, and is
replaced by the minimum of its old value with the length of a newly found path.
For instance, consider below graph. We will start from vertex A. So vertex A has distance 0 and
remaining vertices have undefined (infinite) distance from source. Let S be the set of vertices
whose shortest path distances from source are already calculated.
∞ ∞
B C
10 2
4 1 8 7
0 A
3 9
2
E D
∞ ∞
Initially S = {} and we push source vertex to it. S = {A}. We start from source vertex A and start
relaxing A’s neighbors. Since vertex B can be reached from a direct edge from vertex A, we update
its distance to 10 (weight of edge A-B). Similarly vertex E can be reached through a direct edge
from A, so we update its distance from infinity to 3.
=min(INF,10)=10 ∞
B C
10 2
4 1 8 7
0 A
3 9
2
E D
=min(INF,3)=3 ∞ S={A}
After processing all outgoing edges of A, we next consider vertex having minimum distance. B
has distance of 10, E has distance 3 and all remaining vertices have distance infinity. So we choose
E and push it into the Set S. Now our set becomes S = {A, E}. Next we relax E’s neighbors. E has
DATA STRUCTURES
UNIT IV GRAPHS
2 neighbors B and C. We have already found one route to vertex B through vertex A having cost
10. But if we visit vertex B through vertex E, we are getting even a cheaper route. i.e. (cost of edge
A-E + cost of edge E-B) = 3 + 1 = 4 < 10 (cost of edge A-B).
=min(10,3+1)=4 B 2 C =min(INF,3+8)=11
10
1 4 8 7
A
0 9
3
2
E D
3 =min(INF,3+2)=5 S={A,E}
We repeat the process till we have processed all the vertices. i.e Set S become full.
4 2 =min(11,4+2)=6
B C
10
4 1 8 7
A
0 9
3
2
E D
3 5 S={A,E,B}
4 2 =min(6,5+7)=6
B C
10
4 1 8 7
A
0 9
3
2
E D
3 5 S={A,E,B,D}
DATA STRUCTURES
UNIT IV GRAPHS
4 B 2 C 6
10
A
4 1 8 7
0 9
3
2 D
E
3 5 S={A,E,B,D,C}
4 B 2 C 6
10
A
4 1 8 7
0 9
3
2 D
E
3 5
Dijkstra's Shortest Path Algorithm
1) Assign to every vertex a value which will store an associated distance. For the source vertex,
make this distance zero. For every other vertex, initially make this distance infinite.
2) Mark all nodes as unvisited.
3) Set the "current vertex" initially to be the source vertex.
4) Repeat the following until all vertices have been visited:
a. For the current vertex, consider all its unvisited neighbors and calculate their tentative
distance (i.e., the sum of the distance associated with the current vertex plus the distance
between the current vertex and the unvisited neighbor).
If this distance is less than the previously recorded distance for the current vertex, overwrite
the current vertex distance with this smaller distance (edge relaxation) and mark the current
vertex as visited.
b. Set the unvisited node with the smallest distance from the source node as the next "current
node"
DATA STRUCTURES