Daniel Sutantyo, Department of Computing, Macquarie University
8.3 - Matrix Multiplication Method
8.3 - Matrix Multiplication Method
ShortestPath(\(u\),\(v\))
\(w(u,y_1)\) + ShortestPath(\(y_1\),\(v\))
\(w(u,v)\)
\(\dots\)
\(w(u,y_2)\) +ShortestPath(\(y_2\),\(v\))
\(w(u,y_\ell)\) +ShortestPath(\(y_\ell\),\(v\))
8.3 - Matrix Multiplication Method
\(x_i\)
\(x_j\)
\(\cdots\)
\(x_k\)
\(p_{ik}\)
\((k,j)\)
8.3 - Matrix Multiplication Method
ShortestPath(\(u\),\(v\)) with at most \(m\) edges
\(w(u,y_1)\) + ShortestPath(\(y_1\),\(v\)) with at most \((m-1)\) edges
\(w(u,v)\)
\(\dots\)
\(w(u,y_2)\) +ShortestPath(\(y_2\),\(v\)) with at most \((m-1)\) edges
\(w(u,y_\ell)\) +ShortestPath(\(y_\ell\),\(v\)) with at most \((m-1)\) edges
8.3 - Matrix Multiplication Method
8.3 - Matrix Multiplication Method
\[ l_{ij}^{(0)} = \begin{cases}0 & \text{if $i = j$},\\ \infty & \text{otherwise.}\end{cases} \]
8.3 - Matrix Multiplication Method
\[ l_{ij}^{(1)} = \begin{cases}0 & \text{if $i = j$},\\ ? & \text{otherwise.}\end{cases} \]
\[ l_{ij}^{(0)} = \begin{cases}0 & \text{if $i = j$},\\ \infty & \text{otherwise.}\end{cases} \]
\[ l_{ij}^{(2)} = \begin{cases}0 & \text{if $i = j$},\\ ? & \text{otherwise.}\end{cases} \]
8.3 - Matrix Multiplication Method
\[ l_{ij}^{(1)} = \begin{cases}0 & \text{if $i = j$},\\ w(i,j) & \text{otherwise.}\end{cases} \]
\[ l_{ij}^{(0)} = \begin{cases}0 & \text{if $i = j$},\\ \infty & \text{otherwise.}\end{cases} \]
\[ l_{ij}^{(2)} = \begin{cases}0 & \text{if $i = j$},\\ ? & \text{otherwise.}\end{cases} \]
8.3 - Matrix Multiplication Method
\[ l_{ij}^{(2)} = \begin{cases}0 & \text{if $i = j$},\\ ? & \text{otherwise.}\end{cases} \]
i
j
\[ l_{ij}^{(2)} =\min_{1 \le k \le n} \left(l_{ik}^{(1)} + w(k,j)\right) \]
\[ l_{ij}^{(1)} = \begin{cases}0 & \text{if $i = j$},\\ w(i,j) & \text{otherwise.}\end{cases} \]
note: \(w(j,j) = 0\)
8.3 - Matrix Multiplication Method
\[\ell^{(m)}_{ij} = \min_{1 \le k \le n} \left( l_{ik}^{(m-1)} + w(k,j)\right)\]
8.3 - Matrix Multiplication Method
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & -1 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & -1 & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
5
2
1
4
3
8
3
9
-5
-4
1
7
\[L^{(1)} = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & -3 & -4 \\ \infty & 0 & 4 & 4 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & 2 & -5 & 0 & \infty \\ \infty & \infty & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & -1 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[\left(\begin{matrix}0 & 8 & -8 & -3 & -4\end{matrix}\right)\left(\begin{matrix}8 \\ 0 \\ 7 \\ \infty \\ \infty\end{matrix}\right)\]
\[ = (0\times 8) + (8 \times 0) + (-8 \times 7) + (-3 * \infty) + (-4 * \infty)\]
\[W = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & -1 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
8.3 - Matrix Multiplication Method
\[ = (0\times 8) + (8 \times 0) + (-8 \times 7) + (-3 * \infty) + (-4 * \infty)\]
\[W = \left(\begin{matrix} 0 & 8 & \infty & \infty & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & \infty & \infty \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[L^{(3)} = \left(\begin{matrix} 0 & 8 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[L^{(4)} = \left(\begin{matrix} 0 & -1 & -8 & -3 & -4 \\ \infty & 0 & -1 & 4 & 3 \\ \infty & 7 & 0 & 11 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
do you see the similarity?
8.3 - Matrix Multiplication Method
8.3 - Matrix Multiplication Method
8.3 - Matrix Multiplication Method
8.3 - Matrix Multiplication Method
// Let L = L^(i) and W be the adjacency matrix
// Compute Lp = L^(i+1)
public static int[][] extend(int[][] L, int [][] W) {
int INF = Integer.MAX_VALUE
int n = L.length;
int[][] Lp = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// set Lp[i][j] to infinity at start
Lp[i][j] = INF;
for (int k = 0; k < n; k++) {
// if either W[k][j] or L[i][k] is infinity,
// then Lp[i][j] should be infinity as well
if (W[k][j] != INF && L[i][k] != INF)
Lp[i][j] = Math.min(Lp[i][j], L[i][k]+W[k][j]);
}
}
}
return Lp;
}