You are on page 1of 31

Department of CSE

Advanced Data Structures


Unit - 4
3
Unit - 4
SYLLABUS: Graph algorithms: Minimum-Cost Spanning Trees- Prim's
Algorithm, Kruskal's Algorithm Shortest Path Algorithms: Dijkstra's Algorithm,
All Pairs Shortest Paths Problem: Floyd's Algorithm, Warshall's Algorithm.
Day 33
Topics to be covered: Graphs Basic Concepts
Q1. Define Graph.
A graph is defined as a collection of set of vertices and a set of edges. A graph
G = (V, E) consists of a set of vertices, V, and a set of edges, E. Edges are
sometimes referred to as arcs.
Q2. What is a directed graph and undirected graph?
A graph that is having directions to the edges in a graph is called as a directed
graph. Directed graphs are sometimes referred to as digraphs.

Q3. What is an undirected graph?


A graph that doesn’t contain any directions to the edges is
called as an undirected graph. Each edge is an unordered
pair {u, v} of vertices,
Example: The following graph is an undirected graph.
Q4. Define weighted graph.
A Weighted graph is a graph in which all the edges in it are labelled with some
weights.

Q5. What is self-loop?


If there is an edge whose starting and ending vertices are same then the edge
is called a self-loop.
4
Example:

Q6. Define Path.


A path is a walk that leads or connects a finite sequence of edges from a given
set of vertices.
Q7. What is a Cycle in a graph?
A cycle is a closed path such that there exists an edge whose starting vertex
reaches the same ending vertex (i.e. both starting and ending vertices are
same, but in their path they can have some more vertices)
Example:

In the above graph the path V1-V2-V3-V1 is a cycle.


Q8. Define adjacent vertices.
Two vertices u and v are adjacent if they are connected by an edge, in other
words, (u, v) is an edge.
Example: a and b are adjacent vertices in the given graph.

Q9. What is an acyclic graph and cyclic graph?


If a graph does not contain any cycles, then the graph is called an acyclic
graph and if it contains cycle then the graph is cyclic.
5

Q10. Define degree of vertex of an undirected graph.


The number of edges connected with a vertex vi is called degree of vertex vi

Q11. What is in-degree of a vertex and out-degree of a vertex?


The in-degree of a vertex is the number of edges that are incident or coming
towards the vertex vi and the out-degree of a vertex is the total number of
edges emanating or going out from vertex vi.

Q12. Define isolated vertex of a graph.


A vertex vi is an isolated vertex if there is no edge connected from this vertex
to any other vertex (i.e. degree of vertex is 0)
Example: d is an isolated vertex in the given graph.
6

Q13. What is pendant vertex?


A vertex is vi is a pendant vertex if its in-degree is 1 and its out-degree is 0.

Class work:
1) What is degree of vertex of a graph?
Homework:
1) -------------- is a vertex having in-degree of 1 and out-degree of 0.
Previous JNTUK Questions:
1) What is path, cycle, loop and Adjacency vertex of a graph? (JNTUK-
Regular- April/May – 2016 -SET-2- [4M])
Day 34
Topics to be covered: Representation of Graphs
Q1. What are the various ways to represent the graphs? Explain the
adjacency matrix representation of graphs.
A graph can be represented most commonly by 2 ways
i) Adjacency matrix representation
ii) Adjacency list representation (linked representation)
Representation of graph using adjacency matrix: An adjacency matrix is a
matrix in which the entries or elements of the matrix are defined as follows
aij = 1 , if there is an edge from vi to vj. = 0, otherwise.
Adjacency matrix is also termed as Boolean or bit matrix (because it uses the
bits 0 or 1 in matrix)
Example: An adjacency matrix is used to represent the graph in the following
way for an undirected graph
7

In adjacency matrix representation of a graph, if there is an edge from vi to vj,


then aij = 1, otherwise aij = 0 for example in the above undirected graph there
is an edge from v1 to v2, therefore a12 =1 and also a21=1.

In adjacency matrix representation of a digraph, if there is an edge from vi to


vj, then aij = 1, but aji ǂ 1 for example in the above digraph there is an edge
from v1 to v5, therefore a15 =1 but a51 ǂ1 because there is no edge from v5 to v1.
Q2. Describe adjacency list representation of graphs.
An adjacency list is the representation of graph in which the graph is created
or represented with dynamic data structure linked list.
The node structure for the representation of graph is given as
Struct node
{
Char vertex;
Struct node * next;
};

Example: The adjacency list representation of a graph is given below

If the graph is a weighted graph, then the node structure of a graph is


