You are on page 1of 73

www.rejinpaul.

com

DS UNIT V
Syllabus: Representation of Graphs Breadth-first search Depth-first search
Topological sort Minimum Spanning Trees Kruskal and Prim algorithm Shortest path
algorithm Dijkstras algorithm Bellman-Ford algorithm Floyd-Warshall algorithm.

Introduction to Graphs
Graph is a non-linear Data Structure. It consists of vertices and edges. It is denoted by

G={V,E}

Vertices:They are the nodes in the graph. They are represented in a circle.

Ex: A

Edges : Two adjacent vertices are joined by edges.

Ex: A B

5.1 Types of Graphs:


There are 4 types of graphs. They are

unweighted undirected graph


weighted undirected graph
unweighted directed graph
weighted directed graph

Fig. 5.1.1 unweighted undirected graph Fig. 5.1.2 weighted undirected graph

Fig. 5.1.3unweighted directed graph Fig. 5.1.4 weighted directed graph

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Note: If the weight of the edge is not specified, then the default value is 1.

5.2 Basic terminologies


Directed Graph (or) Digraph

Directed graph is a graph which consists of directed edges, where each edge in E is
unidirectional. It is also referred as Digraph. If (v,w) is a directed edge then (v,w) # (w,v)

A
(C,B) (B,C)
C B
B
Fig. 5.2.1

Undirected Graph

An undirected graph is a graph, which consists of undirected edges. If (v,w) is an undirected edge,
then (v,w)=(w,v)

(A,B) = (B,A)
C B
B
Fig. 5.2.2

Weighted Graph

A graph is said to be weighted graph if every edge in the graph is assigned a weight or value. It
can be directed or undirected graph.

Fig. 5.2.3 Fig. 5.2.4

Complete Graph

A complete graph is a graph in which there is an edge between every pair of vertices. A complete graph
with n vertices will have n(n-1)/2 edges. There is a path from every vertex to every other vertex. A
complete digraph is a strongly connected graph.

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Fig. 5.2.5

Strongly Connected Graph

If there is a path from every vertex to every other vertex in a directed graph then it is said to be
strongly connected graph. Otherwise, it is said to be weakly connected graph.

C B
B

Fig. 5.2.6Strongly connected Graph Fig. 5.2.7 Weakly connected Graph

Path

A path in a graph is a sequence of vertices 1,2,,nsuch that i, i+1 E for 1iN.


Referring Fig. 7.2.7 the path from A to B is A, C, B.

Length

The length of the path is the number of edges on the path, which is equal to N-1, where N
represents the number of vertices. Referring the Fig. 7.2.7 length of the path from A to B is 2.
(i.e) (A,C) , (C,B). If there is a path from a vertex to itself, with no edges, then the path length is
zero (0).

Loop

If the graph contains an edge (v,v) from a vertex to itself, then the path is referred to as a
loop.

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Simple Path

A simple path is a path such that all vertices on the path, except possibly the first and the
last are distinct. A simple cycle is the simple path of length atleast one that begins and ends at the
same vertex.

Cycle

A cycle in a graph is a path in which first and last vertex are the same. A graph which
has cycles is referred to as cyclic graph.

Fig. 5.2.8

Degree

The number of edges incident on a vertex determines its degree. The degree of the vertex
A written as degree (A). The indegree of the vertex A, is the number of edges entering into the
vertex A. Similarly outdegree of the vertex A is the number of edges exiting from that vertex A.
In Fig. 4.2.9, Indegree (A) = 2, Outdegree (A) = 1.

Fig. 5.2.9

Acyclic Graph

A directional graph which has no cycles is referred to as acyclic graph. It is abbreviated


as DAG (Directioinal Acyclic Graph).

Fig. 5.2.10

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.3 Representation of Graphs


There are several representations of graphs, but we will discuss the 3 commonly used
representations namely

Incidence Matrix
Adjacency Matrix
Adjacency List
5.3.1 Incidence Matrix

A graph containing m vertices and n edges can be represented by a matrix with m rows
and n columns. The matrix formed by storing 1 in the ith row and jth column corresponding to the
matrix, if there exists a ithvertex, connected to one end of the jthedge, and 0, if there is no ith
vertex, connected to any end of the jth edge of the graph, such a matrix is referred as an incidence
matrix.

IncMat [i] [j] = 1,if there is an edge Ejfrom vertex Vi

= 0, otherwise

Fig. 4.3.1

E1 E2 E3 E4
A 1 1 0 0
B 0 0 1 1
C 1 0 1 0
D 0 1 0 1
Fig.4.3.2 Incidence matrix for graph in Fig. 4.3.1

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.3.2 Adjacency Matrix

A graph containing n vertices can be represented by a matrix with n rows and n columns.
The matrix formed by storing the edge weight in its ithrow and jth column of the matrix, if there
exists an edge between ithand jth vertex of the graph, and 0 if there are no edges to itself and if
there are no edges between ithand jth vertex of the graph, such a matrix is referred as an adjacency
matrix.

1 2 3 4 5 6 7
1 0 1 1 1
2 0 1 1
3 0 1
4 1 0 1 1 1
5 1 0 1
6 0
7 1 0

Fig. 4.3.3 Fig. 4.3.4 Adjacency matrix

5.3.3 Adjacency List

A graph containing m vertices and n edges can be represented using a linked list, referred
to as adjacency list. The number of vertices in a graph forms a singly linked list. Each vertex
have a separate linked list, with nodes equal to the numberof edges connected from the
corresponding vertex.

Fig.5.3.5 Fig. 5.3.6 Adjacency List

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.4 Graph Traversals


A graph traversal is a systematic way of visiting the nodes in a specific order. There are 2
types of graph traversals namely,

Breadth First Search(BFS)


Depth First Search(DFS)
5.4.1 Breadth First Search (BFS)

Breadth First Search (BFS) of a graph G starts from an unvisited vertex u. Then all unvisited
vertices viadjacent to u are visited and then all unvisited vertices wjadjacent to viare visited and so on. The
traversal terminates when there are no more nodes to visit. BFS uses a queue data structure to keep track
of the order of the nodes whose adjacent nodes are to be visited.

5.4.1.1 Steps to Implement BFS

1. Select the start vertex and mark it as visited (i.e) place the value 1.
2. Enqueue the START vertex.
3. Dequeue the vertex.
4. Find all adjacent unvisited vertices of the dequeued vertex.
5. Mark all unvisited adjacent vertices as visited.
6. Enqueue all adjacent vertices.
7. Repeat from step 3 to step 6 until the queue becomes empty
Vertices Visited
A B A 1
B 1
C 1
D 1
enqueue() A B D C
D C Dequeue() A B D C
output: A B D C

