0% found this document useful (0 votes)
8 views84 pages

Unit-5 - Graphs Feb 2024

The document provides an overview of graphs, defining them as non-linear data structures consisting of vertices and edges, and discusses various types of graphs such as directed, undirected, cyclic, and acyclic graphs. It also highlights the applications of graphs in real-world scenarios like Google Maps and social networks, and explains basic terminology related to graphs, including paths and degrees of vertices. Additionally, the document covers graph representation methods and primitive operations for manipulating graphs.

Uploaded by

protocolpsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views84 pages

Unit-5 - Graphs Feb 2024

The document provides an overview of graphs, defining them as non-linear data structures consisting of vertices and edges, and discusses various types of graphs such as directed, undirected, cyclic, and acyclic graphs. It also highlights the applications of graphs in real-world scenarios like Google Maps and social networks, and explains basic terminology related to graphs, including paths and degrees of vertices. Additionally, the document covers graph representation methods and primitive operations for manipulating graphs.

Uploaded by

protocolpsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

4.

Graph:

4.1.1 Introduction to 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.

Figure 4.1: Social Graph (e.g., Facebook)

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)

4.1.2. Definition of Graph:

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)

A graph is defined as an ordered pair G = {V, E}, where


V = Set of vertices and
E = Set of edges.

Figure 4.3: Example of Graph

For the above Graph


V = {A, B, C, D} and
E = {[A, B], [A, C], [B, D], [C, D]}

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.

4.1.3. Graph: Basic Terminology

1. Degree of vertex
The degree of a vertex is the number of edges connecting it.
Notation – degree (V)

Degree of Vertex in a Directed Graph

Figure 4.10 Undirected Graph

Table 1 Degree of undirected Graph for Example 1


Vertex Degree
a 2
b 3
c 1
d 2

3
e 0

Degree of Vertex in a Directed Graph


In a directed graph, each vertex has an in-degree and an out-degree.
In-degree of vertex V is the number of edges that are coming into the vertex V and Out-
degree of vertex V is the number of edges that are going out from the vertex V.

In Degree Notation: deg−(V).


Out Degree Notation: deg+(V).

Consider the following example.

Vertex In-degree Out-degree


a 1 2
b 2 0
c 2 1
d 1 1
e 1 1
f 1 1
g 0 2

Figure 4.11. Directed Graph (Example 2)

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

Vertices adjacent to node 2: 1 and 4


Vertices adjacent to node 4: 2, 3 and 6

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

Figure 4.13. Undirected Graph

Possible Path from vertex a to f: a-d-c-f and a-b-c-f


Possible Path from vertex d to e: d-c-e
Example: Directed Graph

Figure 4.14. Directed Graph

Path from vertex 0 to 7: 0-3-5-6-7 and 0-3-4-6-7

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

Path a-g-f-e-c-f-d-c-b: neither simple nor closed


Path a-g-f-e-c-b: simple Path
Path a-g-f-e-c-f-d-a: closed Path

4.1.4. Types of Graph:

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.

Figure 4.16: Null Graph

Trivial Graph

Graph having only a single vertex and no edges, and it is the smallest possible Graph.

Figure 4.17. Trivial 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."

Figure 4.18: Examples of “2-Regular Graphs”

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.

Figure 4.19. Connected Graph

Disconnected Graph

The Graph in which at least one node is not reachable from a node is a disconnected graph.

Figure 4.20. 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.

Figure 4.21. Examples of Complete Graph


Cycle Graph

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.

Figure 4.22: Three Examples of Cycle Graph

Cyclic Graph

A graph containing at least one cycle is known a cyclic graph.

Figure 4.23. Cyclic Graph


This Graph in Figure 15 contains two cycles. Therefore, it is a cyclic graph.

8
Acyclic Graph

A graph not containing any cycle in it is called an acyclic graph.

Figure 4.24. 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

Figure 4.25. Multi Graph

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.

Figure4.27: (a) Weighted undirected Graph (b) Weighted directed 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.

