Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 88

SRI RAMAKRISHNA

INSTITUTE OF TECHNOLOGY

Programming and Data


Structures - II
UNIT – V

Representation of Graphs – Breadth-first search – Depth-first search – Topological sort – Minimum


Spanning Trees – Kruskal and Prim algorithm – Shortest path algorithm – Dijkstra‟s algorithm –
Bellman-Ford algorithm – Floyd-Warshall algorithm.

10/10/2020 R.Nagendran 1
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Graph Data Structure • Formally, a graph is a pair of sets (V,
• A graph is a pictorial representation of E), where V is the set of vertices and
a set of objects where some pairs of E is the set of edges, connecting the
objects are connected by links. The pairs of vertices. Take a look at the
interconnected objects are represented following grah −
by points termed as vertices, and the
links that connect the vertices are
called edges.

10/10/2020 R.Nagendran 2
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• In the above graph,


• V = {a, b, c, d, e}
• E = {ab, ac, bd, cd, de}

10/10/2020 R.Nagendran 3
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Graph Data Structure
• Mathematical graphs can be represented in data-structure. We can
represent a graph using an array of vertices and a two dimensional
array of edges. Before we proceed further, let's familiarize ourselves
with some important terms −
• Vertex − Each node of the graph is represented as a vertex. In
example given below, labeled circle represents vertices. So A to G
are vertices. We can represent them using an array as shown in
image below. Here A can be identified by index 0. B can be
identified using index 1 and so on.
• Edge − Edge represents a path between two vertices or a line
between two vertices. In example given below, lines from A to B, B
to C and so on represents edges. We can use a two dimensional array
to represent array as shown in image below. Here AB can be
represented as 1 at row 0, column 1, BC as 1 at row 1, column 2 and
so on, keeping other combinations as 0.
• Adjacency − Two node or vertices are adjacent if they are connected
to each other through an edge. In example given below, B is adjacent
to A, C is adjacent to B and so on.
• Path − Path represents a sequence of edges between two vertices. In
example given below, ABCD represents a path from A to D.
10/10/2020 R.Nagendran 4
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Depth First Traversal


• Depth First Search
algorithm(DFS) traverses a
graph in a depthward motion and
uses a stack to remember to get
the next vertex to start a search
when a dead end occurs in any
iteration.