Fig. 5.4.1 Fig. 5.5.2

Step By Step BFS Traversal for Graph in Fig. 5.4.1

Step 1
Vertices Visited
A
A 1
B 0
C 0
Lets start with the root vertex. Let us consider A as root vertex. D 0
enqueue() A
Mark the root vertex A as visited (1), enqueue() the vertex A and Dequeue() A
dequeue() it.

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 2

Find the adjacent unvisited vertices of the dequeued vertex A.

A B Vertices Visited
A 1
B 1
C 0
D 1
D enqueue() A B D
Dequeue() A B
Mark the unvisited adjacent vertices B and D as visited.

enqueue() the adjacent vertices B and D and dequeue() vertex B .

Step 3

Find the adjacent unvisited vertices for vertex B. Notice that since D is visited, it is not
considered.

B Vertices Visited
A 1
B 1
C 1
D 1
enqueue() A B D C
C Dequeue() A B
enqueue() vertex C, and mark it as visited

Step 4 Vertices Visited


A 1
dequeue() the vertex D. There are no unvisited adjacent vertices B 1
to vertex D because all the adjacent vertices are visited. C 1
D 1
enqueue() A B D C
Dequeue() A B D

Step 5 Vertices Visited


A 1
dequeue() the vertex C. There are no unvisited adjacent vertices B 1
to vertex C. Now all the vertices of the graph are visited. C 1
D 1
Therefore BFS ends. enqueue() A B D C
The BFS order is A B D C Dequeue() A B D C

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.4.1.2 Pseudo Code For BFS

void BFS ( Vertex S )


