640 likes | 766 Views
Discrete Math 2 Shortest Paths Using Matrix. CIS112 February 14, 2007. Overview. Previously: In weighted graph . . Shortest path from #7 to all others Search matrix method Now: Problem 8.6.2 Implement Floyd’s Algorithm. Strategy. 3 nested loops i ≔ 1 to 6 j ≔ 1 to 6
E N D
Discrete Math 2Shortest Paths Using Matrix CIS112 February 14, 2007
Overview • Previously: • In weighted graph . . • Shortest path from #7 to all others • Search matrix method • Now: • Problem 8.6.2 • Implement Floyd’s Algorithm 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) 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 Kutztown University
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 Kutztown University
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 Kutztown University
The Basic Operation • Initially old cost is edge cost • Later . . • Old cost is cost of best route found so far Kutztown University
Denotations • Outer loop – the i loop • Middle loop – the j loop • Inner loop – the k loop Kutztown University
Operation • 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 Kutztown University
Matrix for Weighted Graph Kutztown University
First Outer Loop • i = 1 • j = ≔ 1 to 6 • k ≔ 1 to 6 Kutztown University
First Inner Loop • i = 1 • j = 1 • k ≔ 1 to 6 Kutztown University
First Middle Loop • d(1,1) = ∞ • d(1,1) + d(1,k) ≮ d(1,k), ∀k • Therefore, no change Kutztown University
Second Middle Loop • i = 1 • j = 2 • k ≔ 1 to 6 Kutztown University
Second Middle Loop • k = 1 • d(1,1) = ∞ • d(2,1) + d(1,1) = ∞ • d(2,1) + d(1,1) = ∞≮ d(2,1) • Therefore, no change Kutztown University
i=1; j=2; k = 2 (2,1) + (1,2) < (2,2) ≡ 4 < ∞ Kutztown University
i=1; j=2; k = 3 (2,1) + (1, 3) < (2,3) ≡ 5 < ∞ Kutztown University
i=1; j=2; k ≔ 4,5,6(2,1) + (1,k) < (2,k) ≡ ∞ ≮ x Kutztown University
Third Middle Loop • i = 1 • j = 3 • k ≔ 1 to 6 Kutztown University
Third Middle Loop • k = 1 • d(1,1) = ∞ • d(3,1) + d(1,1) = ∞ • d(3,1) + d(1,1) = ∞≮ d(3,1) • Therefore, no change Kutztown University
i=1; j=3; k = 2 (3,1) + (1, 2) < (3,2) ≡ 5 < ∞ Kutztown University
i=1; j=3; k = 3 (3,1) + (1, 3) < (3,3) ≡ 6 < ∞ Kutztown University
i=1; j=3; k ≔ 4,5,6 (3,1) + (1, k) < (3,k) ≡ ∞ ≮ x Kutztown University
4th, 5th & 6th Middle Loops • i = 1 • j ≔ 4 to 6 • k ≔ 1 to 6 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 Kutztown University
Second Outer Loop • i = 2 • j = ≔ 1 to 6 • k ≔ 1 to 6 Kutztown University
i=2; j=1; k = 1 (1,2) + (2, 1) < (1,1) ≡ 4 < ∞ Kutztown University
i=2; j=1; k = 2 (1,2) + (2, 2) < (1,2) ≡ 6 ≮ 2 Kutztown University
i=2; j=1; k = 3 (1,2) + (2, 3) < (1,3) ≡ 7 ≮ 3 Kutztown University
i=2; j=1; k = 4 (1,2) + (2, 4) < (1,4) ≡ 7 < ∞ Kutztown University
i=2; j=1; k = 5 (1,2) + (2, 5) < (1,5) ≡ 4 < ∞ Kutztown University
i=2; j=1; k = 6 (1,2) + (2, 6) < (1,6) ≡ ∞ ≮ ∞ Kutztown University
Second Middle Loop • i = 2 • j = 2 • k ≔ 1 to 6 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 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 + ∞≮∞ Kutztown University
Matrix After 2nd Middle Loop {no change} Kutztown University
Third Middle Loop k ≔ 1 to3 • i j k • 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 Kutztown University
Third Middle Loop k ≔ 4 to6 • i j k • 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 + ∞≮∞ Kutztown University
Matrix After 3rd Middle Loop {1 cell changed} 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 < ∞ 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 Kutztown University
Matrix After 4th Middle Loop {3 cells changed} Kutztown University
Fifth Middle Loop k ≔ 1 to3 • i j k • 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 Kutztown University
Fifth Middle Loop k ≔ 4 to6 • i j k • 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 Kutztown University
Matrix After 5th Middle Loop {2 cells changed} Kutztown University
Sixth Middle Loop k ≔ 1 to3 • i j k • 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 ≮ ∞ Kutztown University
Sixth Middle Loop k ≔ 4 to6 • i j k • 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) ? • ∞ + ∞≮ ∞ Kutztown University
Matrix After 6th Middle Loop {0 cells changed} Kutztown University
Third Outer Loop • i = 3 • j = ≔ 1 to 6 • k ≔ 1 to 6 • Step by step details given here Kutztown University
Matrix After 3rd Outer Loop {0 cells changed} Kutztown University