Skip to content

Commit 994ac27

Browse files
committed
Update Floyd-Warshall READMEs.
1 parent 52918ee commit 994ac27

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,13 @@ algorithm is an abstraction higher than a computer program.
154154
* `A` [Levenshtein Distance](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
155155
* `A` [Longest Common Subsequence](src/algorithms/sets/longest-common-subsequence) (LCS)
156156
* `A` [Longest Common Substring](src/algorithms/string/longest-common-substring)
157-
* `A` [Longest Increasing subsequence](src/algorithms/sets/longest-increasing-subsequence)
157+
* `A` [Longest Increasing Subsequence](src/algorithms/sets/longest-increasing-subsequence)
158158
* `A` [Shortest Common Supersequence](src/algorithms/sets/shortest-common-supersequence)
159159
* `A` [0/1 Knapsack Problem](src/algorithms/sets/knapsack-problem)
160160
* `A` [Integer Partition](src/algorithms/math/integer-partition)
161161
* `A` [Maximum Subarray](src/algorithms/sets/maximum-subarray)
162162
* `A` [Bellman-Ford Algorithm](src/algorithms/graph/bellman-ford) - finding shortest path to all graph vertices
163+
* `A` [Floyd-Warshall Algorithm](src/algorithms/graph/floyd-warshall) - find shortest paths between all pairs of vertices
163164
* `A` [Regular Expression Matching](src/algorithms/string/regular-expression-matching)
164165
* **Backtracking** - similarly to brute force, try to generate all possible solutions, but each time you generate next solution you test
165166
if it satisfies all conditions, and only then continue generating subsequent solutions. Otherwise, backtrack, and go on a
+90-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,94 @@
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+
![Recursive Formula](https://wikimedia.org/api/rest_v1/media/math/render/svg/f9b75e25063384ccca499c56f9a279abf661ad3b)
26+
27+
![Recursive Formula](https://wikimedia.org/api/rest_v1/media/math/render/svg/34ac7c89bbb18df3fd660225fd38997079e5e513)
28+
![Recursive Formula](https://wikimedia.org/api/rest_v1/media/math/render/svg/0326d6c14def89269c029da59eba012d0f2edc9d)
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+
![Example](https://upload.wikimedia.org/wikipedia/commons/2/2e/Floyd-Warshall_example.svg)
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 |
289

390
## References
491

592
- [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

Comments
 (0)