{
Vertex V,W;
Queue Q;
visited[S] = 1;
enqueue(S,Q);
while(!(Isempty(Q))
{
V=dequeue(Q);
print(V);
for each W adjacent to V
if ( visited[w] == 0 )
{
visited[W] = 1;
enqueue(W,Q);
}
}
}

Fig. 5.4.3
Advantages of Breadth-First Search

1. Breadth first search will never get trapped exploring the useless path forever.
2. If there is a solution, BFS will definitely find it out.
3. If there is more than one solution then BFS can find the minimal one that requires less
number of steps.

Disadvantages of Breadth-First Search

1. The main drawback of Breadth first search is its memory requirement. Since each level of
the tree must be saved in order to generate the next level, and the amount of memory is
proportional to the number of nodes stored, the space complexity of BFS is O(bd). As a
result, BFS is severely space-bound in practice so will exhaust the memory available on
typical computers in a matter of minutes.
2. If the solution is farther away from the root, breath first search will consume lot of time.

Applications of BFS

1. To find the shortest path from a vertex s to a vertex v inan unweighted graph
2. To find the length of such a path
3. To find out if a graph contains cycles
4. To construct a BFS tree/forest from a graph

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.4.2 Depth First Search (DFS)

Now, we will discuss the another algorithm for traversal of the graph. Visit the first node
initially, and then find the unvisited node which is adjacent to the first node, is visited and a DFS
is initiated from the adjacent node (considering it as the first node). If all the adjacent nodes have
been visited, backtrack to the last node visited, and find another adjacent node and again initiate
the DFS from adjacent node. This traversal continues until all nodes have been visited once.

5.4.2.1 Steps to Implement DFS

1. Select the start vertex


2. Visit the vertex ( place 1)
3. Push the vertex on the stack
4. pop the vertex
5. find the adjacent vertices and find any one of the unvisited adjacent vertex
6. repeat from step 2 to 5 until the stack becomes empty

Output: A C B D

Fig. 5.4.4 Fig. 5.4.5

Step By Step DFS Traversal for Graph in Fig. 5.4.4

Step 1

Let us start with the root vertex A.


Vertices Visited Push Pop
A 1 A A
B 0
A C 0
D 0
Output: A

10

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Mark the vertex A as visited. push the vertex A into the stack and pop it out.

Step 2

Find the unvisited adjacent vertices of the popped vertex A.

Vertices Visited Push Pop


A 1 A A
C B 0
C 1 C C
D 0
Output: A C
B

Either C or B can be selected as unvisited adjacent vertex. Let us select vertex C. Mark the
vertex C as visited. push the vertex C into the stack and pop it out.

Step 3

Find the unvisited adjacent vertices of the popped vertex C.

C D
Vertices Visited Push Pop
A 1 A A
B 1 B B
C 1 C C
B
D 0
Output: A C B

Notice that vertex A is not considered, since it is visited. Either B or D can be selected. Let us
select vertex B. Mark the vertex B as visited. push B into the stack and pop it out.

Step 4

Find the unvisited adjacent vertices of the popped vertex B.

D
Vertices Visited Push Pop
A 1 A A
B 1 B B

B 11

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

C 1 C C
D 1 D D

Output: A C B D

Notice that vertex C and A are not considered, since they are visited. Mark the vertex D as
visited. push D into the stack and pop it out.

Step 5

Since all the vertices of D are visited, and all the vertices in the graph are visited, DFS ends. The
output is the order in which the vertices are popped out. Output: A C B D.

5.4.2.2 Pseudo Code For DFS

void DFS ( Vertex V )


{
Vertex W;
Visited[V] = 1;
push(V);
pop(V);

for each W adjacent to V


if( !Visited[W] )
DFS(W);
}

Fig. 5.4.6

Advantages of Depth-First Search

1. The advantage of depth-first Search is that memory requirement is only linear with
respect to the search graph. This is in contrast with breadth-first search which requires
more space. The reason is that the algorithm only needs to store a stack of nodes on the
path from the root to the current node.
2. The time complexity of a depth-first Search to depth d is O(b^d) since it generates the
same set of nodes as breadth-first search, but simply in a different order. Thus practically
depth-first search is time-limited rather than space-limited.
3. If depth-first search finds solution without exploring much in a path then the time and
space it takes will be very less.

12

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Disadvantages of Depth-First Search

1. The disadvantage of Depth-First Search is that there is a possibility that it may go down
the left-most path forever. Even a finite graph can generate an infinite tree. One solution
to this problem is to impose a cutoff depth on the search. Although the ideal cutoff is the
solution depth d and this value is rarely known in advance of actually solving the
problem. If the chosen cutoff depth is less than d, the algorithm will fail to find a
solution, whereas if the cutoff depth is greater than d, a large price is paid in execution
time, and the first solution found may not be an optimal one.
2. Depth-First Search is not guaranteed to find the solution.
3. And there is no guarantee to find a minimal solution, if more than one solution exists.

Applications of DFS
1. To find a path from a vertex s to a vertex v.
2. To find the length of such a path.
3. To construct a DFS tree/forest from a graph.

5.4.2.3 Applications of DFS

To check whether the undirected graph is connected or not


To check if the connected undirected graph is bi-connected or not
To check whether the directed graph is a-cyclic or not
5.4.2.4 Undirected Graph

An undirected graph is connected if and only if a DFS starting from any node visits each
and every node.

Rules for constructing DFS spanning tree

1. Vertex old, path old, ignore the edge


2. Vertex new, path new, draw tree edge ( )
3. Vertex old, path new, draw back edge ( - - > )

13

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step by step construction of DFS spanning tree for graph in Fig. 5.4.7

Step 1

Select the starting vertex. Let the starting vertex be A. From A we travel to vertex B. The vertex
B is unvisited and the path from A to B is new. Therefore draw a tree edge between A and B in
DFS spanning tree.

Step 2

From B, we traverse to vertex C. C is an unvisited vertex and edge from B to C is new. Therefore
draw a tree edge between B and C in DFS spanning tree.

14

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 3

From C, we traverse to vertex D. D is an unvisited vertex and edge from C to D is new.


Therefore draw a tree edge between C and D in DFS spanning tree.

Step 4

Since A,B,C are all visited, we back track from D in DFS spanning tree. Thus we reach vertex C.
From C we traverse to vertex E. The vertex E is new and the edge from C to E is also new.
Therefore draw a tree edge from C to E in DFS spanning tree.

15

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 5

From E we have an edge from E to A. The vertex A is visited, but the edge is new. Therefore
draw a back edge from E to A in DFS spanning tree.

Step 6

Since all the edges from E are visited we back track to C in DFS spanning tree. Since we have 2
unvisited edges from D, we travel to D. Since vertices A and B are old, but the edges are new,
draw back edges from D to A and from D to B in DFS spanning tree.

16

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

17

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.4.2.5 Checking Bi-Connectivity

Bi-Connectivity

A connected undirected graph is bi-connected if there are no vertices whose removal


disconnects the rest of the graph.

Articulation point

The vertices whose removal would disconnect the graph are known as articulation points.

Steps to find Articulation point

1. Find DFS spanning tree


2. Number the vertex in the order in which they are visited. This number is referred
as Num(v)
3. Compute the lowest numbered vertex for every vertex v in the DFS spanning tree
which we call as Low(w)(i.e) reachable from v by taking 0 or more tree edges
and then possible one back edge. By definition Low(v) is the
a. Minimum of Num(v)
b. The lowest Num(w) among all back edges
c. The lowest Low(w) among all tree edges
(i.e) min(Num(v),Num(w),Low(w))

Use post order traversal to calculate Low(v). The root is articulation if and only if it has
more than 2 children. Any vertex V other than root is an articulation point if and only if V has
some children such that Low(w)Num(v).

Example

Checking whether the graph is bi-connected or not by finding the articulation point

18

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Low(v)=min(Num(v),Num(w),Low(w))

Low(F)=min(Num(F),Num(D),Low(D))
=min(6,4)
=4
Low(E)=min(Num(E),Num(F),Low(F))
=min(5,6,4)
=4
Low(D)=min(Num(D),Num(E),Low(E),Num(A),Low(A))
=min(4,5,4,1)
=1
Low(G)=min(Num(G))
=min(7)
=7
Low(C)=min(Num(C),Num(D),Low(D),Num(G),Low(G))
=min(3,4,1,7,7)
=1
Low(B)=min(Num(B),Num(C),Low(C))
=min(2,3,1)
=1
Low(A)=min(Num(A),Num(B),Low(B))
=min(1,2,1)
=1

At vertex F
Low(W) Num(V)
1 6

19

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

At vertex D
Low(A) Num(D)
1 4

Low(E) Num(D)DFS spanning tree


4 4(articulation point)

At vertex E
Low(F) Num(E)
4 5

At vertex C
Low(D) Num(C)
1 3

Low(G) Num(C)
7 3(articulation point)

5.4.3 Comparison between DFS and BFS

S.No DFS BFS


1 This traversal is done with the help of stack This traversal is done with the help of queue data
data structure structure
2 It works using two ordering. The first order It works using one ordering. The order in which
is the orderin which the vertices are reached the vertices are reached in the same order they get
for the first time (i.e the visited vertices are removed from the queue
pushed onto the stack) amd the second order
in which the vertices become dead end
(vertices are popped off the stack)
3 The DFS sequence is composed of tree edges The BFS sequence is composed of tree edges and
and back edges cross edges
4 Application: To check connectivity, a- Application: To check connectivity, a-
cyclicity of a graph and to find articulation cyclicity of a graph and to find shortest path
point, DFS is used. between two vertices BFS is used

5.5 Topological Sort


It is a linear ordering of vertices in a directed a-cyclic graph, such that if there is a path
from Vi to Vj, then Vj appears after Vi in the linear ordering.

5.5.1 Procedure

1. Find the indegree for every vertex


2. Place the vertices whose indegree is zero on the empty queue

20

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

3. Dequeue one vertex at a time from the queue and decrement the indegree of all its
adjacent vertices
4. Enqueue a vertex to the queue if its indegree falls to zero
5. Repeat from step step 3 unitl the queue becomes empty
The topological ordering is the order in which the vertices are dequeued.

21

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 1

Find the Indegree of vertices 1,2,3,4,5,6.Indegree of a vertex is the number of edges entering into
the vertex. Indegree of vertex 1 is 0, vertex 2 is 0, vertex 3 is 1, vertex 4 is 3, vertex 5 is 1, vertex
6 is 3.

Vertices Indegree
1 0
2 0
3 1
4 3
5 1
6 3
enque()
dequeue()
Step 2

enqueue() the vertices with Indegree 0. Therefore enqueue() vertices 1 and 2.

22

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Vertices Indegree
1 0
2 0
3 1
4 3
5 1
6 3
enque() 1,2
dequeue()
Step 3

dequeue() vertex 1 and find all the adjacent vertices. The adjacent vertices are 3 and 4. Therefore
decrement the Indegree of the adjacent vertices 3 and 4 by 1.
Vertices Indegree
1 3 1 0
2 0
3 1 0
4 3 2
5 1
4 6 3
enque() 1,2
Step 4 dequeue() 1

enqueue() the vertex 3 since its Indegree is 0. dequeue() the vertex 2.

Vertices Indegree
1 0
2 0
3 1 0
4 3 2
5 1
6 3
enque() 1,2 3
dequeue() 1 2

23

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 5
Find the adjacent vertices of vertex 2. Decrement the Indegree of the adjacent vertices 4 and 5 by
1.

4 Vertices Indegree
1 0
2 0
3 1 0
2 5 4 3 2 1
5 1 0
6 3
enque() 1,2 3
dequeue() 1 2

Step 6
enqueue() the vertex 5 since its indegree is 0. dequeue() the vertex 3.

Vertices Indegree
1 0
2 0
3 1 0
4 3 2 1
5 1 0
6 3
enque() 1,2 3 5
dequeue() 1 2 3

Step 7
Find the adjacent vertices of the dequeued vertex 3. Decrement the Indegree of the vertices 4 and
6 by 1.

3
Vertices Indegree
1 0
2 0
3 1 0
4 6 4 3 2 1 0
5 1 0
6 3 2
enque() 1,2 3 5
dequeue() 1 2 3

24

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 8
enqueue() the vertex 4, because its Indegree is 0. dequeue() the vertex 5.

Vertices Indegree
1 0
2 0
3 1 0
4 3 2 1 0
5 1 0
6 3 2
enque() 1,2 3 5 4
dequeue() 1 2 3 5

Step 9

Find the adjacent vertices of the dequeued


Vertices Indegree
vertex 5. Decrease the Indegree of the vertex 6 1 0
by 1. 2 0
6 3 1 0
4 3 2 1 0
5 1 0
6 3 2 1
enque() 1,2 3 5 4
5
dequeue() 1 2 3 5

Step 10

dequeue() the vertex 4 whose Indegree is 0 and Vertices Indegree


1 0
find the adjacent vertices of the vertex 4. 2 0
3 1 0
4 3 2 1 0
5 1 0
4 6 6 3 2 1
enque() 1,2 3 5 4
dequeue() 1 2 3 5 4

25

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Vertices Indegree
1 0
2 0
Step 11
3 1 0
4 3 2 1 0
Decrement the Indegree of the vertex 6
5 1 0
by1. 6 3 2 1 0
enque() 1,2 3 5 4
dequeue() 1 2 3 5 4

Step 12
Vertices Indegree
enqueue() and dequeue() the vertex 6. 1 0
2 0
The topological sorted order is 3 1 0
4 3 2 1 0
123546 5 1 0
6 3 2 1 0
enque() 1,2 3 5 4 6
dequeue() 1 2 3 5 4 6

Pseudo Code For Topological Sort

void Topsort(Graph G)
{
Queue Q;
Vertex V,W;
int counter=0;
Q = Create_Queue(NumVertex);
MakeEmpty(Q);
for each vertex V
if ( Indegree[V] == 0 )
enqueue(V,Q);
while( !IsEmpty(Q) )
{
V = Dequeue(Q);
Topnum[V] = ++counter;
for each W adjacent to V

26

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

if ( --Indegree[W] == 0)
enqueue(W,Q);
}
if ( counter != NumVertex )
error("Graph has a cycle!..");
DisposeQueue(Q);
}
Fig. 4.5.5

5.6 Minimum Spanning Tree (MST)


A spanning tree of a graph is just a subgraph that contains all the vertices of the graph, but uses
only sufficient edges to form a tree. A graph may have many spanning trees. Let us consider a graph
containing n vertices, the spanning trees formed for that graph must have n-1 edges. For example, the
complete graph shown in Fig. 4.6.1 which has four vertices, has four spanning trees as shown in Fig. 5.6.2

A B A B A B

D C D C D C

Fig. 5.6.1
A B A B

D C D C

Fig. 5.6.2

A minimum spanning tree in a network is one of the spanning trees of the graph which has
the smallest sum of weights amongst all spanning trees. Minimum spanning trees can be
computed quickly and easily, and they create a sparse subgraph hat reflects a lot about the
original graph. As an educational tool, minimum spanning tree algorithms provides graphical
evidence tat greedy algorithms can give provably optimal solutions. You can create minimum
spanning trees, using two greedy algorithms. They are,

Prims algorithm
Kruskals algorithm
Usually a greedy algorithm, at each stage makes the local best choice, with no concern to
the implications on the future choices. Such techniques generally do not deliver optimal
solutions, but in this case the greedy algorithm for both the Prims and Kruskals algorithm,
delivers the best case for finding the minimum spanning tree, for a given graph.

27

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.6.1 Prims algorithm

At each stage a new vertex to add to the tree is done by choosing the edge (u,v) such that
the cost of u,v is the smallest among all edges where u is in the tree and v is not in the tree. The
concept used in Prims algorithm is divide and conquer. The strategy used in Prims algorithm is
greedy technique.

Divide: Smaller problems are solved recursively


Conquer: The solution to the original problem is then formed from the solutions of the sub
problems.

5.6.1.1 Procedure to construct MST by Prims Algorithm

1. Select the start or source vertex and mark it as the known vertex (known = 1) and make
its distance value dv, path vertex pv as 0 and make dv and pvof other vertices as and 0
respectively.
2. Find the adjacent vertices of known (known=1) vertex.
3. If any of the adjacent vertices is already known, no need to find the cost. Otherwise, find
the minimum cost for the unknown adjacent vertices by using the formula.
dw = min( dw,Cc,w)
Cc,wcost on the edge
and check the following condition.
If new cost is lesser than the previous cost, then update pvand dv. Otherwise
no need to update dv and pvof the respective vertex.
4. Select the vertex which has the smallest dv among the unknown vertices and mark it as
known vertex.
5. Repeat from step 2 to step 4 until all the vertices are marked known.

28

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.6.1.2 Step by step process to find MST for Graph in Fig. 5.6.3

Given graph is undirected weighted graph. So MST can be constructed. Initial configuration
table is

Vertices Known Dv pv
V1 1 0 0
V2 0 0
V3 0 0
V4 0 0
V5 0 0
V6 0 0
V7 0 0

Step 1

Find the adjacent vertices of the source vertex V1. Calculate new dw of the adjacent vertices.
dwof a vertex is the minimum of its dv value and the cost on the edge. Update dv, if and only if
newly calculated dwvalue is less than the current value of dv of respective vertices.

Since, newly calculated dwvalues are lesser than the current dv values of respective adjacent
vertices V2, V3, V4, the dvvalues are assigned with dw. Then set the path vertex pvas the current
vertex. The current vertex is V1. Therefore set the pv values of the adjacent vertices V2, V3,
V4as V1. New configuration table is:

29

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Vertices Known Dv pv
V1 1 0 0
V2 0 2 V1
V3 0 4 V1
V4 0 1 V1
V5 0 0
V6 0 0
V7 0 0

Step 2

Select the vertex that has the minimum dv value among the unknown vertices as current vertex.
Here V4 has the minimum dv value among the unknown vertices. Therefore select V4 as the
current vertex, and mark it as known vertex (i.e.) set known = 1.

Calculate dw values for each adjacent vertices V2, V5, V7, V6, V3. Since V1 is known, it is
ignored. The newly calculated dw values are lesser for V3, V5, V6 and V7 than their respective
dv values. Therefore update their dv values with respective dw values. Set V3, V5, V6 and V7
spvas current vertex V4. Since V2 sdv and dwvalues are equal, it is ignored. The new
configuration table is:

Vertices Known Dv pv
V1 1 0 0
V2 0 2 V1
V3 0 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 8 V4
V7 0 4 V4

30

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 3

Select a vertex that is unknown and has minimum dv as current vertex. V2 and V3 have
minimum dv value and both are unknown. Any one of them can be set as current vertex. Let us
consider V2 as current vertex.

Set V2 as known vertex. Find the adjacent vertices of V2. Since V1 and V4 are known, they are
omitted. Calculate dv for V5. Since dv anddw are equal for V5, updation is not done. The new
configuration table is:

Vertices Known dv Pv
V1 1 0 0
V2 1 2 V1
V3 0 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 8 V4
V7 0 4 V4

Step 4

Select a vertex that is unknown and has minimum dvas current vertex. V3 has minimum dv value
among unknown vertices. Select V3 as current vertex. Find the adjacent vertices of V3.

Since V1 and V4 are known, they are ignored. The newly calculated dwvalue of V6 is less than
its dv value. Therefore update dv value with dw. Set pvof V6 as V3. The new configuration table
is:

Vertices Known dv Pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 5 V3
V7 0 4 V4

Step 5

Select a vertex that is unknown and has minimum dv as current vertex. V7 has minimum dv
value among unknown vertices. Select V7 as current vertex. Set V7 as known vertex.

Find the adjacent vertices of V7. Since V4 is known it is ignored. The newly calculated dwvalues
of V5 and V6 are lesser than their respective dv values. Therefore update their dv values with
respective dw values. Set their pv values as V7. The new configuration table is:

31

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Vertices Known dv Pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 6 V7
V6 0 1 V7
V7 1 4 V4

Step 6

Select a vertex that is unknown and has minimum dv as current vertex. V6 has minimum dv
value among unknown vertices. Select V6 as current vertex. Set V6 as known. Find the adjacent
vertices of V6.Since V3, V4 and V7 are all known ignore them. The new configuration table is:

Vertices Known dv Pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 6 V7
V6 1 1 V7
V7 1 4 V4

Step 7

Select a vertex that is unknown and has minimum dv as current vertex. V5 has minimum dv
value among unknown vertices. Select V5 as current vertex. Set V5 as known. Find the adjacent
vertices of V5. Since V2, V4 and V7 are all known ignore them. The new configuration table is:

Vertices Known dv Pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 1 6 V7
V6 1 1 V7
V7 1 4 V4

Step 8

Draw all the vertices given in the undirected weighted graph. Draw edge by referring initial
configuration table between vertex and pvcolumn with the cost of dv column.

32

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.6.1.3 Pseudo code for Prims algorithm

void Prim ( Table T )


{
vertex v,w,c;

33

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

for( ; ; )
{
v = smallest unknown distance vertex;
if ( v == not a vertex )
break;
T[v].known = true;
for each w adjacent to v
if ( !(T[w].known) )
{
c = min ( T[w].Dist, cv,w )
if ( c < T[w].Dist )
{
/* update w */
Decrease( T[w].Dist to c);
T[w].path = v;
}
}
}
}

Fig. 5.6.7

5.6.2 Kruskals Algorithm

Kruskals Algorithm uses a greedy technique to compute a minimum spanning tree. This
algorithm selects the edges in ascending order and accepts an edge if it doesnt cause a cycle.
The algorithm terminates if enough edges are accepted.

In general, Kruskals algorithm maintains a forest a collection of trees. Initially, there


are | V | single node trees. Adding an edge merges two trees into one. When the algorithm
terminates, there is only one tree, which is called as Minimum Spanning Tree (MST).This
algorithm uses two Data Structures namely find and union.

5.6.2.1 Steps for constructing MST using Kruskals Algorithm

1. Form a table with 3 columns namely, edges, weight/cost, action (accept/reject)


2. Arrange all the edges in the table in ascending order of cost.
3. Traverse the table from top to bottom.
4. An edge is selected when it doesnt form cycle in the graph, otherwise it is
rejected.
5. Repeat step 4 until all the edges are either accepted or rejected.
6. Finally, draw the MST with all the vertices, and selected or accepted edges.

34

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.6.2.2 Step by step process to find MST for Graph in Fig. 5.6.3

Step 1

Construct the table with 3 columns. Arrange the edges in ascending order based on their weight
or cost.

Weight / Action ( Accept /


Edges
Cost Reject)
( V1, V4 ) 1
( V6, V7 ) 1
( V1, V2 ) 2
( V3, V4 ) 2
( V2, V4 ) 3
( V4, V7 ) 4
( V1, V3 ) 4
( V3, V6 ) 5
( V5, V7 ) 6
( V4, V5 ) 7
( V4, V6 ) 8
( V2, V5 ) 10

Step 2

Consider the edge ( V1,V4 ). The edge doesnt form any cycle in the graph. Therefore the edge (
V1, V4 ) is selected or accepted.

Weight / Action ( Accept /


Edges
Cost Reject)
( V1, V4 ) 1
( V6, V7 ) 1
( V1, V2 ) 2
( V3, V4 ) 2
( V2, V4 ) 3
( V4, V7 ) 4
( V1, V3 ) 4
( V3, V6 ) 5
( V5, V7 ) 6
( V4, V5 ) 7
( V4, V6 ) 8
( V2, V5 ) 10

35

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

The intermediate MST is

Step 2

Now consider the edge ( V6, V7 ). The edge doesnt form any cycle in the graph. Therefore the
edge( V6, V7 ) isaccepted.

Weight / Action ( Accept /


Edges
Cost Reject)
( V1, V4 ) 1
( V6, V7 ) 1
( V1, V2 ) 2
( V3, V4 ) 2
( V2, V4 ) 3
( V4, V7 ) 4
( V1, V3 ) 4
( V3, V6 ) 5
( V5, V7 ) 6
( V4, V5 ) 7
( V4, V6 ) 8
( V2, V5 ) 10

. The intermediate MST is:

36

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 3

Now consider the edge ( V1, V2 ). The edge doesnt form any cycle in the graph. Therefore the
edge ( V1, V2 ) is accepted.

Weight / Action ( Accept /


Edges
Cost Reject)
( V1, V4 ) 1
( V6, V7 ) 1
( V1, V2 ) 2 The intermediate MST is:
( V3, V4 ) 2
( V2, V4 ) 3
( V4, V7 ) 4
( V1, V3 ) 4
( V3, V6 ) 5
( V5, V7 ) 6
( V4, V5 ) 7
( V4, V6 ) 8
( V2, V5 ) 10

Step 4

Consider the edge ( V3, V4 )The edge doesnt form any cycle in the graph. Therefore the edge
( V3, V4 ) is accepted.

Weight / Action ( Accept /


Edges
Cost Reject)
( V1, V4 ) 1
( V6, V7 ) 1
( V1, V2 ) 2
( V3, V4 ) 2
( V2, V4 ) 3
( V4, V7 ) 4
( V1, V3 ) 4
37

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

( V3, V6 ) 5 The intermediate MST is:


( V5, V7 ) 6
( V4, V5 ) 7
( V4, V6 ) 8
( V2, V5 ) 10

Step 5

Consider the edge ( V2, V4 )This edge forms a cycle in the graph. Therefore the edge ( V2, V4 )
is rejected.

Weight / Action ( Accept /


Edges
Cost Reject)
( V1, V4 ) 1
( V6, V7 ) 1
( V1, V2 ) 2
( V3, V4 ) 2 The intermediate MST is:
( V2, V4 ) 3
( V4, V7 ) 4
( V1, V3 ) 4
( V3, V6 ) 5
( V5, V7 ) 6
( V4, V5 ) 7
( V4, V6 ) 8
( V2, V5 ) 10

Step 6

Consider the edge ( V4, V7 )This edge doesnt form any cycle in the graph. Therefore the edge
( V4, V7 ) is accepted.

Weight / Action ( Accept /


Edges
Cost Reject)
( V1, V4 ) 1
( V6, V7 ) 1

38

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

( V1, V2 ) 2
( V3, V4 ) 2 The intermediate MST is:
( V2, V4 ) 3
( V4, V7 ) 4
( V1, V3 ) 4
( V3, V6 ) 5
( V5, V7 ) 6
( V4, V5 ) 7
( V4, V6 ) 8
( V2, V5 ) 10

Similarly, all the edges are either accepted or rejected. The final MST is

5.6.2.3 Pseudo code for Kruskals algorithm

void Kruskal ( Graph G )


{
intEdgesAccepted = 0;
DisjointSet S;
Heap H;
Vertex U, V;
SetTypeUset, Vset;
Edge E;
Ininitialize(S);
BuildHeap(H); //construction of Min Heap
while( EdgesAccepted<NumVertex - 1 )
{
E = DeleteMin(H);
Uset = Find(U,S);
Vset = Find(V,S);
if(USet != Vset)//Edge is accepted,since no cycle exist
{

39

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

EdgesAccepted++;
SetUnion(S,Uset,Vset);
}
}
}

SetType Find(Vertex U, DisjointSet S)


{
if ( S[U] <= 0 )
return U;
else
return Find(S[U],S);
}

void SetUnion( DisjointSet S, SetTypeUset, SetTypeVset)


{
S[Vset] = Uset;
}

Fig. 5.6.8

5.7 Shortest Path Algorithm


So far, we have seen, the steps for traversing (visiting) the nodes in a non-weighted
graph. In a weighted graph, you should consider the weight ( i.e , distance between any two
nodes) specified for each edge between two vertices. Our aim is to visit any two nodes, with a
minimum weight, such that the distance travelled is minimum. One such algorithm is referred as
Dijikstra's Shortest Path Algorithm. This is very much used in travelling salesman problem.

There are 2 types of Shortest path algorithm

1. Unweighted
2. Weighted
5.7.1 Unweighted Shortest Path problem

1. Initial Configuration Table


It consists of 4 columns vertices, known, distance value(dv), path vertex(pv)
Select the source or start vertex
Place the value 0 to known, dv+pv for the source
For all other vrtices place the value 0 to known to dv and 0 to pv

40

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

2. Procedure
1. Enqueue the start or source vertex
2. Dequeue the vertex and mark it as the known vertex by placing 1 to known in
the table and find its adjacent vertices.
3. If any of the adjacent vertices is already known (known=1), then no need to
calculate the dv otherwise calculate dv for the unknown (known=0) adjacent
vertices by using the formula (dw=dv+1) where
a. w = adjacent vertex
b. v = current vertex and check the following condition
4. If the newly calculated dw is less than the previous dv of the respective
vertex, then update the dv and pv otherwise no need to update dv and pv of
the respective vertex.
5. Enqueue the adjacent vertices
6. Repeat from 1 to 5 until the queue becomes empty

Vertices Known dv Pv
A 0 0
B 0 0 0
C 0 0
D 0 0
E 0 0
F 0 0
G 0 0
Enqueue B
Dequeue
Step 1:
Vertices Known dv Pv
A 0 0
B 1 0 0
C 0 0
D 0 0
E 0 0
F 0 0
G 0 0
Enqueue B
Dequeue B

41

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Vertices Known dv Pv
A 0 0
B 1 0 0
C 0 1 B
D 0 0
E 0 1 B
F 0 0
G 0 1 B
Enqueue B C E G
Dequeue B
Step 2:
Vertices Known dv Pv
A 0 0
B 1 0 0
C 1 1 B
D 0 0
E 0 1 B
F 0 0
G 0 1 B
Enqueue B C E G
Dequeue B C

Vertices Known dv Pv
A 0 0
B 1 0 0
C 1 1 B
D 0 2 C
E 0 1 B
F 0 0
G 0 1 B
Enqueue B C E G D
Dequeue B C

Step 3
Vertices Known dv Pv
A 0 0
B 1 0 0
C 1 1 B
D 0 2 C
E 1 1 B
F 0 0
G 0 1 B
Enqueue B C E G D
Dequeue B C E

42

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Vertices Known dv Pv
A 0 0
B 1 0 0
C 1 1 B
D 0 2 C
E 1 1 B
F 0 2 E
G 0 1 B
Enqueue B C E G D F
Dequeue B C E
Step 4:
Vertices Known dv Pv
A 0 0
B 1 0 0
C 1 1 B
D 0 2 C
E 1 1 B
F 0 2 E
G 1 1 B
Enqueue B C E G D F
Dequeue B C E G

Step 5:
Vertices Known dv Pv
A 0 0
B 1 0 0
C 1 1 B
D 1 2 C
E 1 1 B
F 0 2 E
G 1 1 B
Enqueue B C E G D F
Dequeue B C E G D

Vertices Known dv Pv
A 0 3 D
B 1 0 0
C 1 1 B
D 1 2 C
E 1 1 B
F 0 2 E
G 1 1 B
Enqueue B C E G D F A
Dequeue B C E G D

43

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 6:
Vertices Known dv Pv
A 0 3 D
B 1 0 0
C 1 1 B
D 1 2 C
E 1 1 B
F 1 2 E
G 1 1 B
Enqueue B C E G D F A
Dequeue B C E G D F

Step 7:
Vertices Known dv Pv
A 1 3 D
B 1 0 0
C 1 1 B
D 1 2 C
E 1 1 B
F 1 2 E
G 1 1 B
Enqueue B C E G D F A
Dequeue B C E G D F A

5.7.2 Dijikstra's algorithm

Initial Configuration Table


Select the source or the start vertex
Mark itas known (known=1) and place 0 to dv and pv
for all other vertices place the value 0 to known to dv and 0 to pv
Procedure

1. Find the adjacent vertices of the known vertex


2. If any of the adjacent vertices is already known (known=1), then no need to calculate the
dv otherwise calculate dv for the unknown (known=0) adjacent vertices by using the formula
dw = dv + cvw
cvw= cost on the edge (v,w)
3. If newly calculated dw is less than the previous dv of the respective vertex, then update
the dv and pv. Otherwise no need to update dv and pv of the respective vertex.
4. Among the unknown vertices select a vertex which has shortest dv and mark it as the
known vertex.
5. Repeat from 1 go 4 until all the vertices are marked known.

44

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Example:

Step 1:

Vertices Known dv Pv
A 1 0 0
B 0 0
C 0 0
D 0 0
E 0 0
H 0 0

Vertices Known dv Pv
A 1 0 0
B 0 2 A
C 0 0
D 0 0
E 0 5 A
H 0 4 A

Step 2:

Vertices Known dv Pv
A 1 0 0
B 1 2 A
C 0 0
D 0 0
E 0 5 A
H 0 4 A

45

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Vertices Known dv Pv
A 1 0 0
B 1 2 A
C 0 7 B
D 0 0
E 0 5 A
H 0 4 A

Step 3:

Vertices Known dv Pv
A 1 0 0
B 1 2 A
C 0 7 B
D 0 0
E 0 5 A
H 1 4 A

Vertices Known dv Pv
A 1 0 0
B 1 2 A
C 0 6 H
D 0 0
E 0 5 A
H 1 4 A

Step 4:

Vertices Known dv Pv
A 1 0 0
B 1 2 A
C 0 6 H
D 0 0
E 1 5 A
H 1 4 A

Vertices Known dv Pv
A 1 0 0
B 1 2 A
C 0 6 H
D 0 8 E
E 1 5 A
H 1 4 A

46

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Step 5:

Vertices Known dv Pv
A 1 0 0
B 1 2 A
C 1 6 H
D 0 8 E
E 1 5 A
H 1 4 A

Step 6:

Vertices Known dv Pv
A 1 0 0
B 1 2 A
C 1 6 H
D 1 8 E
E 1 5 A
H 1 4 A

SINGLE SOURCE SHORTEST PATHS


Shortest path of a pair of vertices <u , v > : a path from u to v, with minimum path
weight.

Application
I) Gps navigator
II) If weights are time, it produces the fastest route
III) If weights are gas cost, it produces the lowest cost route
IV) If weights are distance, it produces the shortest route