Figure 4.28 Bi-Partite Graph

Set 1 (vertices): {a, b} Set 2 (vertices): {c, d, e}

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.

Figure: 4.30 (a). Hamiltonian Graph 19(b.) Non-Hamiltonian Graph


Figure 19(a) graph contains a cycle a-b-c-f-d-e-a that visits each vertex exactly once. Therefore, the
cycle is a Hamiltonian cycle. It implies that the Graph is a Hamiltonian Graph.
A cycle in Figure 4.30(b) graph visits all vertices, a-b-d-c-f-d-e-a but vertex d appears twice in the
cycle. Thus, the Graph does not contain a Hamiltonian cycle. It implies that the Graph is a Non-
Hamiltonian Graph.

4.1.5. Similarity and difference between Tree and Graph:

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).

Figure 4.5: - Google map

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.

Figure 4.6: - Facebook as Graph

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.

Figure 4.7: - www as graph

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)

4.1.7. Graph Representation:

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.

1 if a single edge exists between vertex i to vertex j (adjacent node)


2 if self-loop exists (In case of multigraph)
v[i][j] n if number of parallel edges is n
w if assigned weight is w (weighted Graph)
0 otherwise

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).

True or 1 if edge exist between I to j


v[i][j]
False or 0 otherwise

Example (Directed Graph)

Figure 4.31: Graph Adjacency matrix Representation

Figure 4.32: Graph Adjacency matrix Representation

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

Example (Undirected Graph)

Figure: 4.33 Undirected Graph

Figure 4.34: Graph Adjacency matrix Representation

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)

Figure 4.35: Graph Adjacency matrix Representation

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.

Example (Weighted Undirected Graph)

Figure 4.36: Graph Adjacency matrix Representation

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.

Example (Multi Graph)

Figure 4.37 : Graph Adjacency matrix Representation

If self-loop exists then its count is 2 because of following reason-


A(source)→A (destination or sink) + A (destination or sink) →A(source)=1+1=2

16
If multiple edge exists then it counts 2 and if single edge exists then it counts only 1.

4.1.8. Graph primitive Operations

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.

Figure 4.38 : Graph operations

e[4][4]={ {0 ,1 ,0, 1},

{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)

// name as Input and returns it’s unique no equivalent

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)

// Returns true if two vertices are adjacent.


BEGIN:

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)

// Displays all adjacent vertices for the given vertex


BEGIN:
i=uniqueNumber(v)
IF i == –1 THEN
RETURN
FOR J = 0 TO N DO
IF e[i][j] != 0 THEN
WRITE(v[j])
END;

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.

Figure 4.39 : Adjacency Matrix


A2AC = 2, so there is two paths of length 2 from A to C.
First Path = A->B->C

Second Path= A->D->C

Path Matrix-

Path matrix is defined as of order V x V, where V is the number of vertex defined as-

1 if there is a path from Vi to Vj

A[i][j] =

0 otherwise

Figure 4.40 : Path Matrix

Steps- 1- Find Adjacency matrix A of given graph.

Step2- BN=A + A2 + … +AN

Step3- Path matrix is defined as=

1 for all non zero value of in BN

19
P[i][j]=

0 otherwise

Figure 4.41 : Explanation of Path MAtrix

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-

1 if the jth edge Ej is incident on ith vertex Vi


Im[i][j] =

0 otherwise

Figure 4.42. : Graph Incidence Matrix

2-Adjacency List Representation to be formatted

20
Adjacency list defined as – A link list for each adjacent node of the given vertex.

The node contains the data field and next field.


Data Next
Another node named vertex contains data field and node field.
Data Node Head

ALGORITHM Insert (v[ ], n) // Algorithm performs Insertion operation in a graph