8
Struct node
{
Int weight;
Char vertex;
Struct node * next;
};

For example consider the weighted graph given below, the adjacency list
representation is given as

Class work:
1) Write the node representation of a weighted graph.
Homework:
1) If there is an edge from vi to vj in the graph then the value that edge in
adjacency matrix is --------------
Previous JNTUK Questions:
1) How to represent a graph using adjacency matrix and adjacency
list? Explain. (JNTUK-Regular- December – 2013 – SET -4 [8M])
Day 35
Topics to be covered: Graph Traversal algorithms
Q1. What is a graph traversal? Name the graph traversal algorithms.
A graph traversal means visiting all the vertices in the graph exactly only once.
Graph traversal algorithms are used are
i) BFS (Breadth First Search) ii) DFS (Depth First Search)
Q2. Explain DFS algorithm with example.
9
DFS (Depth First Search) algorithm is a graph traversal algorithm where we
will start searching or finding the path from a starting or source vertex to the
ending or destination vertex deeply or vertically from top to bottom.
In DFS algorithm we will use stack data structure to search the path because
we search the path vertically from top to bottom.
Algorithm: Suppose a graph of vertices v1, v2, v3, v4........vn is given as below.

Step 1: Start the algorithm from vertex v1 and mark this vertex V1 as visited in
the array as ‘1’, if not visited mark as ‘0’ and push the vertex V1 into stack.

Step 2: Find adjacent vertices of V1are V2,V8,V3 .Now pop the vertex V1 from the
stack and place it in output and push any one of the adjacent vertex to V1 into
the stack. Repeat this procedure of steps for the remaining vertices.

Step 3: Find adjacent vertices of V2are V1, V4, V5 and V8 .Vertex V1 is already
visited so don’t consider the adjacent vertex V1 and take any adjacent vertex
for the next step and say V8.Now pop the vertex V2 from the stack and place it
in output and push adjacent vertex V8 into the stack and mark V8 as visited .
10

Step 4: Find adjacent vertices of V8 are V1,V2,V3 and V8 .Vertex V1 and V2 are
already visited so don’t consider them and take any adjacent vertex for the
next step and say V3.Now pop the vertex V8 from the stack and place it in
output and push adjacent vertex V3 into the stack and mark V3 as visited .

Step 5: Find adjacent vertices of V3 are V1,V4,V6 and V8 .Vertex V1 and V8 are
already visited so don’t consider them and take any adjacent vertex for the
next step and say V6.Now pop the vertex V3 from the stack and place it in
output and push adjacent vertex V6 into the stack and mark V3 as visited .

Step 6: Find adjacent vertices of V6 are V3 and V7 .Vertex V3 is already visited so


don’t consider it and take adjacent vertex V7 for the next step. Now pop the
vertex V6 from the stack and place it in output and push adjacent vertex V7
into the stack and mark V7 as visited.
11

Step 7: Find adjacent vertices of V7 are V5 and V6 .Vertex V6 is already visited so


don’t consider it and take adjacent vertex V5 for the next step. Now pop the
vertex V7 from the stack and place it in output and push adjacent vertex V5
into the stack and mark V5 as visited.

Step 8: Find adjacent vertices of V5 are V2,V4 and V7 .Vertex V2 and V7 are already
visited so don’t consider it and take adjacent vertex V4 for the next step. Now
pop the vertex V5 from the stack and place it in output and push adjacent
vertex V4 into the stack and mark V4 as visited.

Step 9: As all the vertices in the graph are visited, now pop the remaining
vertex V4 from the stack and place it in output
12
The traversal path for the given graph by using DFS is
V1 – V2 – V8 - V3 – V6 – V7 – V5 – V4
Q3. Describe BFS algorithm with example.
BFS (Breadth First Search)algorithm is a graph traversal algorithm where we
will start searching or finding the path from a starting or source vertex to the
ending or destination vertex breadth wise or horizontally from top to bottom.
In BFS algorithm we will use Queue data structure to search the path because
we search the path horizontally from top to bottom. We insert the elements
at rear of the queue and remove it from front of the queue.
The algorithm works by taking a source vertex and finding its adjacent
vertices, inserting them into the marking them as visited or known. This
procedure is repeated until all the vertices are visited in the graph.
Algorithm:
Suppose a graph of vertices v1, v2, v3, v4........vn is given as below.

Step 1: Start the algorithm from vertex v1 and mark this vertex V1 as visited in
the array as ‘1’, if not visited mark as ‘0’ and push the vertex V1 into rear of
the queue.