10/10/2020 R.Nagendran 5
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• s in example given above, DFS algorithm
traverses from A to B to C to D first then
to E, then to F and lastly to G. It employs
following rules.
• Rule 1 − Visit adjacent unvisited vertex.
Mark it visited. Display it. Push it in a
stack.
• Rule 2 − If no adjacent vertex found, pop
up a vertex from stack. (It will pop up all
the vertices from the stack which do not
have adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until
stack is empty.

10/10/2020 R.Nagendran 6
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Step Traversal Description

1. Initialize the stack

Mark S as visited and put it onto the stack. Explore any


unvisited adjacent node from S. We have three nodes
2. and we can pick any of them. For this example, we
shall take the node in alphabetical order.

10/10/2020 R.Nagendran 7
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

Mark A as visited and put it onto the stack.


Explore any unvisited adjacent node from A.
3.
Both S and D are adjacent to A but we are
concerned for unvisited nodes only.

Visit D and mark it visited and put onto the stack.


Here we have B and C nodes which are adjacent to
4.
D and both are unvisited. But we shall again
choose in alphabetical order.

10/10/2020 R.Nagendran 8
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

We choose B, mark it visited and put onto stack.


5. Here B does not have any unvisited adjacent
node. So we pop B from the stack.

We check stack top for return to previous node and


6. check if it has any unvisited nodes. Here, we find D
to be on the top of stack.

10/10/2020 R.Nagendran 9
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

Only unvisited adjacent node is from D is C


7. now. So we visit C, mark it visited and put it
onto the stack.

As C does not have any unvisited adjacent node so we keep popping the stack until we find a node which has
unvisited adjacent node. In this case, there's none and we keep popping until stack is empty.

10/10/2020 R.Nagendran 10
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Breadth First Traversal
• Breadth First Search algorithm(BFS) traverses a
graph in a breadthwards motion and uses a
queue to remember to get the next vertex to start
a search when a dead end occurs in any iteration.
• As in example given above, BFS algorithm
traverses from A to B to E to F first then to C
and G lastly to D. It employs following rules.
• Rule 1 − Visit adjacent unvisited vertex. Mark it
visited. Display it. Insert it in a queue.
• Rule 2 − If no adjacent vertex found, remove the
first vertex from queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until queue is
empty.

10/10/2020 R.Nagendran 11
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Step Traversal Description

1.

Initialize the queue

2.

We start from visiting S (starting


node), and mark it visited.

10/10/2020 R.Nagendran 12
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

We then see unvisited adjacent node from S. In


this example, we have three nodes but
3.
alphabetically we choose A mark it visited and
enqueue it.

4. Next unvisited adjacent node from S is B. We


mark it visited and enqueue it.

10/10/2020 R.Nagendran 13
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

Next unvisited adjacent node from S is C.


5.
We mark it visited and enqueue it.

Now S is left with no unvisited adjacent


6.
nodes. So we dequeue and find A.

10/10/2020 R.Nagendran 14
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

7. From A we have D as
unvisited adjacent node. We
mark it visited and enqueue it.

10/10/2020 R.Nagendran 15
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Minimum Spanning Trees
• Problem: Laying Telephone Wire

Central office

10/10/2020 R.Nagendran 16
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Wiring: Naive Approach

Central office

Expensive!
10/10/2020 R.Nagendran 17
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Wiring: Better Approach

Central office

Minimize the total length of wire connecting the customers


10/10/2020 R.Nagendran 18
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Minimum-cost spanning trees
• Suppose you have a connected undirected graph with a weight (or cost)
associated with each edge
• The cost of a spanning tree would be the sum of the costs of its edges
• A minimum-cost spanning tree is a spanning tree that has the lowest cost.
16 16
A B A B
21 11 6 11 6

19 5 5
F C F C
33 14
10
E 18 D E 18 D
A connected, undirected graph A minimum-cost spanning tree
10/10/2020 R.Nagendran 19
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Minimum Spanning Tree (MST)
A minimum spanning tree is a subgraph of an undirected weighted graph G,
such that
• it is a tree (i.e., it is acyclic)
• it covers all the vertices V
• contains |V| - 1 edges
• the total cost associated with tree edges is the minimum among all
possible spanning trees
• not necessarily unique

10/10/2020 R.Nagendran 20
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Applications of MST
• Any time you want to visit all vertices in a graph at minimum cost (e.g., wire routing on
printed circuit boards, sewer pipe layout, road planning…)

• Internet content distribution


• $$$, also a hot research topic
• Idea: publisher produces web pages, content distribution network replicates web pages to many locations so
consumers can access at higher speed
• MST may not be good enough!
• content distribution on minimum cost tree may take a long time!

• Provides a heuristic for traveling salesman problems. The optimum traveling salesman tour is at
most twice the length of the minimum spanning tree (why??)
10/10/2020 R.Nagendran 21
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• How Can We Generate a MST?

9 b 9 b
a 2 6 a 2 6
d d
4 5 4 5
5 4 5 4

5 e 5 e
c c

10/10/2020 R.Nagendran 22
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Prim(-Jarnik)’s Algorithm
• Similar to Dijkstra’s algorithm (for a connected graph)
• We pick an arbitrary vertex s and we grow the MST as a cloud of vertices, starting from s
• We store with each vertex v a label d(v) = the smallest weight of an edge connecting v to
a vertex in the cloud.

At each step:
 We add to the cloud the vertex u outside the cloud with
the smallest distance label
 We update the labels of the vertices adjacent to u

10/10/2020 R.Nagendran 23
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Prim’s algorithm
T = a spanning tree containing a single node s;
E = set of edges adjacent to s;
while T does not contain all the nodes
{
remove an edge (v, w) of lowest cost from E
if w is already in T then discard edge (v, w)
else {
add edge (v, w) and node w to T
add to E the edges adjacent to w
}
}
• An edge of lowest cost can be found with a priority queue
• Testing for a cycle is automatic
• Hence, Prim’s algorithm is far simpler to implement than Kruskal’s algorithm (presented below)
10/10/2020 R.Nagendran 24
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Example  7
7 D 7 D
2 2
B 4 B 4
8 9  5 9 
2 5 F 2 5 F
C C
8 8
8 3 8 3
E E
A 7 A 7
0 7 0 7

7 7
7 D D
2 2 7
B 4 B 4
5 9  9 4
2 5 F 5 5
C 2 F
8 C
3 8
8 8 3
E E
A 7 A
0 7 7 7
0
10/10/2020 R.Nagendran 25
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

7
7 D
2
B 4
5 9 4
2 5 F
C
8
8 3
E
A 3 7
0 7
7 D
2
B 4
5 9 4
2 5 F
C
8
8 3
E
A 3
0 7

10/10/2020 R.Nagendran 26
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Vertex Parent
e -
e d b c a b -
0     c -
9 b
d -
a 2 6
d
4 5 Vertex Parent
5 4 e -
d b c a
5 e b e
c 4 5 5  c e
d e

The MST initially consists of the vertex e, and we update


the distances and parent for its adjacent vertices

10/10/2020 R.Nagendran 27
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Vertex Parent
e -
d b c a
b e
4 5 5  c e
9 b d e
a 2 6
d
4 5
5 4
Vertex Parent
5 e
c e -
a c b
b e
2 4 5 c d
d e
a d

10/10/2020 R.Nagendran 28
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Vertex Parent
e -
a c b
b e
2 4 5 c d
9 b d e
a 2 6 a d
d
4 5
5 4
5 e
c Vertex Parent
e -
c b
b e
4 5 c d
d e
a d
10/10/2020 R.Nagendran 29
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Vertex Parent
e -
c b
b e
9 4 5 c d
b
d e
a 2 6
d a d
4 5
5 4

5 e Vertex Parent
c e -
b
b e
5 c d
d e
a d

10/10/2020 R.Nagendran 30
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Vertex Parent
e -
b
b e
5 c d
9 b d e
a 2 6 a d
d
4 5
5 4

5 e
c Vertex Parent
e -
b e
The final minimum spanning tree c d
d e
a d
10/10/2020 R.Nagendran 31
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Prim’s Algorithm Invariant


• At each step, we add the edge (u,v) s.t. the weight of (u,v) is minimum
among all edges where u is in the tree and v is not in the tree

• Each step maintains a minimum spanning tree of the vertices that have
been included thus far

• When all vertices have been included, we have a MST for the graph!
•3
10/10/2020 R.Nagendran 32
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Correctness of Prim’s
• This algorithm adds n-1 edges without creating a cycle, so clearly it creates a
spanning tree of any connected graph (you should be able to prove this).

But is this a minimum spanning tree?


Suppose it wasn't.

• There must be point at which it fails, and in particular there must a single edge
whose insertion first prevented the spanning tree from being a minimum
spanning tree.
10/10/2020 R.Nagendran 33
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Correctness of Prim’s
• Let G be a connected, undirected
graph
• Let S be the set of edges chosen by
Prim’s algorithm before choosing an
errorful edge (x,y) x
• Let V(S) be the vertices incident with y
edges in S
• Let T be a MST of G containing all
edges in S, but not (x,y).
10/10/2020 R.Nagendran 34
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Correctness of Prim’s
• Edge (x,y) is not in T, so there must be
a path in T from x to y since T is
connected.
• Inserting edge (x,y) into T will create a
cycle x
• There is exactly one edge on this cycle y
with exactly one vertex in V(S), call
this edge (v,w)

10/10/2020 R.Nagendran 35
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Correctness of Prim’s
• Since Prim’s chose (x,y) over (v,w), w(v,w) >= w(x,y).
• We could form a new spanning tree T’ by swapping (x,y) for (v,w) in T
(prove this is a spanning tree).
• w(T’) is clearly no greater than w(T)
• But that means T’ is a MST
• And yet it contains all the edges in S, and also (x,y)

...Contradiction
10/10/2020 R.Nagendran 36
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Another Approach
• Create a forest of trees from the vertices
• Repeatedly merge trees by adding “safe edges” until only one tree remains
• A “safe edge” is an edge of minimum weight which does not create a cycle
forest: {a}, {b}, {c}, {d}, {e}
9
b
a 2 6
d
4 5
5 4

5 e
c
10/10/2020 R.Nagendran 37
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Kruskal’s algorithm
T = empty spanning tree;
E = set of edges;
N = number of nodes in graph;
while T has fewer than N - 1 edges
{
remove an edge (v, w) of lowest cost from E
if adding (v, w) to T would create a cycle
then discard (v, w)
else add (v, w) to T
}
• Finding an edge of lowest cost can be done just by sorting the edges
• Testing for a cycle: Efficient testing for a cycle requires a additional algorithm (UNION-
FIND) which we don’t cover in this course. The main idea: If both nodes v, w are in the same
component of T, then adding (v, w) to T would result in a cycle.
10/10/2020 R.Nagendran 38
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Kruskal Example
2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
10/10/2020 R.Nagendran 39
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 40
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 41
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 42
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 43
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 44
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 45
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 46
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 47
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 48
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 49
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 50
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 51
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

10/10/2020 R.Nagendran 52
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Time Compexity
• Let v be number of vertices and e the number of edges of a given
graph.
• Kruskal’s algorithm: O(e log e)
• Prim’s algorithm: O( e log v)
• Kruskal’s algorithm is preferable on sparse graphs, i.e., where e is
very small compared to the total number of possible edges: C(v, 2) =
v(v-1)/2.

10/10/2020 R.Nagendran 53
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Dijkstra’s Algorithm
• Single Source Multiple Destination Shortest Path Algorithm
• Works with directed and undirected graphs
• Works with weighted and unweighted graphs
• Rare type of algorithm 
A greedy algorithm that produces an optimal solution

10/10/2020 R.Nagendran 54
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Initialize array
3
5 F C K dv pv
10
A 7 3 A F  
8 4
18 B F  
4
B D
9 C F  
10
H D F  
2 9 25
3 E F  
G E
7 F F  
G F  
H F  

10/10/2020 R.Nagendran 55
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Start with G
3
5 F C K dv pv
10 A
A 7 3
8 4
18 B
4
B D C
9
10
H D
2 9 25
3 E
G E
7 F
G T 0 
H

10/10/2020 R.Nagendran 56
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Update unselected
3
5 F C nodes K d p
v v

10 A
A 7 3
8 4
18 B
4
B D C
9
10
H D 2 G
2 9 25
3 E
G E
7 F
G T 0 
H 3 G

10/10/2020 R.Nagendran 57
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Select minimum distance
3
5 F C K dv pv
10 A
A 7 3
8 4
18 B
4
B D C
9
10
H D T 2 G
2 9 25
3 E
G E
7 F
G T 0 
H 3 G

10/10/2020 R.Nagendran 58
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Update unselected nodes
3
5 F C K dv pv
10 A
A 7 3
8 4
18 B
4
B D C
9
10
H D T 2 G
2 9 25
3 E 27 D
G E
7 F 20 D
G T 0 
H 3 G

10/10/2020 R.Nagendran 59
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Select minimum distance
3
5 F C K dv pv
10 A
A 7 3
8 4
18 B
4
B D C
9
10
H D T 2 G
2 9 25
3 E 27 D
G E
7 F 20 D
G T 0 
H T 3 G

10/10/2020 R.Nagendran 60
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Update unselected nodes
3
5 F C K dv pv
10 A 7 H
A 7 3
8 4
18 B 12 H
4
B D C
9
10
H D T 2 G
2 9 25
3 E 27 D
G E
7 F 20 D
G T 0 
H T 3 G

10/10/2020 R.Nagendran 61
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Select minimum distance
3
5 F C K dv pv
10 A T 7 H
A 7 3
8 4
18 B 12 H
4
B D C
9
10
H D T 2 G
2 9 25
3 E 27 D
G E
7 F 20 D
G T 0 
H T 3 G

10/10/2020 R.Nagendran 62
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Update unselected nodes
3
5 F C K dv pv
10 A T 7 H
A 7 3
8 4
18 B 12 H
4
B D C
9
10
H D T 2 G
2 9 25
3 E 27 D
G E
7 F 17 A
G T 0 
H T 3 G

10/10/2020 R.Nagendran 63
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Select minimum distance
3
5 F C K dv pv
10 A T 7 H
A 7 3
8 4
18 B T 12 H
4
B D C
9
10
H D T 2 G
2 9 25
3 E 27 D
G E
7 F 17 A
G T 0 
H T 3 G

10/10/2020 R.Nagendran 64
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Update unselected nodes
3
5 F C K dv pv
10 A T 7 H
A 7 3
8 4
18 B T 12 H
4
B D C 16 B
9
10
H D T 2 G
2 9 25
3 E 22 B
G E
7 F 17 A
G T 0 
H T 3 G

10/10/2020 R.Nagendran 65
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Select minimum distance
3
5 F C K dv pv
10 A T 7 H
A 7 3
8 4
18 B T 12 H
4
B D C T 16 B
9
10
H D T 2 G
2 9 25
3 E 22 B
G E
7 F 17 A
G T 0 
H T 3 G

10/10/2020 R.Nagendran 66
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Update unselected nodes
3
5 F C K dv pv
10 A T 7 H
A 7 3
8 4
18 B T 12 H
4
B D C T 16 B
9
10
H D T 2 G
2 9 25
3 E 22 B
G E
7 F 17 A
G T 0 
H T 3 G

10/10/2020 R.Nagendran 67
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Select minimum distance
3
5 F C K dv pv
10 A T 7 H
A 7 3
8 4
18 B T 12 H
4
B D C T 16 B
9
10
H D T 2 G
2 9 25
3 E 22 B
G E
7 F T 17 A
G T 0 
H T 3 G

10/10/2020 R.Nagendran 68
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Update unselected nodes
3
5 F C K dv pv
10 A T 7 H
A 7
8 4
18 B T 12 H
4
B D C T 16 B
9
10
H D T 2 G
2 9 25
3 E 19 F
G E
7 F T 17 A
G T 0 
H T 3 G

10/10/2020 R.Nagendran 69
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
2
Select minimum distance
3
5 F C K dv pv
10 A T 7 H
A 7
8 4
18 B T 12 H
4
B D C T 16 B
9
10
H D T 2 G
2 9 25
3 E T 19 F
G E
7 F T 17 A
G T 0 
H T 3 G

Done

10/10/2020 R.Nagendran 70
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Order of Complexity
• Analysis
• findMin() takes O(V) time
• outer loop iterates (V-1) times
 O(V2) time
• Optimal for dense graphs, i.e., |E| = O(V2)
• Suboptimal for sparse graphs, i.e., |E| = O(V)
• If the graph is sparse, i.e., |E| = O(V)
• maintain distances in a priority queue
• insert new (shorter) distance produced by line 10 of Figure 9.32
 O(|E| log |V|) complexity

10/10/2020 R.Nagendran 71
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• Bellman-Ford Example
π: nil π: nil π: nil A
π: nil d: 0
d: 0 d: ∞ 2 d: ∞ -1
2
A B A B
-1 -1

π: nil π: nil C
2 3 2 d: ∞ 2 3
d: ∞
-3 -3
4 C 4 C
5 3 5 3
-1 -1
D 4 E D E
4
π: nil π: nil π: nil A π: nil
d: ∞ d: ∞ d: ∞ 4 d: ∞
10/10/2020 R.Nagendran 72
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

π: π: nil A π: π: nil A
nil 2 d: ∞ -1 nil 2 d: ∞ -1
d: 0 A B d: 0 A B
-1 -1
π: nil C π: nil C
2 3 2 3
d: ∞ 2 d: ∞ 2
4 -3 -3
C 4 C
5 3 5 3
-1 -1
D 4 E
D 4 E
π: nil π: nil C
π: nil A π: nil C
A d: ∞ 3
d: ∞ 4 d: ∞ 3
d: ∞ 4

10/10/2020 R.Nagendran 73
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

π:
nil 2
d: 0 A B
-1
π: nil C
2 3
d: ∞ 2
-3
4 C
5 3
-1
D 4 E
π: nil A π: nil C
d: ∞ 4 d: ∞ 3

10/10/2020 R.Nagendran 74
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Bellman-Ford Example -G contains a negative weight cycle
(iterations from E to A)

π: π:
nil π: nil π: nil A
2 nil d: 0
d: 0 A B d: ∞ 2 d: ∞ -1
A B
-1 -1
π: π: nil C
2 1 2 1
nil d: ∞ 2
d: C∞ -3 -3
4 4 C

5 1 5 1
-1 -1
D 4 E
D 4 E π: nil A π: nil
π: π: d: ∞ 4 d: ∞
nil nil
d: ∞ d: ∞
10/10/2020 R.Nagendran 75
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

π: π: nil A π: π: nil A
nil 2 d: ∞ -1 nil 2 d: ∞ -1
d: 0 A B d: 0 A B
-1 -1
π: nil C π: nil C
2 1 2 1
B B
-3 -3
4 d:C∞ 2 4 d:C∞ 2
0 0
5 1 5 1
-1 -1
D 4 E
D 4 E
π: nil π: nil
π: nil π: nil C C
A C
A d: ∞ 3 1
d: ∞ 4 d: ∞
d: ∞ 4
3
10/10/2020 R.Nagendran 76
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

π: nil π: nil A E
d: 0 2 d: ∞ -1 -2
A B
-1
π: nil C B B
2 1
d: ∞ 2 0 -1
-3
4 C
5 1
-1
D 4 E
π: nil A π: nil C C
d: ∞ 4 d: ∞ 3 1

10/10/2020 R.Nagendran 77
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Floyd-Warshall algorithm for shortest path:


• Use a different dynamic-programming formulation to solve the all-
pairs shortest-paths problem on a directed graph G=(V,E).
• The resulting algorithm, known as the Floyd-Warshall algorithm, runs
in O (V3) time.
• negative-weight edges may be present,
• but we shall assume that there are no negative-weight cycles.

10/10/2020 R.Nagendran 78
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• The structure of a shortest path:
• We use a different characterization of the structure of a shortest path than we used in the
matrix-multiplication-based all-pairs algorithms.
• The algorithm considers the “intermediate” vertices of a shortest path, where an
intermediate vertex of a simple path p=<v1,v2,…,vl> is any vertex in p other than v1 or vl,
that is, any vertex in the set {v2,v3,…,vl-1}
• Let the vertices of G be V={1,2,…,n}, and consider a subset {1,2,…,k} of vertices for
some k.
• For any pair of vertices i,j  V, consider all paths from i to j whose intermediate vertices
are all drawn from {1,2,…,k},and let p be a minimum-weight path from among them.
• 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}.