Single-Source shortest path problem


Given a weighted, directed graph G = ( V , E ) with source vertex S, find all the shortest
(least weight) paths from S to all vertices in V.

Classic algorihms to solve single-source shortest path problem


Bellman-Ford algorithm
A dynamic programming algorithm
Works when some weights are negative
Dijikstra's algoithm
A greedy algorithm
Faster than Bellman-Ford
Works when weights are all non-negative

47

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.8 Bellman Ford algorithm


Given a directed weighted graph G = ( V , E ) and source S, the Bellman Ford algorithm
returns the shortest path lengths from S to every vertex or indicates that there is a negative
weight cycle in G reachable from S.

Observation
If there is a negative cycle, there is no solution
Add this cycle again can always produces a less weight path
If there is no negative cycle, a shortest path has atmost | V | - 1 edges
Idea:
Solve it using dynamic programming
For all the paths have atmost 0 edge, find all the shortest paths
For all the paths have atmost 1 edge, find all the shortest paths



For all the paths have atmost | V | - 1 edge, find all the shortest paths

Bellman Ford Algorithm

48

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Note: Relaxing an edge (u,v) means testing whether we can unprove the shortest path to V found
so far by joins trough u

Procedure
I) Select the start vertex and perform 0-edge shortest distance from start vetex to other