BEGIN:
FOR i=1 TO n DO
v[i].data=character //read character
FOR j = 1 TO m //read m for adjacent node for each node
IF v[i].HEAD==NULL
v[i].HEAD=P
P→data=item
P→next=NULL
RETURN P
ELSE
Q=v[i].HEAD
WHILE Q→next != NULL
Q=Q→next
P→data=item
P→next=NULL
Q→next=P
RETURN v[i].HEAD
END;

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 performs deletion operation on graph


BEGIN:
FOR i=1 TO n DO
FOR j=1 TO m
P= v[i].HEAD
WHILE P!=NULL
Q=P
P=P→next
Q→next=NULL
FREE(P)
P=NULL
END;

ALGORITHM isAdjacent(ver1,ver2)

//Algorithms returns true if two vertices are adjacent


BEGIN:
i=uniqueNumber(ver1)
j=uniqueNumber(ver2)
IF i ==-1 || j==-1 THEN
RETURN false
START= v[i].HEAD
WHILE START !=NULL DO
IF START→DATA==j THEN
RETURN true
START=START→next
RETURN false
END;

ALGORITHM displayAdjacentNode(ver)

// Algorithm displays all adjacent vertices


BEGIN:
i=uniqueNumber(ver)
IF i == – 1 THEN
RETURN
START= v[i].HEAD
WHILE START !=NULL DO
22
WRITE V[START]→data
START=START→next
END;
Complexity- This algorithm takes O (n x v), where n is the number of vertices and v is the number
of an adjacent node.

Map vertex into hash table


In the adjacency list representation of Graph, assign index number as the unique number for each
vertex for convenience. In real life vertex name is not represented by single character. Take the
example of city names. For the storage or implementation point of view big names are not suitable.
To get rid of this problem, convert the vertex name into numbers and map it into the hash table.

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}

Figure 4.43: Representation of Graph

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.

4.2.1. Breadth-First Search (BFS)


The general idea behind a breadth first search begin at a starting note A is as follows. first we
examine the starting note A. then we examine all the neighbors of A. Then we examine all the
neighbors of the neighbors of A and so on. Naturally we need to keep track of the neighbors
of a node, and we need to guarantee that no node is processed more than once. This is
accomplished by using a queue to hold nodes that are waiting to be processed, and by using
a field STATUS which tell us the current status of any node. The algorithm follows.

This algorithm executes a BFS on a graph G beginning at a starting node A.


1. Initialize all nodes to the ready status (STATUS = 1)
2. Put the starting node A in QUEUE and change its status to the waiting state (STATUS 2)
3. Repeat steps 4 and 5 until QUEUE is empty:
4. Remove the front node N of QUEUE, process N and change the status of N to the
processed state (STATUS = 3)
5. Add to the rear of QUEUE all the neighbors of N that are in the ready state (STATE = 1),
and change their status to the waiting state (STATUS = 2).
6. EXIT

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

a. Initially, add A to Q as follows


FRONT = 1 Q: A
REAR = 1 BFS :

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

We stop as soon as J is added to Q. Since J is our final destination. We now backtrack


from J, using the array ORIG to find the path P. Thus

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).

Depth First Search


Algorithm : this algorithm executes a depth first search on a graph G beginning at a starting node A
1. Initialize all nodes to the ready state (STATUS = 1).
2. Push the starting node A onto STACK and changes its status to the waiting state (STATUS = 2).
3. Repeat steps 4 and 5 until STACK is empty.
4. Pop the top node N of STACK . process N and change its status to the processed state (STATUS = 3)
5. Push onto STACK all the neighbors of N that are still in the ready state (STATUS = 1), and change
their status to the waiting state (STATUS = 2)
6. EXIT

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

a. Initially push A onto the stack


Nodes A B C D E F G J K
Status 2 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

So, finally stack is empty and the final DFS is – A, B, G, E, J, K, D, C, F

Complexity of DFS Algorithm


Time Complexity: In DFS we traverse each node exactly once, so the running time of DFS will
be O(V)as there are total V nodes in the Graph but it does not include the time of exploring
adjacent nodes of each called vertex.

Therefore, an additional complexity comes is also calculated, which depends on how to