10/10/2020 R.Nagendran 79
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• The relationship depends on whether or not k is an intermediate vertex


of path p.
• 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, a shortest path
from vertex i to vertex j with all intermediate vertices in the set {1,2,
…,k-1} is also a shortest path from i to j with all intermediate vertices
in the set {1,2,…,k}.
• If k is an intermediate vertex of path p,then we break p down into i
k j as shown Figure 2.p1 is a shortest path from i to k
with all intermediate vertices in the set {1,2,…,k-1}, so as p2.

10/10/2020 R.Nagendran 80
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• All intermediate vertices in {1,2,…,k-1}
p1 p2
k

i j

P:all intermediate vertices in {1,2,…,k}

• Figure 2. Path p is a shortest path from vertex i to vertex j,and


• k is the highest-numbered intermediate vertex of p. Path p1,
• the portion of path p from vertex i to vertex k,has all intermediate
• vertices in the set {1,2,…,k-1}.The same holds for path p2 from
• vertex k to vertex j.

10/10/2020 R.Nagendran 81
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

• Let dij(k) be the weight of a shortest path from vertex i to vertex j with
all intermediate vertices in the set {1,2,…,k}. 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.

• The matrix D(n)=(dij(n)) gives the final answer-dij(n)= for all i,j
V-because all intermediate vertices are in the set {1,2,…,n}.

