Module - 4 - Part2
Module - 4 - Part2
Since there are V vertices, we can cover all vertices with V pairs , hence
Since there are V vertices, we can cover all vertices with V pairs , hence
8 1 2 3 4
1 2
1 1 0 8 Inf 1
1
4 2 2 Inf 0 1 Inf
3 4 Inf 0 Inf
3 4
9 4 Inf 2 9 0
All pair shortest path {Floyd Warshall Algorithm}
Matrix D1 from D0
8 1 2 3 4
1 2
1 1 0 8 Inf 1
1
4 2 2 Inf 0 1 Inf
3 4 12 0 5
3 4
9 4 Inf 2 9 0
In D1 we consider now along with direct path, the path via Node 1 is also available.
All pair shortest path {Floyd Warshall Algorithm}
Matrix D2 from D1
8 1 2 3 4
1 2
1 1 0 8 9 1
1
4 2 2 Inf 0 1 Inf
3 4 12 0 5
3 4
9 4 Inf 2 3 (4 - 2 -1 ) 0
In D2 we consider now along with direct path, the path via Node 1 , Node 2 is also
available.
All pair shortest path {Floyd Warshall Algorithm}
Matrix D3 from D2
8 1 2 3 4
1 2
1 1 0 8 9 1
1
4 2 2 5 0 1 6 (2-3-1-4)
3 4 12 0 5
3 4
9
4 7 (4-2-3- 2 3 0
1)
In D3 we consider now along with direct path, the path via Node 1 , Node 2, Node3 is
also available.
All pair shortest path {Floyd Warshall Algorithm}
Matrix D4 from D3
8 1 2 3 4
1 2
1 1 0 3 (1-4-2) 4 (1-4-2- 1
1 3)
4 2
2 5 0 1 6
3 4 3 4 7 (3-1-4- 0 5
9 2)
4 7 2 3 0
In D4 we consider now along with direct path, the path via Node 1 , Node 2, Node3
and Node 4 is also available.
Time Complexity of All pair shortest path
Ak[i,j] = Ak-1[ i , j ] , Matrix size = V * V = V^2
Ak-1[ i , k ] + Ak-1[ k , j ] We need to solve the above sub-problem for
V matrices
Hence Time Complexity T(n) = V.V^2 = V^3
Space Complexity:
A3[2,1] =
A2[ 2 , 1 ] , If we use separate matrix for each D0 - Dn
then we require V+1 matrices, each of order
A2[ 2 , 3 ] + A2[ 3 , 1 ] V x V , hence space complexity is of order of
V^3.
Objective : To find cycle which incurs minimum cost while visiting all the
node only once and return back to starting node.
1 2 1 0 10 15 20
2 5 0 25 10
3 15 30 0 5
4 15 10 20 0
3 4
1 – 2 – 3 – 4 –1
1–2–4–3–1
1 – 4 – 3 – 2 –1