Professional Documents
Culture Documents
Single Pair Shortest Path Problem
Single Pair Shortest Path Problem
MD Tanvir Anwoar
Introduction: In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized. This is analogous to the problem of finding the shortest path between two intersections on a road map: the graph's vertices correspond to intersections and the edges correspond to road segments, each weighted by the length of its road segment.
Application: * The traffic and length of the highways are path weights. * Vehicle routing problem solving * Solving network design problem * In video games, these algorithms are frequently used to find the shortest path between two points on a map Purpose of the problem solving: * Improve Quality of the service, Reducing time and cost
Problem solving methods : Bellman-Ford Algorithm: a. If some edge weights are negative b. It has complexity O(nm) for a single source c. Total sources solution is O(n^2 m), which is O(n4) for dense graphs
Floyd-Warshall Algorithm 1. Graph should be directed 2. It may contain negative edges but no negative cycle 3. Find the shortest path between all the pairs of nodes
Floyd-Warshall Algorithm 1. Graph should be directed 2. It may contain negative edges but no negative cycle 3. Find the shortest path between all the pairs of nodes
-2 A B
-1
1 1,4 =
1 1,4 =
1 ,2,4
1 1,4 =
1 ,2,4 ; 1,4
(k) (I,j) =
{ min (D if k>= 1
(k-1)
(I,j)),
(k-1) (I,k )
+D
(k-1) (k,j)
Floyd-Warshall Algorithm
matrix formation :
3 1
-4
7 5
8 3
0 X X 2 X
3 0 4 X X
D(0) 8 X X 1 0 X -5 1 X 0
-4 7 -4 7 0
2
6 4 -5
Floyd-Warshall Algorithm
matrix formation :
3 1
-4
7 5 2 6 4
8 3
0 X X 2 X
3 0 4 X X
D(0) 8 X X 11 0 X -5 1 X 0
-4 7 -4 7 0
-5
(0) (2,4)
=1
Floyd-Warshall Algorithm
matrix formation :
3 1
-4
7 5 2 6 4
8 3
0 X X 2 X
3 0 4 5 X
D(1) 8 X X 1 0 X -5 0 X 6
-4 7 X -2 -2 0
Adjacency Matrix -5
(1) (4,5)
= -2
Floyd-Warshall Algorithm
matrix formation :
3 1
-4
7 5 2 6 4
8 3
0 3 7 2 8
1 0 4 -1 5
D(5) -3 2 -4 1 -4 0 5 -5 0 1 6
-4 -1 3 -2 0
Adjacency Matrix -5
(5) (2,3)
= -4
Floyd-Warshall Algorithm
Pseudocode: 1. For i=1 to |V| do 2. For j=1 to |V| do 3. S[i,j,0] = w(i,j) 4. For k=1 to |V| do 5. For i=1 to |V| do 6. For j=1 to |V| do 7. S[i,j,k] = min { 8. S[i,j,k-1], 9. S[i,k,k-1]+S[k,j,k-1] } 10. Return S[:,:,n] # return 2d array with n = |V|
Floyd-Warshall Algorithm