discover all the outgoing edges for each node which depends on the way the Graph is
implemented.

4.2.2. Coding Questions on Graph Traversal

Depth First Search (DFS) Iterative Implementation.


Depth First Search (DFS) Recursive Implementation.
Generate list of possible words from a character matrix.
Find all occurrences of the given string in a character matrix.
Find path from source to destination in a matrix that satisfies given constraints

29
Spanning Tree:

Introduction to 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.

Suppose a incomplete Graph

31
The Adjacency Matrix for the given
incomplete graph

The co-factor for (1, 1) of the resultant


matrix after running STEP 2 and STEP 3 is
eight.

Hence total number of spanning trees =


eight

4.4.2 Minimum Spanning tree:

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.

Sort all the edges from low weight to high weight.


Take the edge with the lowest weight and use it to connect the vertices of the graph. If adding an
edge creates a cycle, reject that edge and go for the next least weight edge.

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

Kruskal's algorithm (Step by step execution)

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 Line No - 1 on a E’ = ∅


given graph
Initialize set A as an empty set. E’ = ∅ or E’={ }

Applying and executing


FOR each vertex v ∈ V [G] DO
Line No - 2, 3, on given
Make_Set(v)
graph
|V| trees each containing {a} {b} {c} {d} {e} {f} {g} {h} {i
one vertex }

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 }

for the edge (f, g), u = f and v = g


(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
f and g belongs to different set, edge (f, g) is included in A, union operation is performed
A = { (g, h), (c, i) , (f, g) }

{a} {b} { c, i } {d} {e} { f, g, h }

for the edge (a, b), u = a and v = b


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
a and b belongs to different set, edge (a, b) is included in A, union operation is performed

A = { (g, h), (c, i), (f, g), (a, b) }

{ 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 = { (g, h), (c, i), (f, g), (a, b), (c, f) }

{ a, b } {d} {e} { c, f, g, h, i }

for the edge (g, i), u = g and v = i


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
g and i 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) }
{ a, b } {d} {e} { c, f, g, h, i }

for the edge (c, d), u = c and v = d


(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) ) ) ) ) ) ) ) ) )

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 }

for the edge (h, i), u = h and v = i


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
h and i 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, b } {e} { c, d, f, g, h, i }

for the edge (a, h), u = a and v = h