49

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

vertices. ( find path lengths = 0 , from source vertex to other vertices ) i.e
1) Initialize the distance value of start or source vertex as 0 and path vertex as 0 (NIL)
2) Initialize the distance values as to all other vertices.

II) Do relaxation steps for | V | - 1 times


| V | number of vertices in the graph
a) find path lengths 1, from source to other vertex
If the newly calculated distance is less than the previous distance value do
update distance and path vertex

b) Find path lengths 2, from source to other vertices


If the newly calculated distance is less than the previous do update
distance and path vertex


c) Find path lengths ( | V | - 1 ), from source to other vertices


If the newly calculated distance is less than the previous, do update
distance and path vertex

III) do Test for Negative length cycle by finding the path lengths | V |, from source to the
other vertices
If the newly calculated distance value is less than the previous distance value, then there
occurs a negative length cycle and return false
Otherwise there is no negative length cycle and return true.

Example 1:

1) Select the start vertex and perform 0-edge shortest distance from start vertex to other
vertices. i.e find the path lengths = 0, from source vertex to other vertices

50

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

2) Find the path lengths 1, from source vertex to the other vertices ( i.e find all the paths
have atmost 1 edge )

51

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

3) Find path lengths 2 from source vertex to other vertex ( i.e find all the paths have
atmost 2 edges )

