Download presentation
Presentation is loading. Please wait.
1
Discrete Math 2 Shortest Paths Using Matrix
2001 Discrete Math 2 Shortest Paths Using Matrix CIS112 February 14, 2007 Daniel L. Silver
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 2007 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) 2007 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 2007 Kutztown University
5
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
6
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
7
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
8
Denotations Outer loop – the i loop Middle loop – the j loop
Inner loop – the k loop 2007 Kutztown University
9
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
10
Matrix for Weighted Graph
Vtx A B C D E Z ∞ 2 3 5 1 4 2007 Kutztown University
11
First Outer Loop i = 1 j = ≔ 1 to 6 k ≔ 1 to 6 2007
Kutztown University
12
First Inner Loop i = 1 j = 1 k ≔ 1 to 6 2007 Kutztown University
13
First Middle Loop d(1,1) = ∞ d(1,1) + d(1,k) ≮ d(1,k), ∀k
Therefore, no change 2007 Kutztown University
14
Second Middle Loop i = 1 j = 2 k ≔ 1 to 6 2007 Kutztown University
15
Second Middle Loop k = 1 d(1,1) = ∞ d(2,1) + d(1,1) = ∞
Therefore, no change 2007 Kutztown University
16
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
17
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
18
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
19
Third Middle Loop i = 1 j = 3 k ≔ 1 to 6 2007 Kutztown University
20
Third Middle Loop k = 1 d(1,1) = ∞ d(3,1) + d(1,1) = ∞
Therefore, no change 2007 Kutztown University
21
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
22
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
23
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
24
4th, 5th & 6th Middle Loops i = 1 j ≔ 4 to 6 k ≔ 1 to 6 2007
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 2007 Kutztown University
26
Second Outer Loop i = 2 j = ≔ 1 to 6 k ≔ 1 to 6 2007
Kutztown University
27
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
28
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
29
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
30
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
31
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
32
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
33
Second Middle Loop i = 2 j = 2 k ≔ 1 to 6 2007 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 2007 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 + ∞ ≮ ∞ 2007 Kutztown University
36
Matrix After 2nd Middle Loop {no change}
Vtx A B C D E Z 4 2 3 7 ∞ 5 6 1 2007 Kutztown University
37
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
38
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
39
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
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 < ∞ 2007 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 2007 Kutztown University
42
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
43
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
44
Fifth Middle Loop k ≔ 4 to6 i j k 2 5 4 2 5 5
2 5 4 (5,2) + (2,4) < (5,4) ? ≮ 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
45
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
46
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
47
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
48
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
49
Third Outer Loop i = 3 j = ≔ 1 to 6 k ≔ 1 to 6
Step by step details given here 2007 Kutztown University
50
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
51
Fourth Outer Loop i = 4 j = ≔ 1 to 6 k ≔ 1 to 6
Step by step details given here 2007 Kutztown University
52
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
53
Fifth Outer Loop i = 5 j = ≔ 1 to 6 k ≔ 1 to 6
Step by step details given here 2007 Kutztown University
54
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
55
Sixth Outer Loop i = 6 j = ≔ 1 to 6 k ≔ 1 to 6
Step by step details given here 2007 Kutztown University
56
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
57
Final Matrix Diagonal values are removed
Vtx A B C D E Z - 2 3 5 4 7 6 8 1 2007 Kutztown University
58
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
59
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
60
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
61
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
62
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
63
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
64
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.