(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
a and h belongs to different set, edge (a, h) is included in A, union operation is performed
A = { (g, h), (c, i), (f, g), (a, b), (c, f), (c, d), (a, h) }

{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 }

for the edge (d, e), u = d and v = e


(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
d and e belongs to different set, edge (a, h) is included in A, union operation is performed
A = { (g, h), (c, i), (f, g), (a, b), (c, f), (c, d), (a, h), (d, e) }

{ a, b, c, d, e, f, g, h, i }

for th e all edge remainin g {(e, f), (b, h), (d, f) },


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
All given edge are not safe. They are not included in set A, also no union operation.
A = { (g, h), (c, i), (f, g), (a, b), (c, f), (c, d), (a, h), (d, e) }

85
{ a, b, c, d, e, f, g, h, i }

4.4.6 Prim’s Algorithm

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

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 is not in Priority
X 28 ꝏ ꝏ ꝏ 10 ꝏ 6 1 NA 5 Queue
5 6,5 = 25

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

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 is not in Priority
X 28 ꝏ ꝏ ꝏ 10 ꝏ 6 1 NA 5 Queue
5 6,5 = 25
4 5,4 = 22
X 28 ꝏ ꝏ 25 X ꝏ 5 4
7 5,7 = 24
X 28 ꝏ 22 X X 24 4 3 4,3 = 12 3

92
5 is not in Priority
5 NA Queue
7 4,7 = 18

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 is not in Priority
X 28 ꝏ ꝏ ꝏ 10 ꝏ 6 1 NA 5 Queue
5 6,5=25
4 5,4=22
X 28 ꝏ ꝏ 25 X ꝏ 5 4
7 5,7=24
3 4,3=12
5 is not in Priority
X 28 ꝏ 22 X X 24 4 3
5 NA Queue
7 4,7=18
2 3,2=16
X 28 12 X X X 18 3 2 4 is not in Priority
4 NA Queue

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

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
X 16 X X X X 18 2 1 NA 7 1 is not in Priority Queue

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

No updation is required because Q = Ø.


Total cost of the given spanning tree is = 10 + 25 + 22 + 12 + 16 + 14
= 99

Example 2:
Find the minimum spanning tree for the given graph.

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

extracted adjacent selected


1 2 3 4 5
vertex vertex edges vertex
2 1,2=1
0 ꝏ ꝏ ꝏ ꝏ 1 2
3 1,3=6

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

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

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

A possible minimum spanning tree is

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:

Single source shortest path


Find a path between one node to all other nodes.
Dijkstra Algorithm
Bellman-Ford Algorithm
All Pairs Shortest Path
Find a path between all pairs of nodes. All nodes are considered as a source node and destination
node.

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 have different path with different cost


A→B→C→D COST= 10 + 2 + 9 = 21
A→B→E→D COST= 10 + 4 + 2 = 16
A → B → E → C → D COST= 10 + 4 + 8 + 9 = 31
A → E → B → C → D COST= 3 + 1 + 2 + 9 = 15
A→E→C→D COST= 3 + 8 + 9 = 20
A→E→D COST= 3 + 2 = 5

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

Undirected graph or directed graph


Non-negative weights
Bellman-ford Algorithm
Directed graph
Non-negative weights are allowed
Both the algorithms initialize the single source and update the weight. To initialize the source node

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;

4.2.4. Dijkstra’s Algorithm

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.

Step 1 weight of Source node = 0


Weight of other nodes = ꝏ
Current Node value
0 1 2 3 4 5 6 7 8
Node updated
0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ - -

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

Step 5 Similarly update the weight of nodes 3 and 5.


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

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 -

Step 7Similarly update weight of node 4


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 -
16 19 10 12 5 4

Step 8No 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

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 -

Step 9No 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 -
16 19 10 12 5 4
16 19 12 8 -
16 19 3 -

Step 10 Last Node Traversed


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 -

125
16 19 10 12 5 4
16 19 12 8 -
16 19 3 -
19 4 -

Step 11Final shortest path between source node to other nodes.

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:

The Algorithm for the above equation is as follows:

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.

All Pair Shortest Path Problem: Floyd Warshall

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

 If not equal to j and (i, j) does not 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:

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

Step 3: Computing the shortest path weights in a bottom-up fashion:

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.

ALGORITHM FloydWarshall (W[ ][ ])


BEGIN:
1. n = rows [W]
2. D0 = W
3. FOR k = 1 TO n DO
4. FOR i = 1 to n DO
5. FOR j = 1 to n DO
6. dij(k) = min (dij(k-1), d ik(k-1) +dkj(k-1))
7. RETURN D(n)
END;
Complexity Analysis of Floyd Warshall Algorithm:
1. n = rows [W] ........................................................................................... O(1)
2. D0 = W..................................................................................................... O(1)
3. FOR k = 1 TO n DO .................................................................................. O(n)
4. FOR i = 1 to n DO................................................................................. O(n)
5. FOR j = 1 to n DO ........................................................................... O(n)
6. dij(k) = min (dij(k-1), d ik(k-1) +dkj(k-1)) .............................................. O(1)
7. RETURN D(n) ........................................................................................................................................ O(1)

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

d14(1) = min(d14(0) , d11(0) + d14(0) ) d15(1) = min(d15(0) , d11(0) + d15(0) )

135
d14(1) = min ( ∞, 0+∞) = ∞ d 15 (1) = min ( -4, 0+(-4)) = -4

d21(1) = min(d21(0) , d21(0) + d 11(0) ) d23(1) = min(d23(0) , d21(0) + d 13(0) )


d21(1) = min (∞ , ∞ + 0) = ∞ d23 (1) = min ( ∞, ∞+8) = ∞

d24(1) = min(d24(0) , d21(0) + d 14(0) ) d25(1) = min(d25(0) , d21(0) + d 15(0) )


d24(1) = min (1, ∞+∞) = 1 d25(1) = min ( 7, ∞+(-4)) = 7

d31(1) = min(d31(0) , d31(0) + d 11(0) ) d32(1) = min(d32(0) , d31(0) + d 12(0) )


d31(1) = min (∞ , ∞ + 0) = ∞ d 32 (1) = min ( 4, ∞+3) = 4

d34(1) = min(d34(0) , d31(0) + d 14(0) ) d35(1) = min(d35(0) , d31(0) + d 15(0) )


d34(1) = min (-5, ∞+∞) = -5 d35(1) = min (∞ , ∞+(-4)) = ∞

d41(1) = min(d41(0) , d41(0) + d 11(0) ) d42(1) = min(d42(0) , d41(0) + d 12(0) )


d41(1) = min (2 , 2 + 0) = 2 d42(1) = min (∞ , 2 +3) = 5

d43(1) = min(d43(0) , d 41(0) + d 13(0) ) d45(1) = min(d45(0) , d41(0) + d 15(0) )


d43(1) = min ( -5, 2+8) = -5 d45(1) = min (∞ , 2+(-4)) = -2

d51(1) = min(d51(0) , d51(0) + d 11(0) ) d52(1) = min(d52(0) , d51(0) + d 12(0) )


d51(1) = min (∞, ∞ + 0) = ∞ d 52 (1) = min (∞ , ∞ +3) = ∞

d53(1) = min(d53(0) , d51(0) + d 13(0) ) d54(1) = min(d54(0) , d51(0) + d 14(0) )


d53(1) = min ( ∞, ∞ +8) = ∞ d 54 (1) = min (6 , ∞+(-4)) = 6

Preparing π1 from D1: 1 as Intermediate vertex: There will be two cases:


Simply copy all the values of π0 to π1 where there is no change in D1 matrix from D0. 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 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

d14(2) = min(d14(1) , d 12(1) + d 24(1) ) d15(2) = min(d15(1) , d12(1) + d 25(1) )


d14(2) = min ( ∞, 3+1) = 4 d15(2) = min ( -4, 3+7) = -4

d21(2)= min(d21(1) , d22(1) + d 21(1) ) d23(2) = min(d23(1) , d22(1) + d 23(1) )


d21(2) = min ( ∞, 0+ ∞) = ∞ d 23 (2) = min ( ∞, 0+∞) = ∞

d24(2) = min(d24(1) , d22(1) + d 24(1) ) d25(2) = min(d25(1) , d22(1) + d 25(1) )


d24(2) = min ( 1, 0+1) = 1 d 25(2) = min ( 7 , 0+7) = 7

d31(2)= min(d31(1) , d32(1) + d 21(1) ) d32(2) = min(d32(1) , d32(1) + d 22(1) )


d31(2) = min ( ∞, 4+ ∞) = ∞ d 32 (2) = min ( 4, 4+0) = 4

d34(2) = min(d34(1) , d 32(1) + d 24(1) ) d35(2) = min(d35(1) , d32(1) + d 25(1) )


d34(2) = min (∞, 4+1) = 5 d35 (2) = min (∞ , 4+7) = 11

d41(2)= min(d41(1) , d42(1) + d 21(1) ) d42(2) = min(d42(1) , d42(1) + d 22(1) )


d41(2) = min ( 2, 5+ ∞) = 2 d42(2) = min ( 5, 5+0) = 5

d43(2) = min(d43(1) , d42(1) + d 23(1) ) d45(2) = min(d45(1) , d42(1) + d 25(1) )


d43(2) = min ( 10, 5+∞) = 10 d45 (2) = min (-2 , 5+7) = -2

d51(2)= min(d51(1) , d52(1) + d 21(1) ) d52(2) = min(d52(1) , d52(1) + d 22(1) )


d51(2) = min ( ∞, ∞+ ∞) = ∞ d 52 (2) = min ( ∞, ∞+0) = ∞

d53(2) = min(d53(1) , d52(1) + d 23(1) ) d54(2) = min(d54(1) , d52(1) + d 24(1) )


d53(2) = min ( ∞, ∞+∞) = ∞ d54(2) = min ( 6 , ∞+1) = 6

Preparing π2 from D2: 2 as Intermediate vertex: There will be two cases:


Simply copy all the values of π1to π2where there is no change in D2 matrix from D1. 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 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

d14(3) = min(d14(2) , d13(2) + d 34(2) ) d15(3) = min(d15(2) , d13(2) + d 35(2) )


d14(3) = min ( 4, 8+11) = 4 d15(3) = min ( -4, 8+11) = -4

d21(3)= min(d21(2) , d23(2) + d 31(2) ) d23(3) = min(d23(2) , d23(2) + d 33(2) )


d21(3) = min ( ∞, ∞+ ∞) = ∞ d 23(3) = min ( ∞, ∞+0) = ∞

d24(3) = min(d24(2) , d23(2) + d 34(2) ) d25(3) = min(d25(2) , d23(2) + d 35(2) )


d24(3) = min ( 1, ∞+(-5)) = 1 d25(3) = min ( 7, ∞+11) = 7

d31(3)= min(d31(2) , d33(2) + d 31(2) ) d32(3) = min(d32(2) , d33(2) + d 32(2) )


d31(3) = min ( ∞, 0+ ∞) = ∞ d32(3) = min ( 4, 0+4) = 4

d34(3) = min(d34(2) , d33(2) + d 34(2) ) d35(3) = min(d35(2) , d33(2) + d 35(2) )


d34(3) = min ( -5, 0+(-5)) = -5 d35(3) = min ( 11, 0+11) = 11

d41(3)= min(d41(2) , d43(2) + d 31(2) ) d42(3) = min(d42(2) , d43(2) + d32(2) )


d41(3) = min ( 2, 10+ ∞) = 2 d42(3) = min ( 5, -5 +4) = -1

d43(3) = min(d43(2) , d43(2) + d 33(2) ) d45(3) = min(d45(2) , d43(2) + d 35(2) )


d43(3) = min ( 10, 10+ 0) = 10 d45(3) = min ( -2, 10+11) = -2

d51(3)= min(d51(2) , d53(2) + d 31(2) ) d52(3) = min(d52(2) , d53(2) + d 32(2) )


d51(3) = min ( ∞, ∞+ ∞) = ∞ d 52(3) = min ( ∞, ∞+4) = ∞

d53(3) = min(d53(2) , d53(2) + d 33(2) ) d54(3) = min(d54(2) , d53(2) + d 34(2) )


d53(3) = min ( ∞, ∞+ 0) = ∞ d54(3) = min ( 6, ∞+(-5)) = 6

Preparing π3 from D3: 3 as Intermediate vertex

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))