52

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

4) Find path lengths 3 from source vertex to other vertex ( i.e find all the paths have
atmost 3 edges )

53

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5) Find path lengths 4 from source vertex to other vertex ( i.e find all the paths have
atmost 4 edges )

54

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Example 2:

1. Select the source or start vertex and perform 0-edge shortest distance from start vertex to
other vertices.
(i.e) find the path length = 0, from source vertex to other vertices

2. Find the path lengths 1, from source vertex to the other vertices ( i.e find all the paths
have atmost 1 edge)

55

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

find path length 2, from source to other vertices ( find all the paths have atmost 2 edges )

56

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

find path lengths 3, from source to other vertices (i.e) (find all the paths have atmost 3 edges )

57

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

find path lengths 4, from source to other vertices ( i.e find all the paths have almost 4 edges )

58

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

To test for negative length cycle, find path lengths 5

ds,v(B) = 0 + -1 = -1 ( no change )
ds,v(E) = -1 + 2 = 1 ( no change )
ds,v(D) = 1 + (-3) = -2 ( no change )
ds,v(B) = -2 + 1 = -1 ( no change )
ds,v(C) = -1 + 3 = 2 ( no change )
Negative length Cycles
Cycle c is a negative length cycle if sum of edge lengths is < 0.
If S can reach a negative length cycle C and cycle C can reach t then shortest path length
from S to t is -