Step 2: Find adjacent vertices of V1 are V2,V3,V4 and V8 . and mark them as
visited in the array. Now remove the vertex V1 from the front of the queue
and place it in output. Insert V2,V3,V4 and V8 into rear of the queue.
13
Step 3: Consider vertex V2, Find adjacent vertices of V2 are V8,V3,V4, V1 and V5 .
and mark them as visited in the array. Now remove the vertex V2 from the
front of the queue and place it in output. Insert V5 into rear of the queue.

Step 4: Consider vertex V8, Find adjacent vertices of V8 are V8,V3,V4, V1 and V5
and mark them as visited in the array. Now remove the vertex V8 from the
front of the queue and place it in output.

Step 5: Consider vertex V3(next in queue), Find adjacent vertices of V3 are


V1,V2,V4, V8 and V6 and mark V6 as visited in the array because the remaining
vertices are already visited. Now remove the vertex V3 from the front of the
queue and place it in output. Insert V6 into rear of the queue.

Step 6: Consider vertex V4(next in queue), Find adjacent vertices of V4 are


V8,V2,V4, V3 and V5 Already these vertices are visited. Now remove the vertex V4
from the front of the queue.
14
Step 7: Consider vertex V5 (next in queue), Find adjacent vertices of V5 are
V2,V4 and V7. The Vertices V2 and V4 are already visited, so mark V7 as visited in
the array. Now remove the vertex V5 from the front of the queue and place it
in output. Insert V7 into rear of the queue.

Step 8: Consider vertex V6 (next in queue), Find adjacent vertices of V6 are V3


and V7. The Vertices V3 and V7 are already visited. Now remove the vertex V6
from the front of the queue.

Step 8: Consider vertex V7 (next in queue), Find adjacent vertices of V7 are V5


and V6. The Vertices V5 and V6 are already visited. Now remove the vertex V7
from the front of the queue. As front reaches rear (front==rear), the
algorithm terminates.

The traversal path for the given graph by using BFS is


V1 – V2 – V8 - V3 – V4 – V5 – V6 – V7
Class work:
1) Which algorithm traverses the graph vertically in a given graph?
Homework:
1) -------------- data structure is used in DFS Graph traversal algorithm.
2) -------------- data structure is used in BFS algorithm.
15
Previous JNTUK Questions:
1) Write and explain breadth first algorithm with an example. (JNTUK-
Regular- April/May - 2016– SET -1 [8M])
2) Write and explain depth first algorithm with example. (JNTUK-
Regular- April/May - 2016– SET -2 [8M])
Day 36
Topics to be covered: Minimum spanning tree definition. Prim’s algorithm,
Kruskal’s algorithm.
Q1. What is a spanning tree?
A Spanning Tree T of an undirected graph G consists of all vertices of G such
that
i) There are no cycles in spanning tree T
ii) There is a path between each pair of adjacent vertices in spanning tree
T(Spanning means it has to cover every vertex in the graph)

Q2. What is a minimum spanning tree (MST)?


A minimum Spanning Tree T of an undirected graph G is a spanning tree that
is formed from the graph edges that connects all the vertices of graph G at
lowest cost.
Consider a graph G with ‘n’ vertices then the MST will have (n – 1) edges,
assuming that the graph is connected.
Q3. Name the algorithms used to find the minimum spanning tree in a
graph
A minimum spanning tree in a graph can be found out by using the following
algorithms i) Prim’s algorithm ii) Kruskal’s algorithm
Q4. Briefly explain prim’s algorithm with an example.
Prim’s algorithm is used to find the minimum spanning tree of a given graph
and the algorithm is given below.
Step 1: Choose any vertex randomly in the graph as root of the spanning tree
Step 2: Find adjacent vertices to the vertex taken in step 1 and write the
distance values dv in the matrix and choose the vertex with smallest dv for the
next step.
16
Step 3: Find adjacent vertices and if the distance between the adjacent
vertices edge value is less than dv values in the distance matrix table, then
update dv values in the table, otherwise keep dv values without any change in
the table.
Step 4: Repeat the above steps 2 and 3 until all the vertices in the graph are
covered (spanned) such that while selecting the vertices in the spanning tree
does not form any cycles.
Step 5: Finally we will get a minimum spanning tree that can computed by
adding the weights of all edges in the spanning tree.
Example: Consider a graph shown below

Step 1: The initial configuration of the distance matrix for the given graph is

