|
1 |
| -# Floyd–Warshall algorithm |
| 1 | +# Floyd–Warshall Algorithm |
| 2 | + |
| 3 | +In computer science, the **Floyd–Warshall algorithm** is an algorithm for finding |
| 4 | +shortest paths in a weighted graph with positive or negative edge weights (but |
| 5 | +with no negative cycles). A single execution of the algorithm will find the |
| 6 | +lengths (summed weights) of shortest paths between all pairs of vertices. Although |
| 7 | +it does not return details of the paths themselves, it is possible to reconstruct |
| 8 | +the paths with simple modifications to the algorithm. |
| 9 | + |
| 10 | +## Algorithm |
| 11 | + |
| 12 | +The Floyd–Warshall algorithm compares all possible paths through the graph between |
| 13 | +each pair of vertices. It is able to do this with `O(|V|^3)` comparisons in a graph. |
| 14 | +This is remarkable considering that there may be up to `|V|^2` edges in the graph, |
| 15 | +and every combination of edges is tested. It does so by incrementally improving an |
| 16 | +estimate on the shortest path between two vertices, until the estimate is optimal. |
| 17 | + |
| 18 | +Consider a graph `G` with vertices `V` numbered `1` through `N`. Further consider |
| 19 | +a function `shortestPath(i, j, k)` that returns the shortest possible path |
| 20 | +from `i` to `j` using vertices only from the set `{1, 2, ..., k}` as |
| 21 | +intermediate points along the way. Now, given this function, our goal is to |
| 22 | +find the shortest path from each `i` to each `j` using only vertices |
| 23 | +in `{1, 2, ..., N}`. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +This formula is the heart of the Floyd–Warshall algorithm. |
| 31 | + |
| 32 | +## Example |
| 33 | + |
| 34 | +The algorithm above is executed on the graph on the left below: |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +In the tables below `i` is row numbers and `j` is column numbers. |
| 39 | + |
| 40 | + |
| 41 | +**k = 0** |
| 42 | + |
| 43 | +| | 1 | 2 | 3 | 4 | |
| 44 | +|:-----:|:---:|:---:|:---:|:---:| |
| 45 | +| **1** | 0 | ∞ | −2 | ∞ | |
| 46 | +| **2** | 4 | 0 | 3 | ∞ | |
| 47 | +| **3** | ∞ | ∞ | 0 | 2 | |
| 48 | +| **4** | ∞ | −1 | ∞ | 0 | |
| 49 | + |
| 50 | + |
| 51 | +**k = 1** |
| 52 | + |
| 53 | +| | 1 | 2 | 3 | 4 | |
| 54 | +|:-----:|:---:|:---:|:---:|:---:| |
| 55 | +| **1** | 0 | ∞ | −2 | ∞ | |
| 56 | +| **2** | 4 | 0 | 2 | ∞ | |
| 57 | +| **3** | ∞ | ∞ | 0 | 2 | |
| 58 | +| **4** | ∞ | − | ∞ | 0 | |
| 59 | + |
| 60 | + |
| 61 | +**k = 2** |
| 62 | + |
| 63 | +| | 1 | 2 | 3 | 4 | |
| 64 | +|:-----:|:---:|:---:|:---:|:---:| |
| 65 | +| **1** | 0 | ∞ | −2 | ∞ | |
| 66 | +| **2** | 4 | 0 | 2 | ∞ | |
| 67 | +| **3** | ∞ | ∞ | 0 | 2 | |
| 68 | +| **4** | 3 | −1 | 1 | 0 | |
| 69 | + |
| 70 | + |
| 71 | +**k = 3** |
| 72 | + |
| 73 | +| | 1 | 2 | 3 | 4 | |
| 74 | +|:-----:|:---:|:---:|:---:|:---:| |
| 75 | +| **1** | 0 | ∞ | −2 | 0 | |
| 76 | +| **2** | 4 | 0 | 2 | 4 | |
| 77 | +| **3** | ∞ | ∞ | 0 | 2 | |
| 78 | +| **4** | 3 | −1 | 1 | 0 | |
| 79 | + |
| 80 | + |
| 81 | +**k = 4** |
| 82 | + |
| 83 | +| | 1 | 2 | 3 | 4 | |
| 84 | +|:-----:|:---:|:---:|:---:|:---:| |
| 85 | +| **1** | 0 | −1 | −2 | 0 | |
| 86 | +| **2** | 4 | 0 | 2 | 4 | |
| 87 | +| **3** | 5 | 1 | 0 | 2 | |
| 88 | +| **4** | 3 | −1 | 1 | 0 | |
2 | 89 |
|
3 | 90 | ## References
|
4 | 91 |
|
5 | 92 | - [Wikipedia](https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm)
|
| 93 | +- [YouTube (by Abdul Bari)](https://www.youtube.com/watch?v=oNI0rf2P9gE&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8&index=74) |
| 94 | +- [YouTube (by Tushar Roy)](https://www.youtube.com/watch?v=LwJdNfdLF9s&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8&index=75) |
0 commit comments