Disadvantages of Bellman Ford Algorithm


1. It does not scale well.
2. Changes in network topology are not reflected quickly since updates are spread node-by-
node.

59

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

3. Count to infinity (if link or node failures render a node unreachable from some set of
other nodes, those nodes may spend forever gradually increasing their estimates of the
distance to it, and in the meantime there may be routing loops).

Advantages of Bellman-Ford Algorithm


1. Cost is minimized when building a network using Bellman-Ford Algorithm
2. Maximizes the performance of the system. Also finds minimum path weight
3. It allows splitting of traffic between several paths. It thus increases the system
performance.

Dijkstras Algorithm Bellman-Fords Algorithm

Dijkstra is a Greedy based algorithm Bellman-Ford is a Dynamic Programming based


algorithm
Dijkstra doesnt work for negative weight edges Bellman-Ford works for negative weight edges
Time complexity of Dijkstra is O(|E| + |V|Log|V|) complexity of Bellman Ford takes O(|V||E|)
Dijkstras algorithm is usually the working principle A distributed variant of the BellmanFord
behind link-state routing protocols, OSPF and IS-IS algorithm is used in distance-vector routing
protocols

5.9 Dynamic Programing


It is a technique for solving problems with overlapping subproblems. Typically, these sub
problems arise from recurrence relating a solution to a given problem with solutions to its smaller
subproblems of the same type.