Step 2: Choose vertex V1 as root of the spanning tree. Find the adjacent
vertices of V1 are V2,V3 and V4. Mark V1 as Known in the table and write Pv
values for V2,V3 and V4 as V1 in the table. Write the dv values for V2(V2 - V1 is 2),
V3(V3 - V1 is 4) and V4 (V4 - V1 is 1) as 2,4 and 1 in the table. Select the vertex
with least dv value for unknown vertices in the next step, So choose V4.
17
Step 3: Choose vertex V4 , find the adjacent vertices of V4 and all vertices are
adjacent to it . So mark V4 as Known in the table and write dv values to all
vertices from V4 in the table such that if new dv value is a smaller value for the
vertex in the table of previous step, then only update dv value otherwise keep
the dv value as it is and write the pv values for the vertex selected. For
example for V2 the edge V4 - V2 is 3,but already we have a smaller value of 2
present for V2 in the previous table so don’t update dv , V3(V3 - V1 is 4) and for
V3 the edge V4 - V3 is 2 whereas in the previous table it is 4,So update dv as 2 for
V3. Select the vertex with least dv value for unknown vertices in the next step,
So choose V2.

Step 4: Choose vertex V2. Find the adjacent vertices of V2 are V1,V4 and V5. So
mark V2 as Known in the table and write or update dv values to all vertices
from V2 in the table. Select the vertex with least dv value for unknown vertices
in the next step, So choose V3.

Step 5: Choose vertex V3 , Find the adjacent vertices of V3 are V1,V4 and V6. So
mark V3 as Known in the table and write or update dv values to all vertices
from V3 in the table. Select the vertex with least dv value for unknown vertices
in the next step, So choose V7.
18
Step 6: Choose vertex V7 , Find the adjacent vertices of V7 are V5,V4 and V6. So
mark V7 as Known in the table and write or update dv values to all vertices
from V7 in the table. Select the vertex with least dv value for unknown vertices
in the next step, So choose V6.

Step 7: Choose vertex V6 , Find the adjacent vertices of V6 are V3,V4 and V7. So
mark V6 as Known in the table and write or update dv values to all vertices
from V6 in the table. Select the vertex with least dv value for unknown vertices
in the next step, So choose V5.

Step 7: Choose vertex V5 , Find the adjacent vertices of V5 are V2,V4 and V7. So
mark V5 as Known in the table and no update of dv values in the table. The only
dv value for unknown vertices is 6 for the edge V5-V7, so add this remaining
edge to the spanning tree.

The cost of the minimum spanning tree is 16 (2+1+2+4+6+1)


Q5. Describe Kruskal’s algorithm with an example.
Kruskal’s algorithm takes an undirected graph G with ‘n’ vertices and
algorithm is given below.
Step 1: Find all the edges of the graph G according to their weights in
ascending order.
19
Step 2: Initially the spanning tree is empty and now select the smallest edge
from list of edges in step 1 and this edge to the spanning tree. If the edge is
chosen, then the edge is considered as accepted edge.
Step 3: Repeat the procedure of adding edges (steps 1 and steps 2) to the
spanning tree such that adding edge to the tree does not form any cycle. If
the selected edge forms a cycle, remove it from the tree and the edge is
considered as rejected edge.
Example: Consider the graph shown below

1) Arrange all the edges with their weights in the ascending order.

2) Choose the edge with minimum weight 1(V1-V4) as the first edge of the
spanning tree. This edge is considered as accepted edge because assigning
this edge in spanning tree does not form a cycle.

3) Choose the edge with next minimum weight 1(V6-V7) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.
20

4) Choose the edge with next minimum weight 2(V1-V2) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.

5) Choose the edge with next minimum weight 2(V3-V4) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.

6) Choose the edge with next minimum weight 3(V2-V4) as the next edge of
the spanning tree, but it is rejected because it forms a cycle.
7) Choose the edge with next minimum weight 4(V1-V3) as the next edge of
the spanning tree, but it is rejected because it forms a cycle.
8) Choose the edge with next minimum weight 2(V4-V7) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.

9) Choose the edge with next minimum weight 5(V3-V6) as the next edge of
the spanning tree, but it is rejected because it forms a cycle.
10) Choose the edge with next minimum weight 6(V5-V7) as the next edge of
the spanning tree and it is accepted because it does not forms a cycle.
21
11) The edges with next minimum weights 7, 8 and 10 are rejected because
all of them form a cycle in the graph.
So the final minimum spanning tree is given below and its cost is 16
(1+1+2+2+4+6).
Q5. Write the differences between Prim’s algorithm and Kruskal’s algorithm.
The differences between Prim’s algorithm and Kruskal’s algorithm
Prim’s algorithm Kruskal’s algorithm
1.Prim’s algorithm constructs a 1. Kruskal’s algorithm constructs a
minimum spanning tree by adding a minimum spanning tree by adding
vertex at a time one edge at a time.
2.The time complexity of prim’s 2.The time complexity of Kruskal’s
algorithm is O(E+V log V) algorithm is O(E log V)
3.Prim’s algorithm selects the edges 3. Kruskal’s algorithm selects edges
only from the set of edges that are from the set of all possible edges of a
adjacent to the vertices graph in ascending order.
4. Prim’s algorithm works better (or 4. Kruskal’s algorithm works better (or
is chosen) when we have more is chosen) when we have more
number of vertices in a graph. number of edge and less number of
vertices in a graph.