Step 4: When K=4 (Node ‘4’ is intermediate node)


d12(4)= min(d12(3) , d14(3) + d 42(3) ) d13(4) = min(d13(3) , d14(3) + d 43(3) )
d12(4) = min ( 3, 3+ 5) = 3 d13(4) = min ( 8, 4+(-5)) = -1

d14(4) = min(d14(3) , d 14(3) + d 44(3) ) d15(4) = min(d15(3) , d14(3) + d 45(3) )


d14(4) = min ( 3, 3+ 0) = 3 d15(4) = min ( -4, 3+(-2)) = -4

d21(4) = min(d21(3) , d 24(3) + d 41(3) ) d23(4) = min(d23(3) , d24(3) + d 43(3) )


d21(4) = min ( ∞, 1+ 2) = 3 d 23 (4) = min ( ∞, 1+(-5)) = -4

d24(4) = min(d24(3) , d 24(3) + d 44(3) ) d25(4) = min(d25(3) , d24(3) + d 45(3) )


d24(4) = min ( 1, 1+ 0) = 1 d25(4) = min ( 7, 1+(-2)) = -1

d31(4) = min(d31(3) , d 34(3) + d 41(3) ) d32(4) = min(d32(3) , d34(3) + d 42(3) )


d31(4) = min ( ∞, 5+ 2) = 7 d32(4) = min ( 4, -5+5) = 0