10/10/2020 R.Nagendran 82
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
• FLOYD-WARSHALL(W)
•n rows[W]
• D(0) W
• for k 1 to n
• do for i 1 to n
• do for j 1 to n
• dij(k) min(dij(k-1),dik(k-1)+dkj(k-1))

• return D(n)
10/10/2020 R.Nagendran 83
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

Figure 3
2
4
3

1 3
8

1
-4 -5
7 2

5 4
6

10/10/2020 R.Nagendran 84
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

Figure 3
2
4
3

1 3
8

1
-4 -5
7 2

5 4
6

10/10/2020 R.Nagendran 85
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

0 3 8   4  NIL 1 1 NIL 1 
   
 0  1 7   NIL NIL NIL 2 2 
    NIL 
D(0)=  4 0 
  (0)=  NIL 3 NIL NIL

2  5 0    4 NIL 4 NIL NIL 
 0   NIL
   6  NIL NIL 5 NIL 

0 3 8   4  NIL 1 1 NIL 1 
   
 0  1 7 
 NIL NIL NIL 2 2 
  
D(1)= 
4 0 
  (1)=  NIL 3 NIL NIL NIL 

2 5 5 0  2  4 1 4 NIL 1 
 0   NIL
   6
 NIL NIL 5 NIL 

10/10/2020 R.Nagendran 86 86
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