Class work:
1) Define minimum spanning tree.
Homework:
1) -------------- algorithm builds spanning tree by adding one edge at a time.
Previous JNTUK Questions:
1) Explain Prim’s algorithm with an example. Give analysis of prim’s algorithm
(JNTUK-Regular- April/May - 2016– SET -4 [10M])
2) Explain Kruskal’s algorithm with example. (JNTUK-Regular- April/May -
2016– SET -3 [8M])
3) What is a minimum spanning tree? Explain with an example, Kruskal’s
algorithm for constructing a minimum cost spanning tree. (JNTUK-Regular-
May/June - 2015– SET -2 [8M])
Day 37
Topics to be covered: Single-source shortest path algorithm–Dijkstra’s
algorithm.
Q1. What is single source shortest path?
A Single-source shortest path is defined as finding the path from a single
source vertex to all the other vertices in the given graph with lowest cost.
Q2. Describe Dijkstra’s algorithm with an example.
22
Dijkstra’s algorithm is used to solve the single source shortest path problem.
Dijkstra’s algorithm is an example of greedy method.
In this algorithm we assume all the weights attached are positive and a
digraph is taken for this algorithm.
Step 1: Select the source vertex V from which the shortest path has to be
calculated.
Step 2: Find adjacent vertices to the vertex V that has the shortest(smallest)
dv value among all vertices and mark that vertex as known by putting a value
‘1’ in the Known column of the table.
Step 3: Repeat this procedure of selecting the adjacent vertices and finding
the shortest through this vertex until all the vertices are covered in dv of the
table.
Example:

Step 1: Select the source vertex as V1, but the value of dv and pv are taken as 0
to indicate that it is a source vertex. Find adjacent vertices to V1 are V2 and V3
The distances are V1 – V2 = 2
V1 – V4 = 1
Update the dv values for V2 and V4 as 2 and 1 in the table.

From the table choose the least dv value i.e. 1 for vertex V4 and choose V4 as
vertex for the next step.
Step 2: Select the vertex V4, find adjacent vertices to V4 are V3, V6,V5 and V7
The distances (dv) are for V3 = 3 (V1 – V4 – V3 ) (1+2= 3)
for V5 = 3 (V1 – V4 – V5 ) (1+2= 3)
23
for V6 = 9 (V1 – V4 – V6 ) (1+8= 9)
for V7 = 5 (V1 – V4 – V6 ) (1+4= 5)
So update the values of dv for V3, V5, V6, V7 because they have ∞ values for V3,
V5, V6, V7 in the previous table. Mark Vertex V4 as known in the table.

From the table choose the least dv value for unknown vertices i.e. 2 for vertex
V2 and choose V2 as vertex for the next step.
Step 3: Select the vertex V2, find adjacent vertices to V2 are V4 and V5
The distances (dv) are for V4 = 3 (V2 – V4)
for V5 = 10 (V2 –V5 )
But vertex V4 is already known. So do not update the values of dv for V4, and V5
because they have least values for V4, V5 have 1 and 3 in the previous table.
Mark Vertex V2 as known in the table.

From the table choose the least dv value for unknown vertices i.e. 3 for vertex
V5 and choose V5 as vertex for the next step.
Step 4: Select the vertex V5, find adjacent vertices are V7
The distances (dv) are for V7 = 6 (V5 – V7)
So do not update the values of dv for V7, because they have least value of 5 for
V7.
24
From the table choose the least dv value for unknown vertices i.e. 3 for vertex
V3 and choose V3 as vertex for the next step
Step 5: Select the vertex V3, find adjacent vertices are V6
The distances (dv) are for V6 = 8 (V1 – V4 – V3 – V6) = (1+2+5= 8)
So update the values of dv for V6 as 8 in the table.

From the table choose the least dv value for unknown vertices i.e. 5 for vertex
V7 and choose V7 as vertex for the next step.
Step 6: Select the vertex V7, find adjacent vertices are V6
The distances (dv) are for V6 = 6 (V1 – V4 – V7 – V6) = (1+4+1= 6)
So update the values of dv for V6 as 6 in the table.

