1 / 64

Discrete Math 2 Shortest Paths Using Matrix

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

jin-chen
Download Presentation

Discrete Math 2 Shortest Paths Using Matrix

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Discrete Math 2Shortest Paths Using Matrix CIS112 February 14, 2007

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. The Basic Operation • Initially old cost is edge cost • Later . . • Old cost is cost of best route found so far Kutztown University

  8. Denotations • Outer loop – the i loop • Middle loop – the j loop • Inner loop – the k loop Kutztown University

  9. 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

  10. Matrix for Weighted Graph Kutztown University

  11. First Outer Loop • i = 1 • j = ≔ 1 to 6 • k ≔ 1 to 6 Kutztown University

  12. First Inner Loop • i = 1 • j = 1 • k ≔ 1 to 6 Kutztown University

  13. First Middle Loop • d(1,1) = ∞ • d(1,1) + d(1,k) ≮ d(1,k), ∀k • Therefore, no change Kutztown University

  14. Second Middle Loop • i = 1 • j = 2 • k ≔ 1 to 6 Kutztown University

  15. 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

  16. i=1; j=2; k = 2 (2,1) + (1,2) < (2,2) ≡ 4 < ∞ Kutztown University

  17. i=1; j=2; k = 3 (2,1) + (1, 3) < (2,3) ≡ 5 < ∞ Kutztown University

  18. i=1; j=2; k ≔ 4,5,6(2,1) + (1,k) < (2,k) ≡ ∞ ≮ x Kutztown University

  19. Third Middle Loop • i = 1 • j = 3 • k ≔ 1 to 6 Kutztown University

  20. 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

  21. i=1; j=3; k = 2 (3,1) + (1, 2) < (3,2) ≡ 5 < ∞ Kutztown University

  22. i=1; j=3; k = 3 (3,1) + (1, 3) < (3,3) ≡ 6 < ∞ Kutztown University

  23. i=1; j=3; k ≔ 4,5,6 (3,1) + (1, k) < (3,k) ≡ ∞ ≮ x Kutztown University

  24. 4th, 5th & 6th Middle Loops • i = 1 • j ≔ 4 to 6 • k ≔ 1 to 6 Kutztown University

  25. 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

  26. Second Outer Loop • i = 2 • j = ≔ 1 to 6 • k ≔ 1 to 6 Kutztown University

  27. i=2; j=1; k = 1 (1,2) + (2, 1) < (1,1) ≡ 4 < ∞ Kutztown University

  28. i=2; j=1; k = 2 (1,2) + (2, 2) < (1,2) ≡ 6 ≮ 2 Kutztown University

  29. i=2; j=1; k = 3 (1,2) + (2, 3) < (1,3) ≡ 7 ≮ 3 Kutztown University

  30. i=2; j=1; k = 4 (1,2) + (2, 4) < (1,4) ≡ 7 < ∞ Kutztown University

  31. i=2; j=1; k = 5 (1,2) + (2, 5) < (1,5) ≡ 4 < ∞ Kutztown University

  32. i=2; j=1; k = 6 (1,2) + (2, 6) < (1,6) ≡ ∞ ≮ ∞ Kutztown University

  33. Second Middle Loop • i = 2 • j = 2 • k ≔ 1 to 6 Kutztown University

  34. 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

  35. 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

  36. Matrix After 2nd Middle Loop {no change} Kutztown University

  37. 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

  38. 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

  39. Matrix After 3rd Middle Loop {1 cell changed} Kutztown University

  40. 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

  41. 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

  42. Matrix After 4th Middle Loop {3 cells changed} Kutztown University

  43. 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

  44. 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

  45. Matrix After 5th Middle Loop {2 cells changed} Kutztown University

  46. 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

  47. 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

  48. Matrix After 6th Middle Loop {0 cells changed} Kutztown University

  49. Third Outer Loop • i = 3 • j = ≔ 1 to 6 • k ≔ 1 to 6 • Step by step details given here Kutztown University

  50. Matrix After 3rd Outer Loop {0 cells changed} Kutztown University

More Related