Unit-5 - Graphs Feb 2024
Unit-5 - Graphs Feb 2024
Graph:
Consider a courier boy who has to deliver various mails/items to different addresses assigned to
him. The guy picks the packets from the office, and after delivering the items, he comes back to the
office to submit the reports and undeliverable items (if any). The guy will have to plan his travel such
that he does not have to repeat the location he already has visited. Such a scenario can intelligently
be planned if we have the representation of locations and their connections in the form of Graph.
A social graph is also an example of a graph that illustrates interconnections among people and
organizations in a social network.
A level-up application of social Graph is LinkedIn that follows the connections of individuals with
each other and the organizations and industries. It facilitates the organizations in targeting the
right person for the recruitment and persons targeting the organization of their choices.
Similarly, Google and Microsoft offer various services online, e.g., calendar, mailing, document,
spreadsheet, forms, etc. All these services are interconnected with each other. The same can also
be considered as Graph.
1
Figure 4.2: Software services Graph and Social Graph (Linked-in)
A Graph is a non-linear data structure* consisting of vertices/nodes and edges/lines. A graph can
be used to define pair-wise relation between the objects.
(In a non-linear data structure, elements stored in it follow some hierarchy)
The above figure is an example of an undirected graph (A graph in which edges do not have any
direction). Each edge is identified with an unordered pair [u, v] of nodes/vertices in Graph G where
edge E begins at u and ends at v.
2
Figure 4.4: Example of directed Graph
The above figure is an example of a directed graph (in which edges have directions). Each edge E is
identified with an ordered pair (u, v) of nodes in Graph G. Directed graphs are also called digraphs.
For the above Graph
V = {A, B, C, D} and
E = {(A, B), (A, D), (D, B), (C, B)}
There is direct connection from node A to B, but not from B to A. Similarly, direct connection exists
from node D to B, but not from B to D.
1. Degree of vertex
The degree of a vertex is the number of edges connecting it.
Notation – degree (V)
3
e 0
Adjacent Vertices
Two nodes or vertices are adjacent if they are connected to each other through an edge. Vertex v1
is adjacent to a vertex v2 if there is an edge (v1, v2) or (v2, v1). Let us consider the following Graph:
Figure 4.12
Path
4
A path is a sequence of vertices with the property that each vertex in the sequence is adjacent to
the vertex next to it.
Example: Undirected Graph
Path Type
Simple Path: A path is simple path if all of its vertices are distinct.
Closed Path: A path is closed path if the first vertex is the same as the last vertex.
5
Figure 4.15: Path Type
Null Graph
A graph is known as a null Graph if there are no edges in the Graph. In other words, a graph whose
edge set is empty is called a null graph.
Trivial Graph
Graph having only a single vertex and no edges, and it is the smallest possible Graph.
Regular Graph
6
A graph in which the degree of all the vertices is the same is called a regular graph.
If all the vertices in a graph are of degree 'k', it is called a "k-regular graph."
Connected Graph
The Graph in which we can visit any other node in the Graph from one node is known as a
connected graph. At least one path exists between every pair of vertices in a connected Graph.
Disconnected Graph
The Graph in which at least one node is not reachable from a node is a disconnected graph.
The Graph in Fig 12 consists of two independent components that are disconnected.
Complete Graph
7
A graph is a complete Graph in which precisely one edge is present between every pair of
vertices.
A complete graph of ‘n’ vertices contains exactly nC2edges.
A simple graph of 'n' vertices (n>=3) and n edges forming a cycle of length 'n' is called a cycle graph.
In a cycle graph, all the vertices are of degree 2. In other words, a cycle is a simple closed path.
Cyclic Graph
8
Acyclic Graph
Multi Graph
A graph that consists of parallel edges and self-loops is called a multigraph. Parallel edges are those
which connect the same pair of vertices, and self-loops have the same vertex as the endpoints
The Graph above has multiple edges between a to b and b to c. It has self-loop from e to e.
Labelled Graph
A graph that has labels associated with each edge or each vertex is called Labelled Graph.
Figure 4.26 (a) Unlabelled Graph (b) Vertex-labelled Graph (c) edge Labelled Graph
Weighted Graph
9
A graph having a weight, or number, associated with each edge is called Weighted Graph.
Bi-Partite Graph
A bipartite graph is a set of graph vertices decomposed into two disjoint sets such that no graph
vertices within the same are adjacent. It is also called a bi-graph.
Euler Graph
A connected graph G is an Euler graph if all vertices of G are of even degree. In other words, a
connected graph G is an Euler graph, if and only if its edge set can be decomposed into cycles.
10
Figure 4.29. Two examples of Euler Graph
Hamiltonian Graph
A connected graph G is called a Hamiltonian graph if there is a cycle that includes every vertex of
G and the obtained cycle is called the Hamiltonian cycle.
Similarity: Both Tree and Graph are non-linear data structures that contain nodes and edges (used
to connect nodes).
Difference:
• The Tree does not contain any Cycle but the Graph may or may not contain a cycle.
• A root is always defined in a Tree that is primarily used to identify the starting point in
Tree. In a Graph it is not essential to define any vertex as a root.
11
4.1.6. Applications of Graph:
a) Google maps: One of the most common applications of the Graph is the Google maps, where
cities are considered vertices and connection between cities are represented as edges. Whenever
we have to find a route between two stations, the shortest path algorithm works to select the
shortest route between the pair of vertices (e.g. Dijkstra Algorithm, to be discussed in subsequent
text in this chapter).
b) Social networks like Facebook, Instagram, LinkedIn etc, are another example of Graph in which
people are connected with each other just like edges connected in the Graph. It is an example of
an undirected Graph. In Facebook, every user is considered a vertex in Graph. When user1 sends a
friend request to user2, an edge is formed between user1 and user2.
12
c) World wide Web: The world wide web (www) has become the primary information source. It
consists of a large number of pages that are tied together using hyperlinks. This www can be
considered as a directed graph in which a web page can be considered a node, and the hyperlink
can be considered an edge.
d) In operating system, Resource Allocation graph are also example of Graph in which resources
and processes are considered as vertices and there will be either request edge (if a process request
for resource) or assignment edge (if resource is allocated to a process).
Figure 4.8: - Resource Allocation Graph (Rectangles represent Resources and Circles represent Processes)
In practice, it is not possible to represent the Graph by using node structure just like binary tree
representation. There are three types of representation of graphs in the memory.
1. Sequential Representation-
The Graph can be represented sequentially by using matrices. It is of two types-
13
Adjacency –Matrix Representation-
In this representation, take a two-dimensional integer type array whose order depends on the
number of vertices in the given Graph. Let us suppose that if the given Graph contains v vertex,
then the order of the two-dimensional array is vXv.
Adjacency matrix is also called bit matrix or Boolean matrix. If an edge exists between two nodes,
it stores true (represented by 1) otherwise false (represented by 0).
14
In the above diagram A to B edge exist, so in the matrix A to B is 1. A to C edge does not exist, so in
the matrix, A to C is 0.
Points to Remember-
In the case of a directed Graph, the sum of the matrix is always equal to the number of edges |E|
or the sum of in-degree of each vertex.
In above diagram
Number of edges= 6
Sum of matrix = 6
Number of edges = Sum of Matrix
In the above diagram A to B edge exist, so in the matrix A to B is 1. A to C edge does not exist, so in
the matrix, A to C is 0.
Points to Remember-
In the case of an undirected Graph, the sum of the matrix is always equal to twice the number of
edges i.e., 2|E| or sum of degree of each vertex.
In above diagram,
Number of edges= 9, Sum of matrix= 18
Sum of matrix=2|E|
15
Example (Weighted Directed Graph)
In the above diagram A to B edge exist, so in the matrix A to B is 2. A to C edge does not exist, so in
the matrix A to C is 0.
In the above diagram, edge exists between 1 to 2 edge, so in the matrix 1 to 2 entry is 28. Edge
does not exist between 1 to 3, so in the matrix 1 to 3 entry is 0.
16
If multiple edge exists then it counts 2 and if single edge exists then it counts only 1.
Let us suppose that there are four persons A, B, C and D stores in Array v of characters
v [4] = {‘A’ , ‘B’ , ‘C’ , ‘D’}
Let us suppose there is a 2-dimensional array named e, which stores connections between persons.
{0 , 0,1, 0},
{0 , 0,0, 0},
{0 , 0,1, 0} };
Let us assume that the index of the person in v array represents the unique number of that person.
ALGORITHM uniqueNumber(ver)
BEGIN:
FOR i=0 TO N DO
IF v[i]==ver THEN
RETURN i
RETURN -1
END;
Explanation-
This algorithm takes the vertex name as input and returns its index number. For example, if the
input parameter is 'B' then the algorithm returns 1, which is the index number of vertex 'B.'
17
ALGORITHM isAdjacent(ver1,ver2)
i=uniqueNumber(ver1)
j=uniqueNumber(ver2)
IF i == –1 || j== –1 THEN
RETURN false
RETURN e[i][j]
END;
Explanation-
This algorithm takes two vertices as parameters and returns the corresponding value in e[i][j] if
vertex names exist in a given Graph. For example, if the input parameter is 'A' and 'B', it returns 1.
ALGORITHM displayAdjacentNode(v)
Explanation-
This algorithm takes the vertex name as a parameter and displays all its adjacent vertices. For
example, if input parameter is 'A', it displays 'B' and 'D'.
Complexity-
If uniqueNumber() is implemented using hash table then isAdjacent() takes constant time. This is
one of the biggest advantage of adjacency matrix implementation.
18
Power of Adjacency Matrix-
Let us suppose that A be the adjacency matrix of given Graph then A to the power of N (A N) gives
the number of paths of length N from Vi to Vj.
Path Matrix-
Path matrix is defined as of order V x V, where V is the number of vertex defined as-
A[i][j] =
0 otherwise
19
P[i][j]=
0 otherwise
B) Incidence Matrix-
Incidence matrix of order V x E, where V is the number of vertex and E is number of edges defined
as-
0 otherwise
20
Adjacency list defined as – A link list for each adjacent node of the given vertex.
Explanation-
In this algorithm, input parameter v [ ] is the array of structure and structure element contains
data (char type) and head (a pointer which is node type). P and Q are node type structures (node
type structure contains char type data and address field of type node).
21
ALGORITHM Delete(v[ ], n)
ALGORITHM isAdjacent(ver1,ver2)
ALGORITHM displayAdjacentNode(ver)
Method-
Let us take an example Allahabad, and the hash table size is 100.
Sum the ASCII value of each character. If the sum is more than 99 then again sum each digits until
it is less than or equal to 99
This is the simplest method to map city names into hash table. You can choose any method
according to your requirement.
Google also maps city names into a hash table by taking the longitude and latitude of the city
name.
Set Representation
An algebraic structure G(v, e) where v is a non-empty set of vertices and e is the non-empty set of
edges called graph. Therefore Graph also represented as-
V = {A, B, C, D}
E = {AB, BC, AC, CD}
23
Graph Traversal:
Many graph algorithms require one to systematically examine the nodes and edges of a graph
G. there are two standard ways that is done. one way is called Breadth First Search (BFS) and
another is called Depth First Search (DFS). the BFS search will use queue as an auxiliary
structure to hold nodes for future processing, and analogously, the DFS will use a stack.
During the execution of our algorithms each node N of G will be in one of three states called
the status of N, as follows
STATUS = 1 : (Ready State.) The initial state of the Node N
STATUS = 2 : (Waiting State.) The Node N is on the queue or stack, waiting to be processed.
STATUS = 3: (Processed State.) The Node N has been processed.
We now discuss the two searches separately.
The above algorithm will process only those nodes which are reachable from the starting note
A. Suppose one wants examine all the nodes in the graph G. Then the algorithm must be
modified so that it begin again with another node (which we will call B) that is a still in the
ready state. This node B can be obtained by traversing the list of nodes.
24
Adjacency list
A F,C,B
B G,C
C F
D C
E D,C,J
F D
G C,E
J D,K
K E,G
b. Remove the front element A from Q by setting FRONT : FRONT + 1, and add to queue
the neighbors of A as follows
FRONT = 2 Q: A, F, C, B
REAR = 4 BFS : A
note that the origin A of each of the three edges is added to ORIG
c. Remove the front element F from Q by setting FRONT : FRONT + 1, and add to QUEUE
the neighbors of F as follows
FRONT = 3 Q: A, F, C, B, D
REAR = 4 BFS : A, F
d. Remove the front element C from Q by setting FRONT : FRONT + 1, and add to QUEUE
the neighbors of C (which are in the ready state) as follows
FRONT = 4 Q: A, F, C, B, D
REAR = 5 BFS : A, F, C
Note that the neighbor F of C is not added to Q, since F is not in the ready state (because
F has already been added to Q)
e. Remove the front element B from Q by setting FRONT : FRONT + 1, and add to QUEUE
25
the neighbors of B (which are in the ready state) as follows
FRONT = 5 Q: A, F, C, B, D, G
REAR = 6 BFS : A, F, C, B
Note that only G is added to Q, since the other neighbor, C is not in the ready state
f. Remove the front element D from Q by setting FRONT : FRONT + 1, and add to QUEUE
the neighbors of D (which are in the ready state) as follows
FRONT = 6 Q: A, F, C, B, D, G
REAR = 6 BFS : A, F, C, B, D
g. Remove the front element G from Q by setting FRONT : FRONT + 1, and add to QUEUE
the neighbors of G (which are in the ready state) as follows
FRONT = 7 Q: A, F, C, B, D, G, E
REAR = 7 BFS : A, F, C, B, D, G
h. Remove the front element E from Q by setting FRONT : FRONT + 1, and add to QUEUE
the neighbors of E (which are in the ready state) as follows
FRONT = 8 Q: A, F, C, B, D, G, E, J
REAR = 8 BFS : A, F, C, B, D, G, E
I. Remove the front element J from Q by setting FRONT : FRONT + 1, and add to QUEUE
the neighbors of J (which are in the ready state) as follows
FRONT = 8 Q: A, F, C, B, D, G, E, J, K
REAR = 8 BFS : A, F, C, B, D, G, E, J
J. Remove the front element K from Q by setting FRONT : FRONT + 1, and add to QUEUE
the neighbors of J (which are in the ready state) as follows
FRONT = 8 Q: A, F, C, B, D, G, E, J, K
REAR = 8 BFS : A, F, C, B, D, G, E, J, K
Final BFS : A, F, C, B, D, G, E, J, K
Complexity
26
Insert and Delete operation from queue take O(1) time, and so the total time used for these
operations is O (V). The total time used in scanning adjacency lists is O(E). The overhead for
initialization is O(V), so the time complexity of the BFS algorithm is O(V+E).
Adjacency list
A F,C,B
B G,C
C F
D C
E D,C,J
F D
G C,E
J D,K
K E,G
Nodes A B C D E F G J K
Status 1 1 1 1 1 1 1 1 1
STACK A
DFS
b. Pop and print the top element A then push onto the stack all the neighbors of A (those are in
the ready state
Nodes A B C D E F G J K
Status 3 2 2 1 1 2 1 1 1
27
STACK F, C, B
DFS A
a. Pop and print the top element B then push onto the stack all the neighbors of B (those are in
the ready state
Nodes A B C D E F G J K
Status 3 3 2 1 1 2 2 1 1
STACK F, C, G
DFS A, B
a. Pop and print the top element G then push onto the stack all the neighbors of G (those are in
the ready state
Nodes A B C D E F G J K
Status 3 3 2 1 2 2 3 1 1
STACK F, C, E
DFS A, B, G
a. Pop and print the top element J then push onto the stack all the neighbors of J (those are in the
ready state
Nodes A B C D E F G J K
Status 3 3 2 2 3 2 3 3 2
STACK F, C, D, K
DFS A, B, G, E, J
a. Pop and print the top element K then push onto the stack all the neighbors of K (those are in
the ready state
Nodes A B C D E F G J K
Status 3 3 2 2 3 2 3 3 3
STACK F, C, D
DFS A, B, G, E, J, K
a. Pop and print the top element D then push onto the stack all the neighbors of D (those are in
the ready state
Nodes A B C D E F G J K
Status 3 3 2 3 3 2 3 3 3
STACK F, C
DFS A, B, G, E, J, K, D
a. Pop and print the top element C then push onto the stack all the neighbors of C (those are in
the ready state
Nodes A B C D E F G J K
Status 3 3 3 3 3 2 3 3 3
28
STACK F
DFS A, B, G, E, J, K, D, C
a. Pop and print the top element F then push onto the stack all the neighbors of F (those are in the
ready state
Nodes A B C D E F G J K
Status 3 3 3 3 3 3 3 3 3
STACK
DFS A, B, G, E, J, K, D, C, F
29
Spanning Tree:
Consider a situation where we can connect a network cable from the main office to various
customers to provide a network connection.
Figure:
For an undirected graph G, A spanning tree T is a sub graph which includes all the vertices of graph
G and having no cycle. A graph may have numerous spanning trees. There will no spanning tree if
the graph is not connected.
The minimum spanning tree has various applications including network design, computer network,
transportation network, electricity network, water supply network etc.
30
Figure:
The sum of the weights of all edges is weight of the spanning tree. Therefore, different spanning
trees have different weights.
Let a complete graph G with N vertices. Then the total number of possible spanning trees will be
N(N-2), , i.e., │V│ = N. in the graph. e.g., if graph contains 5 vertices then total number of spanning
trees may be be 5(5-2) = 125
The following procedure is used to find the total number of spanning trees in an incomplete
graph:-
For the given incomplete graph construct Adjacency Matrix.
The value of degree of nodes is put in all of the diagonal elements with. The degree of node 1
Element is placed at (1,1), and the degree of node 2 element is placed at (2,2). Similarly next all.
All non-diagonal elements are replaced with -1.
For any element, determine co-factor. The obtained co-factor is equal to the total number of
possible spanning-tree in graph.
31
The Adjacency Matrix for the given
incomplete graph
Consider a situation where a cable company wants to connect five cities to their network. What is
the minimum length of cable through which these cities may be connected?
Figure:
The given problem is modelled as a network and now it is problem of finding the minimum spanning
tree of given network.
32
Figure:
The minimum spanning tree is the spanning tree having minimum weight. There may be various
minimum spanning trees. In real life, this weight can be considered as distance, congestion,
traffic stack etc.
Kruskal's algorithm for Minimum Spanning Tree
Most of the cable network companies (Landline cable/ TV cable/ Dish TV Network) use Kruskal’s
algorithm to save a huge amount of cable for connecting different offices/ houses. On our trip to
some tourist locations, we plan to visit all the important places and sites, but we are short on time.
Then Kruskal’s algorithm gives us the way to visit all places in minimum time.
Keep adding edges (repeat step 2) until all the vertices are connected and a Minimum Spanning
Tree (MST) is obtained.
Step - 1
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
33
Step - 2
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
Step - 3
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
Step - 4
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
Step - 5
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
34
Step - 6
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
Step - 7
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
Step - 8
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
Step - 9
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
35
Step - 10
Edge ( 1, 6 ) ( 3, 4 ) ( 2, 7 ) ( 2, 3 ) ( 4, 7 ) ( 4, 5 ) ( 5, 7 ) ( 5, 6 ) ( 1, 2 )
Weight 10 12 14 16 18 22 24 25 28
This algorithm finds a safe edge (u, v) of least weight among all the edges that connect any two
trees in the growing forest. It is a greedy algorithm. It is like an algorithm to compute connected
components. In Kruskal's algorithm, set A is a forest. Set A is always a subset of some minimum
spanning tree. The safe edge added to A is always a least-weight edge in the graph that connects
two distinct components. It uses a disjoint-set data structure to maintain several disjoint sets of
elements. Each set contains the vertices in a tree of the current forest. It uses Find_Set(u) and
UNION (u, v) procedure.
ALGORITHM MSTKruskal(G,W[ ][ ])
BEGIN:
E’ = ∅
FOR each vertex v ∈ V [G] DO
Make_Set(v)
Sort the edges of E into non-decreasing order by weight in W
FOR each (u, v) ∈ E DO
IF Find_Set(u) != Find_Set (v) THEN
Add edge (u,v) in E’
UNION (u, v)
RETURN E’
END;
Lines 1-3 initialize the set A as an empty set and create |V| trees, each containing one vertex. Line
4 sorts the edges in E into non-decreasing order by weight. Lines 5-8 (loop for each edge in E)
36
checks that if endpoints u and v belong to the same tree, then the edge (u, v) cannot be added to
the forest, and the edge is discarded. Otherwise, the edge (u, v) is added to A in line 7, and the
vertices in the two trees are merged in line 8.
Applying and executing sort the edges of E into non-decreasing order by weight w
Line No - 4, on given graph
Edge (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
Weight 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Applying and executing for each edge (u, v) ∈ E into non-decreasing order by weight w
Line No - 5 on a given graph for the edge (g, h), u = g and v = h
Edge (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
Weight 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Applying and executing IF Find_Set(u) != Find_Set (v) THEN
Line No - 6, on given graph g and h belongs to a different set, edge (g, h) is included in A
Applying and executing Add edge (u,v) in E’
Line No - 7 on a given graph A = { (g, h) }
Applying and executing UNION (u, v)
Line No - 8 on a given graph {a} {b} {c} {d} {e} {f} { g, h } {i}
81
for the edge (c, i), u = c and v = i
(g,h (c,i (f,g (a,b (g,i (c,d (h,i (a,h (b,c (d,e (e,f (b,h (d,f
Edge ) ) ) ) (c,f) ) ) ) ) ) ) ) ) )
Weigh
t 1 2 2 4 4 6 7 7 8 8 9 10 11 14
c and i belongs to different set, edge (c, i) is included in A, union operation is performed
A = { (g, h), (c, i) }
{a} {b} { c, i } {d} {e} {f} { g, h }
{ a, b }{ c, i } {d} {e} { f, g, h }
82
for the edge (c, f), u = c and v = f
(g,h (c,i (f,g (a,b (g,i (c,d (h,i (a,h (b,c (d,e (e,f (b,h (d,f
Edge ) ) ) ) (c,f) ) ) ) ) ) ) ) ) )
Weigh
t 1 2 2 4 4 6 7 7 8 8 9 10 11 14
c and f belongs to different set, edge (c, f) is included in A, union operation is performed
{ a, b } {d} {e} { c, f, g, h, i }
83
Weigh
t 1 2 2 4 4 6 7 7 8 8 9 10 11 14
c and d belongs to different set, edge (c, d) is included in A, union operation is performed
A = { (g, h), (c, i), (f, g), (a, b), (c, f), (c, d) }
{ a, b } {e} { c, d, f, g, h, i }
{ a, b } {e} { c, d, f, g, h, i }
{e} { a, b, c, d, f, g, h, i }
84
for the edge (b, c), u = b and v = c
Edge (g,h) (c,i) (f,g) (a,b) (c,f) (g,i) (c,d) (h,i) (a,h) (b,c) (d,e) (e,f) (b,h) (d,f)
Weight 1 2 2 4 4 6 7 7 8 8 9 10 11 14
b and c belongs to Same set, so the given edge is not safe. It is not included in set A, no union as well
A = { (g, h), (c, i), (f, g), (a, b), (c, f), (c, d), (a, h) }
{e} { a, b, c, d, f, g, h, i }
{ a, b, c, d, e, f, g, h, i }
85
{ a, b, c, d, e, f, g, h, i }
Prim's algorithm is used to find the minimum spanning tree. It takes a graph as input and gives a
minimum spanning tree as output. In prims, the tree starts from an arbitrary root vertex r and
grows until the tree spans all the vertices in V. Prims follow the greedy strategy since the tree is
89
augmented at each step with an edge that contributes the minimum amount possible to the tree's
weight.
Procedure
Step 1: first initialize the starting node of the graph.
Step 2: initialize the priority queue Q and assign key-value ꝏ to each vertex except root or source
node whose key value is 0.
Step 3: check all adjacent nodes belongs to Q of the current node and compare the new cost with
the existing cost. Select minimum cost edge among all edges connected with adjacent nodes.
Remove current node from Q.
Step 4: make the current node to the minimum cost adjacent node.
Step 5: Repeat steps 3 and 4.
ALGORITHM MST Prims (G, w[ ][ ], r) //Graph G, w is weight matrix and r is the starting node
BEGIN:
FOR each vertex u ϵ V[G] DO // u is current vertex and V[G] is all vertices belongs to graph G.
key[u] = ꝏ // initially key value of all vertices are infinite
∏[u] = NIL // ∏[u] means parent of u and root node have no parent so value is
NIL
Key[r]=0 // value of starting node is 0
Initialize(PQ) // PQ is priority queue
FOR all u ϵ V[G] DO
EnQueue(PQ, u)
WHILE !Empty(PQ) DO // all nodes should be traversed
u = DeQueue(PQ) // Return the vertex with minimum key value
FOR each v ϵ adj[u] DO // v is adjacent node of u
if v ϵ PQ and w(u,v) < key[v] THEN // if v present in Priority Queue
∏[v] = u // parent of v is u
key [v] = w(u,v) // update key value of v
END;
Complexity:
We can divide the algorithm into three parts
Part 1 Extraction of the vertex from the priority queue process takes O(log V) time.
Part 2 Decreasing the key of adjacent vertices takes O(log V) time.
Part 3 updating the key values takes constant time.
So the total running time complexity = O(V log V + E log V)
= O( E log V) E is always greater than V.
Example 1:
Find the minimum spanning tree for the given graph.
90
extracted adjacent selected
1 2 3 4 5 6 7
vertex vertex edges vertex
1,2 =
2 28
0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ 1 6
1,6 =
6 10
91
extracted adjacent selected
1 2 3 4 5 6 7
vertex vertex edges vertex
2 1,2 = 28
0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ 1 6
6 1,6 = 10
1 NA 1 is not in Priority Queue
X 28 ꝏ ꝏ ꝏ 10 ꝏ 6 5
5 6,5 = 25
4 5,4 = 22
X 28 ꝏ ꝏ 25 X ꝏ 5 4
7 5,7 = 24
92
5 is not in Priority
5 NA Queue
7 4,7 = 18
93
extracted adjacent selected
1 2 3 4 5 6 7
vertex vertex edges vertex
2 1,2=28
0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ 1 6
6 1,6=10
1 NA 1 is not in Priority Queue
X 28 ꝏ ꝏ ꝏ 10 ꝏ 6 5
5 6,5=25
4 5,4=22
X 28 ꝏ ꝏ 25 X ꝏ 5 4
7 5,7=24
3 4,3=12
X 28 ꝏ 22 X X 24 4 5 NA 3 5 is not in Priority Queue
7 4,7=18
2 3,2=16
X 28 12 X X X 18 3 2
4 NA 4 is not in Priority Queue
1 NA 1 is not in Priority Queue
X 16 X X X X 18 2 3 NA 7 3 is not in Priority Queue
7 2,7=14
94
3 NA 3 is not in Priority Queue
7 2,7=14
2 NA 2 is not in Priority Queue
X X X X X X 18 7 4 NA 7 4 is not in Priority Queue
5 NA 5 is not in Priority Queue
Example 2:
Find the minimum spanning tree for the given graph.
95
4 1,4=10
5 1,5=6
1 is not in Priority
1 NA Queue
X 1 6 10 6 2 3 2,3=4 5
4 2,4=8
5 2,5=3
96
extracted adjacent selected
1 2 3 4 5
vertex vertex edges vertex
2 1,2=1
3 1,3=6
0 ꝏ ꝏ ꝏ ꝏ 1 2
4 1,4=10
5 1,5=6
1 is not in Priority
1 NA Queue
X 1 6 10 6 2 3 2,3=4 5
4 2,4=8
5 2,5=3
1 is not in Priority
1 NA Queue
2 is not in Priority
X X 4 8 3 5 3
2 NA Queue
3 5,3=4
4 5,4=8
1 is not in Priority
1 NA Queue
2 is not in Priority
X X 4 8 X 3 2 NA 4 Queue
4 3,4=4
4 is not in Priority
5 NA Queue
97
extracted adjacent selected
1 2 3 4 5
vertex vertex edges vertex
2 1,2=1
3 1,3=6
0 ꝏ ꝏ ꝏ ꝏ 1 2
4 1,4=10
5 1,5=6
1 is not in Priority
1 NA Queue
X 1 6 10 6 2 3 2,3=4 5
4 2,4=8
5 2,5=3
1 is not in Priority
1 NA Queue
2 is not in Priority
X X 4 8 3 5 3
2 NA Queue
3 5,3=4
4 5,4=8
1 is not in Priority
1 NA Queue
2 is not in Priority
X X 4 8 X 3 2 NA 4 Queue
4 3,4=4
4 is not in Priority
5 NA Queue
1 is not in Priority
1 NA Queue
X X X 4 X 4
2 is not in Priority
2 NA Queue
98
3 is not in Priority
3 NA Queue
5 is not in Priority
5 NA Queue
Total cost= 1 + 3 + 4 + 4
= 12
Introduction to Shortest Path:
In order to find a path between two vertices in such a manner that the sum of the weights of edges
lies in the path is minimum. We can find the shortest path for the directed, undirected or mixed
graph.
Analogy
Road map between two cities. Source city in Greater Noida and Destination city is Meerut. We have
a different path between these two cities that can better be understood by Google Maps.
Here we have three images of google Maps; each image contains a different path and different
distances. The first image has a distance 84.4 km, the second image has a distance 78 km, and the
third image shows the train path between greater Noida to Meerut. Google map is the best
example to find the shortest path between the source location to the destination location. These
locations can be considered as nodes or vertices in the graph, and roads or paths between cities
can be considered as edges between nodes or vertices of the graph.
117
4.2.3. Types of Shortest Path:
Floyd-Warshall algorithm
118
Single-Source Shortest Path
The Single source shortest path algorithm is used to find the shortest path between a particular
node to the other nodes. In a single-source shortest path, first initialize a source node and find the
minimum distance between the source node to all other nodes of graph G.
Consider a situation in which a person is in city A and wants to go to city D; there are many paths
possible between A to D.
We can see that there are many paths possible between city A to city D. This is very complex to
find all possible path between one node to all other nodes, here we find only one combination
from A to D. if we find A to B, A to C, A to D and A to E, then there are many possible paths. After
finding all the paths, we have to find a minimum path between one city to all other cities. To resolve
this problem, we have two algorithms that solves different types of problems of the single-source
shortest path-
Dijkastra’s Algorithm
119
ALGORITHM Initialize_single_source(G,s)
BEGIN:
FOR each vertex v ϵ V[G] DO // loop executes for all vertices of graph
d[v] = ꝏ // Initially weight of all vertices is assigned ꝏ. We consider that there // is no path
between any vertices. Update weight of vertices after // traversal the path between two vertices.
∏[v] = NIL // Initially we consider that there is no path so there is no predecessor // of any
vertex.
d[s] = 0 // Assign 0 weight to the starting/ source vertex.
END;
Relax algorithm is called for updating the weight of the vertex.
ALGORITHM Relax(u, v, w)
BEGIN:
IF d[v] > d[u] + w(u, v) THEN //existing weight (d[v]) is greater than current weight(weight // at
vertex u + weight of edge between u & v)
d[v] = d[u] + w(u, v) // if condition true then update weight of vertex v else leave // as it is
∏[v] = u // u is predecessor of v
END;
Dijkstra’s algorithm is a single source shortest path algorithm in which we find the shortest path
between the source node to other nodes. It takes a weighted graph as input and gives the shortest
path between the source node to other nodes. We can apply Dijkstra’s algorithm only when the
graph is directed, weighted, and weights are non-negative. If weights are negative, then we can
apply bellman-ford, which we will discuss later.
Procedure
Step1:First we initialize the single source.
Step 2: Set weight of source vertex as 0, and weight of other vertices as ꝏ.
Step 3: Set a priority Queue for all vertices on the basis of weight.
Step 4:Extract the min from the priority Queue.
Step 5: Find the adjacent node update the weight of the adjacent node if the net weight is less than
the existing weight else retain existing weight.
Step 6:Update priority queue.
Step 7:Repeat steps 4 to 6 until the priority queue empty.
ALGORITHM Dijkastra(G, w[ ][ ], s)
BEGIN:
Initialize_single_source(G, s) // Initialize the source/starting vertex of the graph
S=Ø // S contain the traversed node initially empty
Initialize(PQ) //PQ is a Priority Queue
120
FOR all u ϵ V[G] DO
EnQueue(PQ, u) // Priority Queue initially contain all the vertices of
graph
WHILE !Empty(PQ) DO // loop executes until Q empty (all vertices traversed)
u = DeQueue(PQ) // Extract the minimum from the priority queue for traversal
S = S Ս {u} // Union traversed vertex with S.
FOR each vertex v ϵAdj[u] DO// loop executes for the all-adjacent vertices of u
Relax(u, v, w) // Relax function update the value of adjacent vertices of u.
END;
Complexity
The complexity of Dijkstra's algorithm is dependent upon the representation of the graph. There
are two methods to represent the graph
Array representation
Adjacency list representation
If we represent a graph using an array, then the time complexity of Dijkstra's algorithm is O(V 2). If
we represent the graph using the Adjacency list, Time Complexity can be reduced to O(E log V) with
the help of binary heap.
Example
Find the shortest path from node 0 to other nodes.
121
Step 2Extract min node that is node 0 and updates weight of adjacent vertices that is 1 and 6 with
respective weight 3 and 6.
Current Node value
0 1 2 3 4 5 6 7 8
Node updated
0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ - -
0 3 ꝏ ꝏ ꝏ ꝏ ꝏ 6 ꝏ 0 1,7
Step 3 Extract min = 1, adjacent vertices = 2, 7. Update only node 2 but not 7 because the existing
cost of 7 is less than the current cost.
Existing weight of node 7 = 6
current weight= weight of node 1 + weight (1, 7)
current weight = 3 + 9 = 12
Node value
0 1 2 3 4 5 6 7 8
Current Node updated
0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ - -
0 3 ꝏ ꝏ ꝏ ꝏ ꝏ 6 ꝏ 0 1,7
3 8 ꝏ ꝏ ꝏ ꝏ 6 ꝏ 1 2
122
Step 4Similarly update the weight of nodes 6 and 8.
Node value
0 1 2 3 4 5 6 7 8
Current Node updated
0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ - -
0 3 ꝏ ꝏ ꝏ ꝏ ꝏ 6 ꝏ 0 1,7
3 8 ꝏ ꝏ ꝏ ꝏ 6 ꝏ 1 2
8 ꝏ ꝏ ꝏ 9 6 12 7 6,8
123
Step 6No update
Node value
0 1 2 3 4 5 6 7 8
Current Node updated
0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ - -
0 3 ꝏ ꝏ ꝏ ꝏ ꝏ 6 ꝏ 0 1,7
3 8 ꝏ ꝏ ꝏ ꝏ 6 ꝏ 1 2
8 ꝏ ꝏ ꝏ 9 6 12 7 6,8
8 16 ꝏ 10 9 12 2 3,5
16 ꝏ 10 9 12 6 -
124
8 ꝏ ꝏ ꝏ 9 6 12 7 6,8
8 16 ꝏ 10 9 12 2 3,5
16 ꝏ 10 9 12 6 -
16 19 10 12 5 4
16 19 12 8 -
125
16 19 10 12 5 4
16 19 12 8 -
16 19 3 -
19 4 -
Example 2
Find the single source shortest path using Dijkstra's Algorithm if the source node is A.
Step 1:
Step 2:
126
Step 3:
Step 4:
Step 5:
Step 6:
127
Introduction to All Pair Shortest Path:
Transitive Closure
Suppose we are at airport x and want to reach another airport, y, z, a, b, etc. If we can reach airport
y from x, there will exist a path between them or say airport y is reachable through airport x.
Similarly, if we can reach airport z through y or through some other airports, then there would be
a path between airport x and y, and we can say airport z is reachable through airport x.
128
Figure: Different airports
Definition
Transitive Closure in a graph shows the path between the nodes. In other words, if there exists a
path between node x and node y, then there should be a corresponding edge(s) between these in
Graph.
The final transitive Closure Ti j (k) of a graph is a Boolean matrix, where if there is a path between
two vertices, it is indicated by 1 in the Matrix, otherwise 0. Here, k is the set of all intermediate
vertices from {1, 2, 3, …, k}.
The recursive definition of transitive Closure of a graph Ti j (k) is given as below:
Note:- Here, we are considering that there should exist a path when the minimum number of nodes
is two unless there exists a self-loop on the node. Let us understand this with some examples of
directed Graph.
129
Transitive Closure (Method 2):
A Reachability Matrix represents the transitive Closure of a directed graph. Reachability Matrix
here means to have a path to reach from a vertex i to another vertex j in a given Graph which is G
= (V, E). The path to reach from Vertex i to j must be between all pairs of Vertices (i, j).
The recursive definition of transitive Closure of a graph Ti j (k) is given as below:
ALGORITHM TransitiveClosure(G)
BEGIN:
n = | V[G] |
FOR i = 1 to n DO
FOR j = 1 to n DO
IF ( (i = j) or ( i, j) ∈ E [G]) THEN
Then Ti j (0) = 1
Else Ti j (0) = 0
FOR k = 1 to n DO // number of nodes
FOR i = 1 to n DO // number of row
130
FOR j = 1 to n DO // number of columns
Ti j (k) = Ti j (k-1) v (Ti k (k-1) ^ T k j (k-1) )
RETURN T n
END;
The above algorithm can be explained by a step by step approach for the given below Graph:
The above T (4) Matrix is the Final Transitive Closure of the above-shown Graph, showing no
131
reachability from Node A to Node B, C, and D after all intermediate steps.
Time Complexity: The time -complexity of the above solution is Θ(n3) as three for loops exist.
Given a Graph G =(V,E), we have to find the shortest path from every vertex u to every vertex v
given in a graph is called All Pair Shortest Path Problem.
Basically, the shortest path from every vertex u to every vertex v can be obtained using the single-
source shortest path algorithms by running the loop |V| times, by making each vertex as a source
once.
If the weights associated with the edges are positive then, we can simply apply Dijkstra’s Algorithm.
If the weights associated with the edges are negative then, Dijkstra’s algorithm will no longer work.
Here, we must run the slower Bellman-Ford Algorithm once from each vertex. The resulting running
time is O (V2E), which on a dense graph will result in O (V4).
Here, we will see the more optimized approach in comparison to Single source shortest path
approaches for computing the shortest path from every vertex u to every vertex v. We will also
explore the relation of all pair shortest path problem to matrix multiplication.
As in a Single Source Shortest Path Algorithms, we assume an adjacency list representation of the
graph. But in all pair shortest path algorithms, we assume the adjacency matrix representation of
the graph.
For a given directed graph G= (V, E), First, we need to prepare the adjacency matrix of a given
graph. Weight adjacency matrix of n* n vertices is prepared for representing the edge weights of
an n vertex directed graph G= (V, E). Weight adjacency matrix is defined as:
0 If i =j
Wij = Weight of the directed edge from i to j If not equal to j and (i, j) belongs to E
132
Negative-weight edges are allowed, but we assume for the time being that the input graph contains
no negative-weight cycles.
Floyd Warshall’s Algorithm is for solving the all-Pairs Shortest Path problem. The problem is to find
the shortest distances between every pair of vertices in a given edge-weighted directed graph.
The strategy followed by Floyd Warshall's algorithm is dynamic programming. The resulting
algorithm, known as the Floyd-Warshall algorithm, runs in Θ (V3) time. As before, negative-weight
edges may be present, but we assume that there are no negative weight cycles.
Step 1: Characterizing the Structure of Shortest Path:
In this, we will use different categorizations to structure the shortest path in comparison to matrix
multiplication based on all pairs algorithms. The algorithm considers the "intermediate" vertices of
the shortest path, where an intermediate vertex of a simple path p = v 1, v2,...,vl is any vertex of p
other than v1 or vl, that is, any vertex in the set {v2, v3,..., vl-1}.
The Floyd-Warshall algorithm exploits a relationship between path p and shortest paths from i to j
with all intermediate vertices in the set {1, 2,..., k - 1}. The relationship depends on whether or not
k is an intermediate vertex of path p. There will be two cases used to define whether k is an
intermediate vertex or not.
Case 1: If K is not an intermediate vertex: If k is not an intermediate vertex of path p, then all
intermediate vertices of path p are in the set {1, 2, ..., k-1}.
Thus, the shortest path from vertex i to vertex j with all intermediate vertices in the set {1, 2, ..., k-
1} is also the shortest path from i to j with all intermediate vertices in the set {1, 2, ..., k}.
Case 2: If K is an intermediate vertex: If k is an intermediate vertex of path p, then we break path
P, from i to j with the help of k. p1 is the shortest path from i to k with all intermediate vertices in
the set {1, 2,..., k}. Because vertex k is not an intermediate vertex of path p1, we see that p1 is a
shortest path from i to k with all intermediate vertices in the set {1, 2,..., k - 1}. Similarly, p2 is a
shortest path from vertex k to vertex j with all intermediate vertices in the set {1, 2, ..., k-1}.
Step 2: A recursive solution to the all-pairs shortest-paths problem:
Let d ijk be the weight of the shortest path from vertex i to vertex j for which all intermediate
vertices are in the set {1, 2,...,k}. When k = 0, a path from vertex i to vertex j with no intermediate
vertex numbered higher than 0 has no intermediate vertices at all. Such a path has at most one
edge, and hence dij0 = wij
A recursive definition is given by
dij(k) = Wij if k = 0
min(dij(k-1), dik(k-1) +dkj(k-1)) if k ≥ 1
133
In this bottom-up procedure is used to compute the shortest path weights for the increasing values
of k. Initially, when k=0, there is no intermediate vertex; simply write the weights associated on the
edges, which may be 0, , and weight on the directed edge.
Once k increases from 0, each vertex one by one will behave as an intermediate vertex and update
the shortest path weights accordingly.
Complexity of the Floyd-Warshall algorithm is computed by the three nesting for loops of lines 3-
6. Each execution of line 6 takes O (1) time. The algorithm thus runs in time θ(n3)
Step 4: Constructing the Shortest Path:
There are a variety of different methods for constructing shortest paths in Floyd-Warshall’s
algorithm. One way is to compute the matrix D of shortest-path weights and then construct the
predecessor matrix Π from the D matrix. This method can be implemented to run in O (n 3) time.
Given the predecessor matrix Π, the PRINT-ALL-PAIRS SHORTEST-PATH procedure can be used to
print the vertices on a given shortest path.
We can compute the predecessor matrix Π "on-line" just as the Floyd-Warshall algorithm computes
the matrices D(k) . Specifically, we compute a sequence of matrices Π(0), Π(1),. , Π(n), where Π =
Π(n) and ΠijK is defined to be the predecessor of vertex j on a shortest path from vertex i with all
intermediate vertices in the set {1, 2,. , k}.
Recursive Formulation:
Case 1: when K =0, there is no intermediate vertex:
(k)
Π ij = Nil If i=j or Wij =
i If i not equal to j and Wij =
134
Case 2: When K is greater than equal to 1, that is, there exists an intermediate vertex.
Πij(K) = Πij(K-1) If d ij(k-1)<= dik(k-1) +dkj(k-1))
Πkj(K-1)If d ij(k-1)>dik(k-1) +dkj(k-1))
Example 1: Apply Floyd Warshall’s Algorithm on the given directed graph. Compute the shortest
path weights DK as well as ΠKpredecessor matrix for the given graph.
Solution 1: First, we will represent the given graph in the form of a matrix. Compute D0 and Π0 using
the recursive formulation to computing the shortest path weights and predecessor node.
Start by Calculating D Matrix: From the given directed graph, First of all, we will prepare the
weighted adjacency matrix highlighting no intermediate vertex between the vertex i and j. Simply
assign weights to the matrix under these three categories: a) If there is a directed edge from vertex
i to j, then simply assign the weights associated with that particular edge. b) If there is no directed
edge from vertex i to j, then simply assign the largest value that is ∞. c) If there is a self-loop, then
simply assign 0.
Preparing π0 from D0: Without Intermediate vertex: There will be two cases:
Assign NIL, where there is no directed edge as well as a self-loop. That is, we simply assign NIL to
all the places in π0matrix where, in D0 matrix there is and 0.
Assign row value i, where there is a directed edge from vertex i to vertex j. Here we simply return
i, as i is the predecessor of vertex j.
Step 1: When K=1 (Node ‘1’ is intermediate node): To calculate shortest path weights:
dij(k) ← min (dij(k-1), dik(k-1) +dkj(k-1))
d12(1) = min(d12(0) , d 11(0) + d 12(0) ) d13(1) = min(d13(0) , d11(0) + d 13(0) )
d12(1) = min ( 3, 0+3) = 3 d13(1) = min ( 8, 0+8) = 8
135
d14(1) = min ( ∞, 0+∞) = ∞ d 15 (1) = min ( -4, 0+(-4)) = -4
If there is the changeinD1 matrix from D0 then we simply calculate the predecessor node of vertex
j with the help of the following condition of recursive formulation:
Πij(K) = Πkj(K-1) If d ij(k-1)>dik(k-1) +dkj(k-1)
136
Step 2: When K=2 (Node ‘2’ is intermediate node)
dij(k) ← min (dij(k-1), dik(k-1) +dkj(k-1))
d12(2)= min(d12(1) , d12(1) + d 21(1) ) d13(2) = min(d13(1) , d12(1) + d 23(1) )
d12(2) = min ( 3, 3+ ∞) = 3 d 13 (2) = min ( 8, 2+∞) = 8
If there is the changeinD2 matrix from D1 then we simply calculate the predecessor node of vertex
j with the help of following condition of recursive formulation:
Πij(K)=Πkj(K-1) If d ij(k-1)>dik(k-1) +dkj(k-1))
137
Step 3: When K=3 (Node ‘3’ is intermediate node)
dij(k) ← min (dij(k-1), dik(k-1) +dkj(k-1))
d12(3)= min(d12(2) , d13(2) + d 32(2) ) d13(3) = min(d13(2) , d13(2) + d 33(2) )
d12(3) = min ( 3, 8+ 4) = 3 d 13(3) = min ( 8, 8+0) = 8
138
There will be two cases:
a) Simply copy all the values of π2 to π3 where there is no change in D3 matrix from D2. Basically,
this will satisfy that condition of recursive formulation in which
Πij(K)=Πij(K-1) If d ij(k-1)<= dik(k-1) +dkj(k-1))
b) If there is the changeinD3 matrix from D2 then we simply calculate the predecessor node of
vertex j with the help of following condition of recursive formulation:
Πij(K)=Πkj(K-1) If d ij(k-1)>dik(k-1) +dkj(k-1))
139
d53(4) = min(d53(3) , d 54(3) + d 43(3) ) d54(4) = min(d54(3) , d54(3) + d 44(3) )
d53(4) = min ( ∞, 6+(-5)) = 1 d54(4) = min ( 6, 6+0) = 6
140
Step 1: Start by Calculating D Matrix: From the given directed graph, First of all, we will prepare the
weighted adjacency matrix highlighting no intermediate vertex between the vertex i and j. Simply
assign weights to the matrix under these three categories: a) If there is a directed edge from vertex
i to j, then simply assign the weights associated with that particular edge. b) If there is no directed
edge from vertex i to j, then simply assign the largest value that is ∞. c) If there is a self-loop, then
simply assign 0.
If the copied column contains any value as then simply copy the corresponding row from D0 to
D1. This is because of the recursive formulation derived to compute the shortest path weights
between any vertices with the help of the Intermediate vertex. Here, dijcontains values as .Here,
we simply have dijk=dijk-1(minimum value)from the recursive formulation dijk = min (dijk-1, dikk-1 + dkjk-
1).
Simply compute remaining values from the recursive formulation: dijk = min (dijk-1, dikk-1 + dkjk-1).
141
Step 3: Preparing D2 from D1 Matrix: (2 is behaving as an Intermediate from vertex i to j):
If the copied row contains any value as then simply copy the corresponding column from D1 to
D2. This is because of the recursive formulation derived to compute the shortest path weights
between any vertices with the help of the Intermediate vertex. Here, dkj contains values as .Here,
we simply have dijk=dijk-1(minimum value)from the recursive formulation d ijk = min (dijk-1, d ikk-1 + dkjk-
1).
If the copied column contains any value as then simply copy the corresponding row from D1 to
D2. This is because of the recursive formulation derived to compute the shortest path weights
between any vertices with the help of the Intermediate vertex. Here, dijcontains values as .Here,
we simply have dijk=dijk-1(minimum value)from the recursive formulation d ijk = min (dijk-1, d ikk-1 + dkjk-
1).
Simply compute remaining values from the recursive formulation: dijk = min (dijk-1, d ikk-1 + dkjk-1).
Step 4: Preparing D3 from D2 Matrix: (3 are behaving as an Intermediate from vertex i to j):
142
In this, category there may be four possible cases:
Here, Intermediate Vertex 3 is either behaving as the source and destination vertex at the following
positions such as (3,1)(3,2)(3,3)(3,4)(3,5)(1,3)(2,3)(4,3)(5,3). Hence, we simply copy all the values
of D3 in D2 at these positions. Here, we simply have dijk=dijk-1(minimum value)from the recursive
formulation dijk = min (dijk-1, d ikk-1 + dkjk-1).
If the copied row contains any value as then simply copy the corresponding column from D3 to
D2. This is because of the recursive formulation derived to compute the shortest path weights
between any vertices with the help of the Intermediate vertex. Here, dkj contains values as .Here,
we simply have dijk=dijk-1 (minimum value) from the recursive formulation dijk = min (dijk-1, dik k-1 +
dkjk-1).
If the copied column contains any value as then simply copy the corresponding row from D3 to
D2. This is because of the recursive formulation derived to compute the shortest path weights
between any vertices with the help of the Intermediate vertex. Here, dij contains values as .Here,
we simply have dijk=dijk-1(minimum value)from the recursive formulation d ijk = min (dijk-1, d ikk-1 + dkjk-
1).
Simply compute remaining values from the recursive formulation: dijk = min (dijk-1, d ikk-1 + dkjk-1).
Step 5: Preparing D4 from D3 Matrix: (4 are behaving as an Intermediate from vertex i to j):
In this, category there may be four possible cases:
Here, Intermediate Vertex 4 is either behaving as a source and destination vertex at the following
positions such as (4,1)(4,2)(4,3)(4,4)(4,5)(1,4)(2,4)(3,4)(5,4). Hence, we simply copy all the values
of D4 in D3 at these positions. Here, we simply have dijk=dijk-1(minimum value) from the recursive
formulation dijk = min (dijk-1, d ikk-1 + dkjk-1).
If the copied row contains any value as then simply copy the corresponding column from D4 to
D3. This is because of the recursive formulation derived to compute the shortest path weights
between any vertices with the help of the Intermediate vertex. Here, dkjcontains values as .Here,
we simply have dijk=dijk-1(minimum value)from the recursive formulation dijk = min (dijk-1, dikk-1 + dkjk-
1).
If the copied column contains any value as then simply copy the corresponding row from D4 to
D3. This is because of the recursive formulation derived to compute the shortest path weights
143
between any vertices with the help of the Intermediate vertex. Here, dijcontains values as .Here,
we simply have dijk=dijk-1(minimum value) from the recursive formulation dijk = min (dijk-1, d ikk-1 + dkjk-
1).
Simply compute remaining values from the recursive formulation:dijk = min (dijk-1, d ikk-1 + dkjk-1).
Step 6: Preparing D5 from D4 Matrix: (5 are behaving as an Intermediate from vertex i to j):
In this, category there may be four possible cases:
Here, Intermediate Vertex 5 is either behaving as a source and destination vertex at the following
positions such as (5,1)(5,2)(5,3)(5,4)(5,5)(1,5)(2,5)(3,5)(4,5). Hence, we simply copy all the values
of D5 in D4 at these positions. Here, we simply have dij k=dijk-1(minimum value) from the recursive
formulation dijk = min (dijk-1, d ikk-1 + dkjk-1).
If the copied row contains any value as then simply copy the corresponding column from D5 to
D4. This is because of the recursive formulation derived to compute the shortest path weights
between any vertices with the help of the Intermediate vertex. Here, dkj contains values as .Here,
we simply have dijk=dijk-1 (minimum value) from the recursive formulation dijk = min (dijk-1, dik k-1 +
dkjk-1).
If the copied column contains any value as then simply copy the corresponding row from D5 to
D4. This is because of the recursive formulation derived to compute the shortest path weights
between any vertices with the help of the Intermediate vertex. Here, dij contains values as .Here,
we simply have dijk=dijk-1(minimum value) from the recursive formulation dijk = min (dijk-1, dikk-1 + dkjk-
1).
Simply compute remaining values from the recursive formulation: dijk = min (dijk-1, dikk-1 + dkjk-1).
144
Now we will calculate the predecessor matrix π from the distance matrix D:
Step 1: Preparing π0 from D0: Without Intermediate vertex
There will be two cases:
Assign NIL, where there is no directed edge as well as a self-loop. That is we simply assign NIL to all
the places in π0matrix where, in D0 matrix there is and 0.
Assign row value i, where there is a directed edge from vertex i to vertex j. Here we simply return
i, as i is the predecessor of vertex j.
145
Step 4: Preparing π3 from D3: 3 as Intermediate vertex
There will be two cases:
Simply copy all the values of π2 to π3 where there is no change in D3 matrix from D2. Basically, this
will satisfy that condition of recursive formulation in which
Πij(K)=Πij(K-1) If d ij(k-1)<= dik(k-1) +dkj(k-1))
If there is the changeinD3 matrix from D2 then we simply calculate the predecessor node of vertex
j with help of following condition of recursive formulation:
Πij(K)=Πkj(K-1) If d ij(k-1)>dik(k-1) +dkj(k-1))
Step 5: Preparing π4 from D4: 4 as Intermediate vertex: There will be two cases:
Simply copy all the values of π3 to π4 where there is no change in D4 matrix from D3. Basically, this
will satisfy that condition of recursive formulation in which
Πij(K)=Πij(K-1) If d ij(k-1)<= dik(k-1) +dkj(k-1))
If there is the changeinD4 matrix from D3 then we simply calculate the predecessor node of vertex
j with help of following condition of recursive formulation:
Πij(K)=Πkj(K-1) If d ij(k-1)>dik(k-1) +dkj(k-1))
Step 6: Preparing π5 from D5: 5 as Intermediate vertex: There will be two cases:
Simply copy all the values of π4 to π5 where there is no change in D5 matrix from D4. Basically, this
will satisfy that condition of recursive formulation in which
Πij(K)=Πij(K-1) If d ij(k-1)<= dik(k-1) +dkj(k-1))
If there is the changeinD5 matrix from D4 then we simply calculate the predecessor node of vertex
j with help of following condition of recursive formulation:
146
Πij(K)=Πkj(K-1) If d ij(k-1)>dik(k-1) +dkj(k-1))
Example 2: Apply Floyd Warshall’s Algorithm to compute the shortest path. Compute Dn Matrix
Solution 2: Step 1: Start by Calculating D Matrix: From the given directed graph, First of all, we will
prepare the weighted adjacency matrix highlighting no intermediate vertex between the vertex i
and j. Simply assign weights to the matrix under these three categories: a) If there is a directed
edge from vertex i to j, then simply assign the weights associated with that particular edge. b) If
there is no directed edge from vertex i to j, then simply assign the largest value, that is ∞. c) If there
is a self-loop, then simply assign 0.
147
we simply have dijk=dijk-1(minimum value) from the recursive formulation dijk = min (dijk-1, d ikk-1 + dkjk-
1).
If the copied column contains any value as then simply copy the corresponding row from D0 to
D1. This is because of the recursive formulation derived to compute the shortest path weights
between any vertices with the help of the Intermediate vertex. Here, dij contains values as .Here,
we simply have dijk=dijk-1(minimum value) from the recursive formulation dijk = min (dijk-1, d ikk-1 + dkjk-
1).
Simply compute remaining values from the recursive formulation: dijk = min (dijk-1, d ikk-1 + dkjk-1).
Step 3: Keep on repeating the same process until we traversed the entire Intermediate vertices:
148
149