0 3 8 4  4  NIL 1 1 2 1 
   
 0  1 7   NIL NIL NIL 2 2 
 11   NIL 2 
D(2)= 
4 0 5
  (2)=  3 NIL 2

2 5 5 0  2  4 1 4 NIL 1 
 0   NIL
   6  NIL NIL 5 NIL 

0 3 8 4  4  NIL 1 1 2 1 
   
 0  1 7 
 NIL NIL NIL 2 2 
 11 
D(3)= 
4 0 5
  (3)=  NIL 3 NIL 2 2 

2 1 5 0  2
  4 3 4 NIL 1 
  0   NIL NIL 
 6
 NIL NIL 5

10/10/2020 R.Nagendran 87
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY

0 3 1 4  4  NIL 1 4 2 1 
   
3 0 4 1 1   4 NIL 4 2 1 
7 3   
D(4)=  4 0 5
  (4)=  4 3 NIL 2 1

2 1 5 0  2  4 3 4 NIL 1 
8 0   4
 5 1 6  3 4 5 NIL 

0 1 3 2  4  NIL 3 4 5 1 
   
3 0 4 1 1 
 4 NIL 4 2 1 
7 3 
D(5)= 
4 0 5
  (5)=  4 3 NIL 2 1 

2 1 5 0  2
   4 3 4 NIL 1 
8 5 1 6 0   4 3 4 5 NIL 

10/10/2020 R.Nagendran 88

You might also like