d34(4) = min(d34(3) , d 34(3) + d 44(3) ) d35(4) = min(d35(3) , d34(3) + d 45(3) )


d34(4) = min ( -5, -5+0) = -5 d35(4) = min ( 11, 5+(-2)) = 3

d41(4)= min(d41(3) , d44(3) + d 41(3) ) d42(4) = min(d42(3) , d44(3) + d 42(3) )


d41(4) = min ( 2, 0+ 2) = 2 d 42(4) = min ( 5, 0+5) = 5

d43(4) = min(d43(3) , d 44(3) + d 43(3) ) d45(4) = min(d45(3) , d44(3) + d 45(3) )


d43(4) = min ( 10, 0+10) = 10 d45(4) = min ( -2, 0+(-2)) = -2

d51(4) = min(d51(3) , d54(3) + d41(3) ) d52(4) = min(d52(3) , d54(3) + d42(3) )


d51(4) = min ( ∞, 6+ 2) = 8 d52(4) = min ( ∞, 6+(-1) = 5

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

Preparing π4 from D4: 4 as Intermediate vertex

There will be two cases:


a) Simply copy all the values of π3to π4where 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))
b) If there is the changeinD4 matrix from D3 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)

Step 5: When K=5 (Node '5' is an intermediate node)


Similarly, by applying the values, we get D(5).