Dynamic Programming suggests solving each smaller subproblem once and recording the results
in a table from which a solution to the original problem can be then obtained.

5.9.1 Warshalls Algorithm

It is for computing the transitive closure of a directed graph. The transitive


closure of a directed graph with n vertices can be defined as the n-by-n Boolean
matrix T = { tij }, in which the element in the ithrow ( 1 i n ) and the
jthcolumn ( 1 j n ) is 1. If there exists a non-trivial directed path ( is a directed
path of a positive length) from the ithvertex to the jthvertex, otherwise tij is 0.

60

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Note: 1) Use multiplication truth table to calculate new value for the current matrix

2) For Existing value(old value), refer previous matrix

61

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Transitive Closure

Warshalls algorithm constructs the transitive closure of a given digraph


with n vertices through a series of n-by-n Boolean matrices.
R(0),... , R(k-1), R(k), ... , R(n)

R(0)is just the adjacency matrix boxed row and column are used for getting R (1). Leave
the shaded box, use union rules to find the value and compare its with the existing value in the
previous adjacency matrix. If the existed value is said to be 1, leave the value as 1, even the new
value is said to be 0 or 1 if the existing value is 0 and the newly calculated value is 1, then update
the value as 1.

62

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

63

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

64

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

5.9.2Floyds algorithm for the All-pairs shortest-paths problem

Given a weighted connected graph ( undirected or directed), the all pairs shortest path
problem asks to find the distances (the lengths of the shortest paths) from each vertex to all other
vertices.

It is convenient to record the lengths of shortest paths in an n-by-n matrix D called the
distance matrix.

Distance matrix: The element dij in the ith row and the jth column of this matrix indicates the
length of the shortest path from the ith vertex to the jth vertex ( 1<= i, j <= n)

We can generate the distance matrix with an algorithm that is very similar to Warshall's
algorithm. It is called Floyd's algorithm, named after the inventor R.Floyd

65

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

It is applicable to both undirected and directed weighted graphs provided that tey do no
contain a cycle of a negative length.

Floyd's algorithm computes the distance matrix of a weighted graph with n vertices
through a series of n-by-n matrices.

D(0),...., D(k-1), D(k), . , D(n)

Each of these matrices contains the lengths of shortest paths with certain constraints on
the paths considered for the matrix. Specifically, the element dij(k) in the ith row and the jth
column of matrix D(k) (k = 0 ,1, n ) is equal to the lengths of the shortest path among all paths
from the ith vertex to the jth vertex with each intermediate vertex, if any, numbered not higher
than k. In particular, the series states with D(0), which does not allow any intermediate vertices in
iths path; hence D(0) is nothing but the weight matrix of the graph. The last matrix in the series,
D(n), contains the lengths of the shortest paths among all paths that can use all n vertices as
intermediate and hence is nothing but the distance matrix.

Floyds Algorithm

Implements Floyds algorithm for the all-pairs shortest-paths problem

Input: The weight matrix W of a graph

Output: The distance matrix of the shortest paths lengths

D W // is not necessary if W can be overwritten

for k 1 to n do

fori 1 to n do

66

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

for j 1 to n do

D[ i, j ] min { D[ i, j ], D[ i, k ] + D [ k, j ] }

67

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Note: Leave the shaded box and use the formula D[ i, j ] min { D[ i, j ], D[ i, k ] + D [ k, j ] }

68

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

69

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

70

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

71

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

72

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com

Advantages of Floyd-Warshall Algorithm:


1. Much easier to code.
2. You get more. All pairs of shortest paths are solved.

Disadvantages of Floyd-Warshall Algorithm:


1. Slower. O(V^3).
2. Harder to understand.

73

Get useful study materials from www.rejinpaul.com

You might also like