Matrix Multiplication
Summarized notes from Introduction to Algorithms, Chapter 25
- dynamic programming algorithm for finding all-pairs shortest paths
- looks like repeated matrix multiplication
- compute shortest path by extending shortest path edge by edge
- Start with \(L^{(1)} = W\) which represents weights from original graph
- after \(n-1\) repetitions will converge if no cycles
-
Solution computes matrix \(L'\) which will be the output
-
Extend-shortest-paths method is used to update weight estimate:
1 2 3 4 5 6 7 8 9 |
|
Two versions
Slow All-Pairs
1 2 3 4 5 6 7 |
|
Faster All-Pairs
1 2 3 4 5 6 7 8 9 |
|
Complexity
Running Time | |
---|---|
\(\Theta(V^4)\) | Slow-APSP |
\(\Theta(V^3 \lg V)\) | Faster-APSP |
Why is Faster-APSP faster?
=> no need to compute all \(L^{(m)}\) matrices → iterate fewer times using repeated squaring
\(L^{(1)} = W\),
\(L^{(2)} = W^2 = W \cdot W\),
\(L^{(4)} = W^4 = W^2 \cdot W^2\),
\(L^{(8)} = W^8 = W^4 \cdot W^4\),
\(\vdots\)
\(L^{(2^{\lceil\lg(n-1)\rceil)}} = W^{2^{\lceil\lg(n-1)\rceil}} = W^{2^{\lceil\lg(n-1)\rceil - 1}} \cdot W^{2^{\lceil\lg(n-1)\rceil - 1}}\)