From the table choose the least dv value for unknown vertices i.e. 6 for vertex
V6 and is the only vertex not visited, choose V6 as vertex for the next step.
Step 7: Select the vertex V6, find adjacent vertices to V6, There are no adjacent
vertices to V6. Mark as known in the table

Hence the shortest path from vertex V1 to all vertices (single source shortest
path) for the given graph is shown below.
25
Vertex Path Shortest distance
V2 V1 – V2 2
V3 V1 – V4 – V3 3
V4 V1 – V4 1
V5 V1 – V4 – V5 3
V6 V1 – V4 – V7 – V6 6
V7 V1 – V4 – V7 5

Class work:
1) What is single-source shortest path?
Home work:
1) -------------- algorithm is used to solve the single-source shortest path
problem.
Previous JNTUK Questions:
1) How to find shortest path between two vertices using Dijkstra’s
algorithm? (JNTUK-Regular- April/May - 2016– SET -2 [8M])
2) Explain single source shortest path problem with an example. (JNTUK-
Regular- May/June - 2015– SET -1 [8M])
3) Develop an algorithm to compute the shortest path using Dijkstra’s
algorithm. Validate the algorithm with a suitable example. (JNTUK-
Regular- May/June - 2015– SET -3 [16M])
Day 38
Topics to be covered: All pairs shortest path algorithms: Warshall’s algorithm
and Floyd’s algorithm
Q1. Define Transitive closure of a graph.
Consider a graph G=(V,E) and has 3 vertices x, y and z, if there are 2 edges x
y , yz , then we can derive a new edge x z. A graph containing all the edges of
this nature is called transitive closure of given graph G.
Q2. Name the algorithms used to find the all pairs shortest path of a graph
All pairs shortest path can be found out by using the following algorithms
i) Warshall’s algorithm ii) Floyd’s algorithm
Q3. Describe an algorithm to find transitive closure of a graph.
Warshall’s algorithm is used to find the transitive closure of a given graph by
constructing a Boolean matrix called Reachability matrix(R) of the given
graph.
In Warshall’s algorithm, we construct a sequence of Boolean matrices
R(0),R(1),R(2) ...............R(n)
Step 1 : For the given graph ‘G’, find the adjacency matrix of graph G and call
this adjacency matrix as R(0).
26
Step 2 : Find R from R and R represents reachability matrix in which only
(1) (0) (1)

one intermediate vertex is used in the path from source vertex Vi to


destination vertex Vj.
Step 3 : Construct R(2) from R(1) and R(2) represents reachability matrix in which
only two intermediate vertices are used in the path from source vertex Vi to
destination vertex Vj.
Step 3 : Similarly Construct R(3) from R(2) , R(4) from R(3) .......... R(n) from R(n-1) by
using the following formula
Rij(k) = Rij(k-1) OR { RiK(k-1) AND Rkj(k-1) }
Step 4 : Finally we get R(n) matrix which gives us the transitive closure of a
given graph.
‘C’ Code for Warshall’s algorithm:
Int warshall’s algorithm( matrix A[n][n] )
{
Input : Adjacency matrix A[n][n] of given graph.
Output : Transitive closure of given graph.
R(0) = adjacency matrix A[n][n];
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
for(k=1; k<=n; k++)
{
R (k) [i][j]= R (k-1) [i][j] OR { R(k-1) [i][k]AND R(k-1) [k][j] } ;
}
}
}
Return R (k) [i][j] ;
}
Example: Consider graph given below

The adjacency matrix for the given graph is

Step 1 : Assign adjacency matrix of given graph to R(0)


27

Step 2 : Find R(1) from R(0) by using the following formula


Rij(k) = Rij(k-1) OR { RiK(k-1) AND Rkj(k-1) }
First row:
R11(1) = R11(0) OR (R11(0) AND R11(0) ) = 0 OR (0 AND 0 ) = 0
R12(1) = R12(0) OR (R11(0) AND R12(0) ) = 1 OR (0 AND 1 ) = 1
R13(1) = R13(0) OR (R11(0) AND R13(0) ) = 0 OR (0 AND 0 ) = 0
Second row:
R21(1) = R21(0) OR (R21(0) AND R11(0) ) = 0 OR (0 AND 1 ) = 0
R22(1) = R22(0) OR (R21(0) AND R12(0) ) = 0 OR (0 AND 1 ) = 0
R23(1) = R23(0) OR (R21(0) AND R13(0) ) = 1 OR (0 AND 0 ) = 1
Third row:
R31(1) = R31(0) OR (R31(0) AND R11(0) ) = 1 OR (1 AND 0 ) = 1
R32(1) = R32(0) OR (R31(0) AND R12(0) ) = 0 OR (1 AND 1 ) = 1
R33(1) = R33(0) OR (R31(0) AND R13(0) ) = 0 OR (1 AND 0 ) = 0

