You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: All-Pairs Shortest Paths/README.markdown
+9-9
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# All-Pairs Shortest Paths
2
2
3
-
The All-Pairs shortest path problem simultaneously computes the shortest path from each node in the graph to each other node, provded a path exists for each pair. In the naive approach, we could simply compute a single-source shortest path from each node to each other node. The number of shortest paths computed is then bound by `O(V^2)`, where `V` is the number of vertices in the graph. Because SSSP is also bounded by `O(V^2)`, the total running time for the naive approach would be `O(V^4)`.
3
+
The All-Pairs shortest path problem simultaneously computes the shortest path from each node in the graph to each other node, provided a path exists for each pair. In the naive approach, we could simply compute a single-source shortest path from each node to each other node. The number of shortest paths computed is then bound by `O(V^2)`, where `V` is the number of vertices in the graph. Because SSSP is also bounded by `O(V^2)`, the total running time for the naive approach would be `O(V^4)`.
4
4
5
-
However, by applying a dynamic approach on the adjacency matrix of the graph, a running time of `Θ(V^3)` is achievable, using the `Floyd-Warshall` algorithm. Floyd-Warshall iterates through an adjacency matrix, and for each pair of start(`i`) and end(`j`) vertices it considers if the current distance between them is greater than a path taken through another vertex(`k`) in the graph (if paths `i`~> `k` and `k`~> `j` exist). It moves through an adjacency matrix for every vertex `k` applying its comparison for each pair (`i`, `j`), so for each `k` a new adjacency matrix `D(k)` is derived, where each value `d(k)[i][j]` is defined as:
5
+
However, by applying a dynamic approach on the adjacency matrix of the graph, a running time of `O(V^3)` is achievable, using the `Floyd-Warshall` algorithm. Floyd-Warshall iterates through an adjacency matrix, and for each pair of start(`i`) and end(`j`) vertices it considers if the current distance between them is greater than a path taken through another vertex(`k`) in the graph (if paths `i`~> `k` and `k`~> `j` exist). It moves through an adjacency matrix for every vertex `k` applying its comparison for each pair (`i`, `j`), so for each `k` a new adjacency matrix `D(k)` is derived, where each value `d(k)[i][j]` is defined as:
where `w[i][j]` is the weight of the edge connecting vertex `i` to vertex `j` in the graph's original adjacency matrix.
9
+
where `w[i][j]` is the weight of the edge connecting vertex `i` to vertex `j` in the graph's original adjacency matrix.
10
10
11
-
When the algorithm memoizes each refined adjacency and predecessor matrix, its space complexity is `Θ(V^3)`, which can be optimised to `Θ(V^2)` by only memoizing the latest refinements. Reconstructing paths is a recursive procedure which requires `Θ(V)` time and `Θ(V^2)` space.
11
+
When the algorithm memoizes each refined adjacency and predecessor matrix, its space complexity is `O(V^3)`, which can be optimised to `O(V^2)` by only memoizing the latest refinements. Reconstructing paths is a recursive procedure which requires `O(V)` time and `O(V^2)` space.
12
12
13
13
# Example
14
14
@@ -22,9 +22,9 @@ the adjacency matrix representation `w` is
22
22
23
23
### Calculating shortest paths' weights
24
24
25
-
At the beginning of the algorithm, `D(0)` is the same as `w`, with the following exceptions to accomodate the comparison function:
25
+
At the beginning of the algorithm, `D(0)` is the same as `w`, with the following exceptions to accommodate the comparison function:
26
26
27
-
1. vertices with no path connecting them have the `ø` replaced with `∞`
27
+
1. vertices with no path connecting them have the `ø` replaced with `∞`
28
28
2. the diagonal has all `0`s
29
29
30
30
Here are all the adjacency matrices derived when perform Floyd-Warshall on the above graph:
@@ -56,7 +56,7 @@ This algorithm finds only the lengths of the shortest paths between all pairs of
56
56
57
57
# Project Structure
58
58
59
-
The provided xcworkspace allows working in the playground, which imports the APSP framework target from the xcodeproj. Build the framework target and rerun the playground to get started. There is also a test target in the xcodeproj.
59
+
The provided xcworkspace allows working in the playground, which imports the APSP framework target from the xcodeproj. Build the framework target and rerun the playground to get started. There is also a test target in the xcodeproj.
60
60
61
61
In the framework:
62
62
@@ -65,12 +65,12 @@ In the framework:
65
65
66
66
# TODO
67
67
68
-
- Implement naive `Θ(V^4)` method for comparison
68
+
- Implement naive `O(V^4)` method for comparison
69
69
- Implement Johnson's algorithm for sparse graphs
70
70
- Implement other cool optimized versions
71
71
72
72
# References
73
73
74
74
Chapter 25 of Introduction to Algorithms, Third Edition by Cormen, Leiserson, Rivest and Stein [https://mitpress.mit.edu/books/introduction-algorithms](https://mitpress.mit.edu/books/introduction-algorithms)
75
75
76
-
*Written for Swift Algorithm Club by [Andrew McKnight](https://github.com/armcknight)*
76
+
*Written for Swift Algorithm Club by [Andrew McKnight](https://github.com/armcknight)*
0 commit comments