0% found this document useful (0 votes)
14 views22 pages

09 Shortest Path

Uploaded by

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

09 Shortest Path

Uploaded by

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

Shortest Path Problems

In Graph
Shortest Path in a Graph

• Shortest path (and distance) from a specific


node to all other nodes in graph
– Dijkstra Algorithm
– Bellman Ford Algorithm
Shortest Path in a Graph

• Shortest path (and distance) from a specific


node to all other nodes in graph
– Dijkstra Algorithm(does not work for negative
weighted graph)
– Bellman Ford Algorithm(work for negative
weighted graph but not for cycle)
– Shortest path and distance from every node to
all other nodes
– Floyd Warshall Algorithm
Bellman Ford Algorithm

• Single Source Shortest Path Algorithm


• Works for Directed and Undirected graphs
• Can be used in a graph with negative edge
weights, unlike Dijkstra Algorithm
• Goes through all nodes and edges, without
specifically looking for minimum distances
Example

A
16 6

10
5 2
B C

-2
8
3 12

E F
14
Bellman Ford Algorithm
Input: Graph G (V, E), Source Vertex: S
Output: Distance [V × V ], Predec [V × V ]

Procedure Bellman-Ford (G)


Repeat for all vertices v in V
Set Dist[v] := INFINITY
Set Predec[v] := NULL
End Repeat

Set Dist[s] := 0
Repeat for all vertices v in V
Repeat for all adjacent nodes t of v
If Dist[t] > Dist[v]+ wt(v,t)
Set Dist[t] := Dist[v]+ wt(v,t)
Set Predec[t] := v
End If
End Repeat
End Repeat
End
Analysis of Algorithm

• The Algorithm goes to all nodes and all edges


of G
• Hence, Growth Factor is O(V.E)
Floyd Warshall Algorithm
• Algorithm to derive All Pair Shortest Path for a
graph
• Extension of Bellman Ford – repeated for all
nodes as Source Nodes
• Distance calculation stored in a 2-d Array,
similar to Adjacency Matrix
• Distances initialized as:
– Dist [x, y] = 0 if x = y
– Dist [x, y] = wt (x, y) if edge (x, y) exists
– Dist [x,y] = ∞ if edge (x, y) does not exist
Floyd Warshall Algorithm
Input: Graph G (V, E)
Output: Distance [V × V ], Predec [V × V ]

Procedure Floyd-Warshall(G)
Initialize Dist [V, V] // Initialize from Adj Matrix
Initialize Predec [V, V] // Initialize predecessors

Repeat for k = 1 to V step 1


Repeat for x = 1 to V step 1
Repeat for y = 1 to V step 1
If Dist[x,y] > Dist[x,k]+ Dist[k,y]
Set Dist[x,y] := Dist[x,k]+ Dist[k,y]
Set Predec [x,y] := k
End If
End Repeat
End Repeat
End Repeat
End
Floyd Warshall Example
Initial Distance Matrix

k=0
4
A B
A B C D
7
A 0 999 9 999
B 4 0 999 999
9

C 999 6 0 2
6

C D D 7 999 999 0
2
Floyd Warshall Example
Distance Matrix

k=1
4
A B
A B C D
A 0 999 9 999
7

B 4 0 13 999
9

C 999 6 0 2
C
2
D D 7 999 16 0

Dist [2,3] = ∞, but

Dist [2,1] = 4, Dist [1,3] = 9

Hence, Dist [2,3] = 4+9 = 13


Floyd Warshall Example
Distance Matrix

k=2
4
A B
A B C D
A 0 999 9 999
7

B 4 0 13 999
9

C 10 6 0 2
C
2
D D 7 999 16 0

Dist [3,1] = ∞, but

Dist [3,2] = 6, Dist [2,1] = 4

Hence, Dist [3,1] = 6+4 = 10


Floyd Warshall Example
Distance Matrix

k=3
4
A B
A B C D
A 0 15 9 11
7

B 4 0 13 15
9

C 10 6 0 2
C
2
D D 7 22 16 0

Dist [2,4] = ∞, but

Dist [2,3] = 13, Dist [3,4] = 2

Hence, Dist [2,4]=13+2 = 15


Floyd Warshall Example
Distance Matrix

k=4
4
A B
A B C D
A 0 15 9 11
7

B 4 0 13 15
9

C 9 6 0 2
C
2
D D 7 22 16 0

Dist [3,1] = 10, but

Dist [3,4] = 2, Dist [4,1] = 7

Hence, Dist [3,1]=2+7 = 9


Floyd Warshall Example
Final Result
4
A B
A B C D
A 0 15 9 11
7

B 4 0 13 15
9

C 9 6 0 2
C
2
D D 7 22 16 0
Analysis of Algorithm

• Floyd Warshall calculates distances between


each pair of node [x, y] via all other nodes as
intermediate nodes (k)
• Dynamic Programming technique – uses a
temporary space of size (V x V)
• Number of calculation for each cell : V (max)
• Hence, Algorithm Growth Factor is O (V3)
Transitive Closure
• Transitive Closure of a graph is used to
determine whether every node of a graph can
be reached from all other nodes
• Very similar to Floyd Warshall
• Distances initialized as:
– Dist [x, y] = 1 if x = y
– Dist [x, y] = 1 if edge (x, y) exists
– Dist [x,y] = 0 if edge (x, y) does not exist
Transitive Closure Algorithm
Input: Graph G (V, E)
Output: Distance [V × V]

Procedure Transitive-Closure(G)
Initialize Dist [V, V] // Initialize from Adj Matrix

Repeat for k = 1 to V step 1


Repeat for x = 1 to V step 1
Repeat for y = 1 to V step 1
If Dist[x,y] = 0
If Dist[x,k] = 1 and Dist[k,y] = 1
Set Dist[x,y] := 1
End If
End Repeat
End Repeat
End Repeat
End
Transitive Closure Example
Initial Distance Matrix

A B
A B C D
A 1 0 1 0
B 1 1 0 0
C 0 1 1 1
C D D 1 0 0 1
Transitive Closure Example
Final Distance Matrix

A B
A B C D
A 1 1 1 1
B 1 1 1 1
C 1 1 1 1
C D D 1 1 1 1
Algorithm Types

• Greedy Algorithms:
– Kruskal
– Prim
– Dijkstra
• Dynamic Programming:
– Bellman Ford
– Floyd Warshall
The End

You might also like