Step 3 : Find R(2) from R(1) by using the following formula


Rij(k) = Rij(k-1) OR { RiK(k-1) AND Rkj(k-1) }
First row:
R11(2) = R11(1) OR (R12(1) AND R21(1) ) = 0 OR (1 AND 0 ) = 0
R12(2) = R12(1) OR (R12(1) AND R22(1) ) = 1 OR (1 AND 0 ) = 1
R13(2) = R13(1) OR (R12(1) AND R23(1) ) = 0 OR (1 AND 1 ) = 1
Second row:
R21(2) = R21(1) OR (R22(1) AND R21(1) ) = 0 OR (0 AND 0 ) = 0
R22(2) = R22(1) OR (R22(1) AND R22(1) ) = 0 OR (0 AND 0 ) = 0
R23(2) = R23(1) OR (R22(1) AND R23(1) ) = 1 OR (0 AND 1 ) = 1
Third row:
R31(2) = R31(1) OR (R32(1) AND R21(1) ) = 1 OR (1 AND 0 ) = 1
R32(2) = R32(1) OR (R32(1) AND R22(1) ) = 1 OR (1 AND 0 ) = 1
R33(2) = R33(1) OR (R32(1) AND R23(1) ) = 0 OR (1 AND 1 ) = 1
28
Step 4 : Find R from R by using the following formula
(3) (2)

Rij(k) = Rij(k-1) OR { RiK(k-1) AND Rkj(k-1) }


First row:
R11(3) = R11(2) OR (R13(2) AND R31(2) ) = 0 OR (1 AND 1 ) = 1
R12(3) = R12(2) OR (R13(2) AND R32(2) ) = 1 OR (1 AND 1 ) = 1
R13(3) = R13(2) OR (R13(2) AND R33(2) ) = 1 OR (1 AND 1 ) = 1
Second row:
R21(3) = R21(2) OR (R23(2) AND R31(2) ) = 0 OR (1 AND 1 ) = 1
R22(3) = R22(2) OR (R23(2) AND R32(2) ) = 0 OR (1 AND 1 ) = 1
R23(3) = R23(2) OR (R23(2) AND R33(2) ) = 1 OR (1 AND 1 ) = 1
Third row:
R31(2) = R31(1) OR (R32(1) AND R21(1) ) = 1 OR (1 AND 1 ) = 1
R32(2) = R32(1) OR (R32(1) AND R22(1) ) = 1 OR (1 AND 1 ) = 1
R33(2) = R33(1) OR (R32(1) AND R23(1) ) = 1 OR (1 AND 1 ) = 1

Hence the transitive closure of given graph is

Q4. Define distance matrix.


A distance matrix is defined as follows

Q5. What is all pairs shortest path?


Ans) All pairs shortest path problem is defined as finding the shortest path or
distance between each and every pair of vertices (Vi , Vj) in the given graph.
Q6. Explain in detail the Floyd’s algorithm to find all pairs shortest path of a
graph.
Floyd’s algorithm is used to find all pairs shortest path of a given graph and
uses a distance matrix to compute the shortest path.
Floyd’s algorithm computes a sequence of distance matrices D(0), D(1),
D(2), D(3)...................... D(n)
29
Where D represents the distance matrix to find the shortest path with one
(1)

intermediate vertex. D(2) represents the distance matrix to find the shortest
path with two intermediate vertices. D(3) represents the distance matrix to
find the shortest path with three intermediate vertices.
Finally D(n) represents the distance matrix that gives the all pairs shortest
(shortest distance or path between every pair of vertices)
‘C’ Code for Floyd’s algorithm:
int Floyd’s algorithm( distance matrix A[n][n] )
{
Input : Distance matrix A[n][n] of given graph.
Output : All pairs shortest path of the given graph.
D(0) = distance matrix A[n][n];
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
for(k=1; k<=n; k++)
{
D (k) [i][j]= min {D (k-1) [i][j] , (D(k-1) [i][k] + D(k-1) [k][j] } ;
}
}
}
Return D (k) [i][j] ;
}
Example: Consider graph given below

The distance matrix for the given graph is

Step 1 : Assign distance matrix of given graph to D(0)


30
Step 2: Find D from D by using the following formula
(1) (0)