4.2.5. Floyd Warshall’s Algorithm: Method 2

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.

Step 2: Preparing D1 from D0 Matrix: (1 is behaving as an Intermediate from vertex i to j)


In this, category there may be four possible cases:
Here, Intermediate Vertex 1 is either behaving as source and destination vertex at the following
positions such as (1,1)(1,2)(1,3)(1,4)(1,5)(2,1)(3,1)(4,1)(5,1). Hence, we simply copy all the values
of D0 in D1 at these positions. Here, we simply have dijk=dijk-1(minimum value) from the recursive
formulation dijk = min (dijk-1, dikk-1 + dkjk-1).
If the copied row contains any value as  then simply copy the corresponding column 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, 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 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):

In this, category there may be four possible cases:


Here, Intermediate Vertex 2 is either behaving as source and destination vertex at the following
positions such as (2,1)(2,2)(2,3)(2,4)(2,5)(1,2)(3,2)(4,2)(5,2). Hence, we simply copy all the values
of D1 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 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.

Step 2: Preparing π1 from D1: 1 as Intermediate vertex


There will be two cases:
Simply copy all the values of π0 to π1 where there is no change in D1 matrix from D0. 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 changeinD1 matrix from D0 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 3: Preparing π2 from D2: 2 as Intermediate vertex


There will be two cases:
a) Simply copy all the values of π1 to π2 where there is no change in D2 matrix from D1. 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 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))

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.

Step 2: Preparing D1 from D0 Matrix: (1 is behaving as an Intermediate from vertex i to j):


In this, category there may be four possible cases:
Here, Intermediate Vertex 1 is either behaving as source and destination vertex at the following
positions such as (1,1)(1,2)(1,3)(1,4)(1,5)(1,6)(2,1)(3,1)(4,1)(5,1)(6,1). Hence, we simply copy all the
values of D0 in D1 at these positions. Here, we simply have dijk=dijk-1(minimum value) from the
recursive formulation dijk = min (dijk-1, dikk-1 + dkjk-1).
If the copied row contains any value as  then simply copy the corresponding column 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, dkj contains values as .Here,

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

You might also like