Skip to content

Commit 97acdd3

Browse files
author
Hai Nguyen
committed
Fix typos
1 parent 829cf17 commit 97acdd3

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

All-Pairs Shortest Paths/README.markdown

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# All-Pairs Shortest Paths
22

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)`.
44

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:
66

77
<img src="img/weight_comparison_formula.png" width="400px" />
88

9-
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.
1010

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.
1212

1313
# Example
1414

@@ -22,9 +22,9 @@ the adjacency matrix representation `w` is
2222

2323
### Calculating shortest paths' weights
2424

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:
2626

27-
1. vertices with no path connecting them have the `ø` replaced with ``
27+
1. vertices with no path connecting them have the `ø` replaced with ``
2828
2. the diagonal has all `0`s
2929

3030
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
5656

5757
# Project Structure
5858

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.
6060

6161
In the framework:
6262

@@ -65,12 +65,12 @@ In the framework:
6565

6666
# TODO
6767

68-
- Implement naive `Θ(V^4)` method for comparison
68+
- Implement naive `O(V^4)` method for comparison
6969
- Implement Johnson's algorithm for sparse graphs
7070
- Implement other cool optimized versions
7171

7272
# References
7373

7474
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)
7575

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

Comments
 (0)