Dij(k) = Min {Dij(k-1) , ( DiK(k-1) + Dkj(k-1) ) }


First row:
D11(1) = Min { D11(0) , (D11(0) + D11(0) ) } = Min { 0, (0 + 0 ) } = 0
D12(1) = Min { D12(0) , (D11(0) + D12(0) ) } = Min { 8 , (0 + 8 ) } = 8
D13(1) = Min { D13(0) , (D11(0) + D13(0) ) } = Min { 5 , (0 + 5 ) } = 5
Second row:
D21(1) = Min { D22(0) , (D21(0) + D12(0) ) = Min { 3, (3 + 0 ) } = 3
D22(1) = Min { D22(0) , (D21(0) + D12(0) ) = Min { 0, (3 + 8 ) } = 0
D23(1) = Min { D23(0) , (D21(0) + D13(0) ) = Min { ∞, ( 3+ 5 ) } = 8
Third row:
D31(1) = Min { D31(0) , (D31(0) + D11(0) ) = Min { ∞, (∞ + 0 ) } = ∞
D32(1) = Min { D32(0) , (D31(0) + D12(0) ) = Min { 2, (∞ + 8 ) } = 2
D33(1) = Min { D33(0) , (D31(0) + D13(0) ) = Min { 0, (∞ + 5 ) } = 0

Step 3: Find D(2) from D(1)


First row:
D11(2) = Min { D11(1) , (D12(1) + D21(1) ) } = Min { 0, (8 + 3 ) } = 0
D12(2) = Min { D12(1) , (D12(1) + D22(1) ) } = Min { 8 , (8 + 0 ) } = 8
D13(2) = Min { D13(1) , (D12(1) + D23(1) ) } = Min { 5 , (8 + 8 ) } = 5
Second row:
D21(2) = Min { D21(1) , (D22(1) + D21(1) ) = Min { 3, (0 + 3 ) } = 3
D22(2) = Min { D22(1) , (D22(1) + D22(1) ) = Min { 0, (0 + 0 ) } = 0
D23(2) = Min { D23(1) , (D22(1) + D23(1) ) = Min { 8, ( 0+ 0 ) } = 8
Third row:
D31(2) = Min { D31(1) , (D31(1) + D11(1) ) = Min { ∞, (2 + 3 ) } = 5
D32(2) = Min { D32(1) , (D31(1) + D12(1) ) = Min { 2, (2 + 0 ) } = 2
D33(2) = Min { D33(1) , (D31(1) + D13(1) ) = Min { 0, (2 + 8 ) } = 0

Step 3: Find D(3) from D(2)


First row:
D11(3) = Min { D11(2) , (D13(2) + D31(2) ) } = Min { 0, (5 + 5 ) } = 0
D12(3) = Min { D12(2) , (D13(2) + D32(2) ) } = Min { 8 , (5 + 2 ) } = 7
D13(3) = Min { D13(2) , (D13(2) + D33(2) ) } = Min { 5 , (5 + 0 ) } = 5
31
Second row:
D21(3) = Min { D21(2) , (D23(2) + D31(2) ) = Min { 3, (8 + 5 ) } = 3
D22(3) = Min { D22(2) , (D23(2) + D32(2) ) = Min { 0, (8 + 2 ) } = 0
D23(3) = Min { D23(2) , (D23(2) + D33(2) ) = Min { 8, ( 8+ 0 ) } = 8
Third row:
D31(3) = Min { D31(2) , (D33(2) + D31(2) ) = Min { 5, (0 + 5 ) } = 5
D32(3) = Min { D32(2) , (D33(2) + D32(2) ) = Min { 2, (0 + 2 ) } = 2
D33(3) = Min { D33(2) , (D33(2) + D33(2) ) = Min { 0, (0 + 0 ) } = 0

So the shortest path between all pairs of vertices is


Pair of vertex Path Shortest distance
V1 – V2 V1 – V3 - V2 7
V1 – V3 V1 – V3 5
V2 – V1 V2 – V1 3
V2 – V3 V2 – V1 – V3 8
V3 – V1 V3 – V2 – V1 5
V3 – V2 V3 – V2 2
Class work:
1) What is all pairs shortest path?
Homework:
1) -------------- algorithm is used to solve all pairs shortest path problem
Previous JNTUK Questions:
1) How to find shortest path between vertices using all pairs shortest path
Floyd’s algorithm. (JNTUK-Regular- April/May - 2016– SET -1 [8M])
2) What is transitive closure? Which algorithm uses transitive closure in
calculating shortest path? Explain it with an example. (JNTUK-Regular-
May/June - 2015– SET - 4 [16M])

You might also like