Discrete Math 2 Shortest Paths Using Matrix 2001 Discrete Math 2 Shortest Paths Using Matrix CIS112 February 14, 2007 Daniel L. Silver
Overview Previously: In weighted graph . . Shortest path from #7 to all others Search matrix method Now: Problem 8.6.2 Implement Floyd’s Algorithm 2007 Kutztown University
Strategy 3 nested loops i ≔ 1 to 6 j ≔ 1 to 6 k ≔ 1 to 6 Basic rule If d(j,i) + d(i,k) < d(j,k) . . Then d(j,k) d(j,i) + d(i,k) 2007 Kutztown University
Interpretation of Matrix Row designates from vertices Column designates to vertices Suppose entry [2,3] is 7 From vertex #2 to vertex #3 . . cost of travel = 7 2007 Kutztown University
Seek – best cost route from j to k The Basic Operation Seek – best cost route from j to k Initial entries = cost of direct route, i.e., cost of edge jk No edge cost = ∞ Guarantees that any route found is better 2007 Kutztown University
Old cost from j to k compared to . . Cost of 2 step hop The Basic Operation Old cost from j to k compared to . . Cost of 2 step hop j to i and then i to k If 2 step hop has better cost . . Then it becomes the new j to k cost 2007 Kutztown University
Initially old cost is edge cost Later . . The Basic Operation Initially old cost is edge cost Later . . Old cost is cost of best route found so far 2007 Kutztown University
Denotations Outer loop – the i loop Middle loop – the j loop Inner loop – the k loop 2007 Kutztown University
Operation Proceed Loops work in tandem 2 outer loops {i & j loops} 2 inner loops {j & k loops} Proceed row by row & column by column Update travel cost . . from vertex j to vertex k 2007 Kutztown University
Matrix for Weighted Graph Vtx A B C D E Z ∞ 2 3 5 1 4 2007 Kutztown University
First Outer Loop i = 1 j = ≔ 1 to 6 k ≔ 1 to 6 2007 Kutztown University
First Inner Loop i = 1 j = 1 k ≔ 1 to 6 2007 Kutztown University
First Middle Loop d(1,1) = ∞ d(1,1) + d(1,k) ≮ d(1,k), ∀k Therefore, no change 2007 Kutztown University
Second Middle Loop i = 1 j = 2 k ≔ 1 to 6 2007 Kutztown University
Second Middle Loop k = 1 d(1,1) = ∞ d(2,1) + d(1,1) = ∞ Therefore, no change 2007 Kutztown University
i=1; j=2; k = 2 (2,1) + (1,2) < (2,2) ≡ 4 < ∞ Vtx A B C D E Z ∞ 2 3 4 5 1 2007 Kutztown University
i=1; j=2; k = 3 (2,1) + (1, 3) < (2,3) ≡ 5 < ∞ Vtx A B C D E Z ∞ 2 3 4 5 1 2007 Kutztown University
i=1; j=2; k ≔ 4,5,6 (2,1) + (1,k) < (2,k) ≡ ∞ ≮ x Vtx A B C D E Z ∞ 2 3 4 5 1 2007 Kutztown University
Third Middle Loop i = 1 j = 3 k ≔ 1 to 6 2007 Kutztown University
Third Middle Loop k = 1 d(1,1) = ∞ d(3,1) + d(1,1) = ∞ Therefore, no change 2007 Kutztown University
i=1; j=3; k = 2 (3,1) + (1, 2) < (3,2) ≡ 5 < ∞ Vtx A B C D E Z ∞ 2 3 4 5 1 2007 Kutztown University
i=1; j=3; k = 3 (3,1) + (1, 3) < (3,3) ≡ 6 < ∞ Vtx A B C D E Z ∞ 2 3 4 5 6 1 2007 Kutztown University
i=1; j=3; k ≔ 4,5,6 (3,1) + (1, k) < (3,k) ≡ ∞ ≮ x Vtx A B C D E Z ∞ 2 3 4 5 6 1 2007 Kutztown University
4th, 5th & 6th Middle Loops i = 1 j ≔ 4 to 6 k ≔ 1 to 6 2007 Kutztown University
4th, 5th & 6th Middle Loops j ≔ 4 to 6 d(j,1) = ∞ d(j,1) + d(i,k) = ∞ d(j,1) + d(i,1) = ∞ ≮ d(j,k) Therefore, no change 2007 Kutztown University
Second Outer Loop i = 2 j = ≔ 1 to 6 k ≔ 1 to 6 2007 Kutztown University
i=2; j=1; k = 1 (1,2) + (2, 1) < (1,1) ≡ 4 < ∞ Vtx A B C D E Z 4 2 3 ∞ 5 6 1 2007 Kutztown University
i=2; j=1; k = 2 (1,2) + (2, 2) < (1,2) ≡ 6 ≮ 2 Vtx A B C D E Z 4 2 3 ∞ 5 6 1 2007 Kutztown University
i=2; j=1; k = 3 (1,2) + (2, 3) < (1,3) ≡ 7 ≮ 3 Vtx A B C D E Z 4 2 3 ∞ 5 6 1 2007 Kutztown University
i=2; j=1; k = 4 (1,2) + (2, 4) < (1,4) ≡ 7 < ∞ Vtx A B C D E Z 4 2 3 7 ∞ 5 6 1 2007 Kutztown University
i=2; j=1; k = 5 (1,2) + (2, 5) < (1,5) ≡ 4 < ∞ Vtx A B C D E Z 4 2 3 7 ∞ 5 6 1 2007 Kutztown University
i=2; j=1; k = 6 (1,2) + (2, 6) < (1,6) ≡ ∞ ≮ ∞ Vtx A B C D E Z 4 2 3 7 ∞ 5 6 1 2007 Kutztown University
Second Middle Loop i = 2 j = 2 k ≔ 1 to 6 2007 Kutztown University
Second Middle Loop k ≔ 1 to3 i j k 2 2 1 (2,2) + (2,1) < (2,1) ? 4 + 2 ≮ 2 2 2 2 (2,2) + (2,2) < (2,2) ? 4 + 4 ≮ 4 2 2 3 (2,2) + (2,3) < (2,3) ? 4 + 5 ≮ 5 2007 Kutztown University
Second Middle Loop k ≔ 4 to6 i j k 2 2 4 (2,2) + (2,4) < (2,4) ? 4 + 5 ≮ 5 2 2 5 (2,2) + (2,5) < (2,5) ? 4 + 2 ≮ 2 2 2 6 (2,2) + (2,6) < (2,6) ? 4 + ∞ ≮ ∞ 2007 Kutztown University
Matrix After 2nd Middle Loop {no change} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 1 2007 Kutztown University
Third Middle Loop k ≔ 1 to3 i j k 2 3 1 2 3 2 2 3 3 2 3 1 (3,2) + (2,1) < (3,1) ? 5 + 2 ≮ 3 2 3 2 (3,2) + (2,2) < (3,2) ? 5 + 4 ≮ 5 2 3 3 (3,2) + (2,3) < (3,3) ? 5 + 5 ≮ 6 2007 Kutztown University
Third Middle Loop k ≔ 4 to6 i j k 2 3 4 2 3 5 2 3 6 2 3 4 (3,2) + (2,4) < (3,4) ? 5 + 5 < ∞ 2 3 5 (3,2) + (2,5) < (3,5) ? 5 + 2 ≮ 5 2 3 6 (3,2) + (2,6) < (3,6) ? 5 + ∞ ≮ ∞ 2007 Kutztown University
Matrix After 3rd Middle Loop {1 cell changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University
Fourth Middle Loop k ≔ 1 to3 i j k 2 4 1 (4,2) + (2,1) < (4,1) ? 5 + 2 < ∞ 2 4 2 (4,2) + (2,2) < (4,2) ? 5 + 4 ≮ 5 2 4 3 (4,2) + (2,3) < (4,3) ? 5 + 5 < ∞ 2007 Kutztown University
Fourth Middle Loop k ≔ 4 to6 i j k 2 4 4 (4,2) + (2,4) < (4,4) ? 5 + 5 < ∞ 2 4 5 (4,2) + (2,5) < (4,5) ? 5 + 2 ≮ 1 2 4 6 (4,2) + (2,6) < (4,6) ? 5 + ∞ ≮ 2 2007 Kutztown University
Matrix After 4th Middle Loop {3 cells changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University
Fifth Middle Loop k ≔ 1 to3 i j k 2 5 1 2 5 2 2 5 3 2 5 1 (5,2) + (2,1) < (5,1) ? 2 + 2 < ∞ 2 5 2 (5,2) + (2,2) < (5,2) ? 2 + 4 ≮ 2 2 5 3 (5,2) + (2,3) < (5,3) ? 2 + 5 ≮ 5 2007 Kutztown University
Fifth Middle Loop k ≔ 4 to6 i j k 2 5 4 2 5 5 2 5 4 (5,2) + (2,4) < (5,4) ? 2 + 5 ≮ 1 2 5 5 (5,2) + (2,5) < (5,5) ? 2 + 2 < ∞ 2 5 6 (5,2) + (2,6) < (5,6) ? 2 + ∞ ≮ 4 2007 Kutztown University
Matrix After 5th Middle Loop {2 cells changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University
Sixth Middle Loop k ≔ 1 to3 i j k 2 6 1 2 6 3 2 6 1 (6,2) + (2,1) < (6,1) ? ∞ + 2 ≮ ∞ 2 6 2 (6,2) + (2,2) < (6,2) ? ∞ + 4 ≮ ∞ 2 6 3 (6,2) + (2,3) < (6,3) ? ∞ + 5 ≮ ∞ 2007 Kutztown University
Sixth Middle Loop k ≔ 4 to6 i j k 2 6 4 2 6 5 2 6 4 (6,2) + (2,4) < (6,4) ? ∞ + 5 ≮ ∞ 2 6 5 (6,2) + (2,5) < (6,5) ? ∞ + 2 ≮ ∞ 2 6 6 (6,2) + (2,6) < (6,6) ? ∞ + ∞ ≮ ∞ 2007 Kutztown University
Matrix After 6th Middle Loop {0 cells changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University
Third Outer Loop i = 3 j = ≔ 1 to 6 k ≔ 1 to 6 Step by step details given here 2007 Kutztown University
Matrix After 3rd Outer Loop {0 cells changed} Vtx A B C D E Z 4 2 3 7 ∞ 5 6 10 1 2007 Kutztown University
Fourth Outer Loop i = 4 j = ≔ 1 to 6 k ≔ 1 to 6 Step by step details given here 2007 Kutztown University
Matrix After 4th Outer Loop {10 cells changed} Vtx A B C D E Z 4 2 3 7 9 5 6 10 12 1 2007 Kutztown University
Fifth Outer Loop i = 5 j = ≔ 1 to 6 k ≔ 1 to 6 Step by step details given here 2007 Kutztown University
Matrix After 5th Outer Loop {12 cells changed} Vtx A B C D E Z 4 2 3 5 7 6 8 1 2007 Kutztown University
Sixth Outer Loop i = 6 j = ≔ 1 to 6 k ≔ 1 to 6 Step by step details given here 2007 Kutztown University
Matrix After 6th Outer Loop {0 cells changed} Vtx A B C D E Z 4 2 3 5 7 6 8 1 2007 Kutztown University
Final Matrix Diagonal values are removed Vtx A B C D E Z - 2 3 5 4 7 6 8 1 2007 Kutztown University
Path Costs from A Given by row entries A B :: 2 A C :: 3 A D :: 5 A E :: 4 A Z :: 7 2007 Kutztown University
Path Costs from B Given by row entries B A :: 2 B C :: 5 B D :: 3 B E :: 2 B Z :: 5 2007 Kutztown University
Path Costs from C Given by row entries C A :: 3 C B :: 5 C D :: 6 C E :: 5 C Z :: 8 2007 Kutztown University
Path Costs from D Given by row entries D A :: 5 D B :: 3 D C :: 6 D E :: 1 D Z :: 2 2007 Kutztown University
Path Costs from E Given by row entries E A :: 4 E B :: 2 E C :: 5 E D :: 1 E Z :: 3 2007 Kutztown University
Path Costs from Z Given by row entries Z A :: 7 Z B :: 5 Z C :: 8 Z D :: 2 Z E :: 3 2007 Kutztown University
Shortest Paths Floyd’s Algorithm gives the path costs Finding the actual paths reuqires additional work We will need two matrices One to hold travel costs Other to hold actual paths 2007 Kutztown University