Presentation is loading. Please wait.

Presentation is loading. Please wait.

All-Pairs Shortest Path: Floyd-Warshall Algorithm

Similar presentations


Presentation on theme: "All-Pairs Shortest Path: Floyd-Warshall Algorithm"— Presentation transcript:

1 All-Pairs Shortest Path: Floyd-Warshall Algorithm
J.-S. Roger Jang (張智星) MIR Lab, CSIE Dept. National Taiwan University 2019/5/21

2 Shortest-Paths Algorithms
Several variants of shortest-path algorithms Single-source all-destination Dijkstra’s algorithm Bellman-Ford algorithm All-pairs shortest paths Floyd-Warshall algorithm

3 Floyd-Warshall Algorithm
2019/5/21 Characteristics All-pairs shortest paths Based on DP Supports negative edge-weights, but not negative-weight cycles O(n3)

4 Three-step DP Formula for Floyd-Warshall Algorithm
2019/5/21 Optimum-value function: 𝑑 𝑖,𝑗 𝑘 is the shortest path from nodes 𝑖 to 𝑗 using nodes only from {1,2,…,𝑘} (𝑘≤𝑛) as intermediate points Recurrent equation: 𝑑 𝑖,𝑗 𝑘 =𝑚𝑖𝑛 𝑑 𝑖,𝑗 𝑘−1 𝑑 𝑖,𝑘 𝑘−1 + 𝑑 𝑘,𝑗 𝑘−1 , with 𝑑 𝑖,𝑗 0 =𝑤 𝑖,𝑗 . Final answer: 𝑑 𝑖,𝑗 𝑛 Weight between 𝑖 and 𝑗

5 An Illustrative Example

6 Example 1 (1/5) Distance: {} Predecessor 𝐷 0 𝜋 0 Quiz! B A B C D 2 6 8
2 6 8 -2 3 4 1 A B C D - 2 3 -2 A D 8 4 1 6 C

7 Example 1 (2/5) Distance: {A} Predecessor 𝐷 1 𝜋 1 B 3 A B C D 2 6 8 ∞
2 6 8 -2 3 4 1 A B C D - 2 -2 A D 8 4 1 6 C

8 Example 1 (3/5) Distance: {A,B} Predecessor 𝐷 2 𝜋 2 B 3 A B C D 2 5 ∞
2 5 -2 3 4 6 1 A B C D - 2 -2 A D 8 4 1 6 C

9 Example 1 (4/5) Distance: {A,B,C} Predecessor 𝐷 3 𝜋 3 B 3 A B C D 2 1
2 1 -2 -1 4 6 A B C D - 2 -2 A D 8 4 1 6 C

10 Example 1 (5/5) Distance: {A,B,C,D} Predecessor 𝐷 4 𝜋 4 B 3 A B C D 2
2 1 -2 -1 4 6 A B C D - 2 -2 A D 8 4 1 6 C

11 Example 2 Distance: {} Predecessor 𝐷 0 𝜋 0 Quiz! B 4 A B C D ∞ 3 -2 1
3 -2 1 5 4 A B C D - -2 1 A D 5 3 C Solution

12 Example 3 Distance: {} Predecessor 𝐷 0 𝜋 0 A B C D Quiz! A B C D ∞ -2
-2 4 3 2 -1 A B C D - B C D Solution

13 Reference Youtube tutorials Web resources Abdul Bari: Basics
Joe James: Basics plus predecessors Erik Demaine: Comprehensive Web resources Chiu CC (in Chinese): Detailed descriptions


Download ppt "All-Pairs Shortest Path: Floyd-Warshall Algorithm"

Similar presentations


Ads by Google