Daniel Sutantyo, Department of Computing, Macquarie University
8.4 - Floyd-Warshall Algorithm
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.4 - Floyd-Warshall Algorithm
8.4 - Floyd-Warshall Algorithm
6
1
5
2
3
7
4
2
2
2
3
1
12
7
11
2
8.4 - Floyd-Warshall Algorithm
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\) (weight is 18)
6
1
5
2
3
7
4
2
2
2
3
1
12
7
11
2
8.4 - Floyd-Warshall Algorithm
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\) (weight is 18)
\( 5 \rightarrow 1 \rightarrow 2 \rightarrow 6\) (weight is 25)
\(5 \rightarrow 1 \rightarrow 3 \rightarrow 6\) (weight is 21)
6
1
5
2
3
7
4
2
2
2
3
1
12
7
11
2
8.4 - Floyd-Warshall Algorithm
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\) (weight is 18)
6
1
5
2
3
7
4
2
2
2
3
1
12
7
11
2
8.4 - Floyd-Warshall Algorithm
6
1
5
2
3
7
4
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\) (weight is 18)
2
2
2
3
1
12
7
11
2
\(p_{5,6}^{(2)} : 5 \rightarrow 1 \rightarrow 2 \rightarrow 6\) (weight is 25)
\(p_{5,6}^{(1)} :\) not possible (weight is \(\infty\))
\(p_{5,6}^{(4)} : 5 \rightarrow 4 \rightarrow 6\) (weight is 3)
\(\{1\}\)
\(\{1,2\}\)
\(\{1,2,3\}\)
\(\{1,2,3,4\}\)
8.4 - Floyd-Warshall Algorithm
6
1
5
2
3
7
4
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\) (weight is 18)
2
2
2
3
1
12
7
11
2
\(p_{5,6}^{(2)} : 5 \rightarrow 1 \rightarrow 2 \rightarrow 6\) (weight is 25)
\(p_{5,6}^{(1)} :\) not possible (weight is \(\infty\))
\(p_{5,6}^{(4)} : 5 \rightarrow 4 \rightarrow 6\) (weight is 3)
\(\{1\}\)
\(\{1,2\}\)
\(\{1,2,3\}\)
\(\{1,2,3,4\}\)
8.4 - Floyd-Warshall Algorithm
6
1
5
2
3
7
4
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\) (weight is 18)
2
2
2
3
1
12
7
11
2
\(p_{5,6}^{(2)} : 5 \rightarrow 1 \rightarrow 2 \rightarrow 6\) (weight is 25)
\(p_{5,6}^{(1)} :\) not possible (weight is \(\infty\))
\(p_{5,6}^{(4)} : 5 \rightarrow 4 \rightarrow 6\) (weight is 3)
\(\{1\}\)
\(\{1,2\}\)
\(\{1,2,3\}\)
\(\{1,2,3,4\}\)
8.4 - Floyd-Warshall Algorithm
6
1
5
2
3
7
4
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\) (weight is 18)
2
2
2
3
1
12
7
11
2
\(p_{5,6}^{(2)} : 5 \rightarrow 1 \rightarrow 2 \rightarrow 6\) (weight is 25)
\(p_{5,6}^{(1)} :\) not possible (weight is \(\infty\))
\(p_{5,6}^{(4)} : 5 \rightarrow 4 \rightarrow 6\) (weight is 3)
\(\{1\}\)
\(\{1,2\}\)
\(\{1,2,3\}\)
\(\{1,2,3,4\}\)
8.4 - Floyd-Warshall Algorithm
8.4 - Floyd-Warshall Algorithm
6
1
5
2
3
2
4
2
2
2
3
1
12
2
2
12
\(p_{5,6}^{(2)} : 5 \rightarrow 1 \rightarrow 2 \rightarrow 6\)
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 2 \rightarrow 6\)
8.4 - Floyd-Warshall Algorithm
6
1
5
2
3
2
4
2
2
2
3
1
12
2
12
2
\(p_{5,6}^{(2)} : 5 \rightarrow 1 \rightarrow 2 \rightarrow 6\)
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\)
8.4 - Floyd-Warshall Algorithm
6
1
5
2
3
2
4
2
2
2
3
1
12
2
12
2
\(p_{5,6}^{(2)} : 5 \rightarrow 1 \rightarrow 2 \rightarrow 6\)
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\)
\(p_{5,3}^{(2)} : 5 \rightarrow 1 \rightarrow 3\)
\(p_{3,6}^{(2)} : 3 \rightarrow 2 \rightarrow 6\)
8.4 - Floyd-Warshall Algorithm
8.4 - Floyd-Warshall Algorithm
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\)
\(p_{5,3}^{(2)} : 5 \rightarrow 1 \rightarrow 3\)
\(p_{3,6}^{(2)} : 3 \rightarrow 2 \rightarrow 6\)
8.4 - Floyd-Warshall Algorithm
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(k\) is not in the shortest path
\(k\) is in the shortest path, so it is the sum of shortest path from \(i\) to \(k\) and \(k\) to \(j\)
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\)
\(p_{5,3}^{(2)} : 5 \rightarrow 1 \rightarrow 3\)
\(p_{3,6}^{(2)} : 3 \rightarrow 2 \rightarrow 6\)
8.4 - Floyd-Warshall Algorithm
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\[D^{(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)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\[D^{(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)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\[D^{(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)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\[D^{(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)\]
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{1,2}^{(3)} = \min(d_{1,2}^{(2)},d_{1,3}^{(2)}+d_{3,2}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{1,2}^{(3)} = \min(d_{1,2}^{(2)},d_{1,3}^{(2)}+d_{3,2}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(8,\infty+7) = 8\)
\(d_{1,3}^{(3)} = \min(d_{1,3}^{(2)},d_{1,3}^{(2)}+d_{3,3}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{1,2}^{(3)} = \min(d_{1,2}^{(2)},d_{1,3}^{(2)}+d_{3,2}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(8,\infty+7) = 8\)
\(d_{1,3}^{(3)} = \min(d_{1,3}^{(2)},d_{1,3}^{(2)}+d_{3,3}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{1,2}^{(3)} = \min(d_{1,2}^{(2)},d_{1,3}^{(2)}+d_{3,2}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(8,\infty+7) = 8\)
\(d_{1,3}^{(3)} = \min(d_{1,3}^{(2)},d_{1,3}^{(2)}+d_{3,3}^{(2)})\)
\(d_{1,4}^{(3)} = \min(d_{1,4}^{(2)},d_{1,3}^{(2)}+d_{3,4}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{1,2}^{(3)} = \min(d_{1,2}^{(2)},d_{1,3}^{(2)}+d_{3,2}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(8,\infty+7) = 8\)
\(d_{1,3}^{(3)} = \min(d_{1,3}^{(2)},d_{1,3}^{(2)}+d_{3,3}^{(2)})\)
\(d_{1,4}^{(3)} = \min(d_{1,4}^{(2)},d_{1,3}^{(2)}+d_{3,4}^{(2)})\)
\(d_{1,5}^{(3)} = \min(d_{1,5}^{(2)},d_{1,3}^{(2)}+d_{3,5}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{1,2}^{(3)} = \min(d_{1,2}^{(2)},d_{1,3}^{(2)}+d_{3,2}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(8,\infty+7) = 8\)
\(d_{1,3}^{(3)} = \min(d_{1,3}^{(2)},d_{1,3}^{(2)}+d_{3,3}^{(2)})\)
\(d_{1,4}^{(3)} = \min(d_{1,4}^{(2)},d_{1,3}^{(2)}+d_{3,4}^{(2)})\)
\(d_{1,5}^{(3)} = \min(d_{1,5}^{(2)},d_{1,3}^{(2)}+d_{3,5}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{4,1}^{(3)} = \min(d_{4,1}^{(2)},d_{4,3}^{(2)}+d_{3,1}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(\infty,-5+\infty) = \infty\)
\(d_{4,2}^{(3)} = \min(d_{4,2}^{(2)},d_{4,3}^{(2)}+d_{3,2}^{(2)})\)
\(d_{4,3}^{(3)} = \min(d_{4,3}^{(2)},d_{4,3}^{(2)}+d_{3,3}^{(2)})\)
\(d_{4,5}^{(3)} = \min(d_{4,5}^{(2)},d_{4,3}^{(2)}+d_{3,5}^{(2)})\)
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\(= \min(\infty,-5+7) = 2\)
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{4,1}^{(3)} = \min(d_{4,1}^{(2)},d_{4,3}^{(2)}+d_{3,1}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(\infty,-5+\infty) = \infty\)
\(d_{4,2}^{(3)} = \min(d_{4,2}^{(2)},d_{4,3}^{(2)}+d_{3,2}^{(2)})\)
\(d_{4,3}^{(3)} = \min(d_{4,3}^{(2)},d_{4,3}^{(2)}+d_{3,3}^{(2)})\)
\(d_{4,5}^{(3)} = \min(d_{4,5}^{(2)},d_{4,3}^{(2)}+d_{3,5}^{(2)})\)
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\(= \min(\infty,-5+7) = 2\)
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{4,1}^{(3)} = \min(d_{4,1}^{(2)},d_{4,3}^{(2)}+d_{3,1}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(\infty,-5+\infty) = \infty\)
\(d_{4,2}^{(3)} = \min(d_{4,2}^{(2)},d_{4,3}^{(2)}+d_{3,2}^{(2)})\)
\(d_{4,3}^{(3)} = \min(d_{4,3}^{(2)},d_{4,3}^{(2)}+d_{3,3}^{(2)})\)
\(d_{4,5}^{(3)} = \min(d_{4,5}^{(2)},d_{4,3}^{(2)}+d_{3,5}^{(2)})\)
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\(= \min(\infty,-5+7) = 2\)
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{4,1}^{(3)} = \min(d_{4,1}^{(2)},d_{4,3}^{(2)}+d_{3,1}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(\infty,-5+\infty) = \infty\)
\(d_{4,2}^{(3)} = \min(d_{4,2}^{(2)},d_{4,3}^{(2)}+d_{3,2}^{(2)})\)
\(d_{4,3}^{(3)} = \min(d_{4,3}^{(2)},d_{4,3}^{(2)}+d_{3,3}^{(2)})\)
\(d_{4,5}^{(3)} = \min(d_{4,5}^{(2)},d_{4,3}^{(2)}+d_{3,5}^{(2)})\)
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\(= \min(\infty,-5+7) = 2\)
8.4 - Floyd-Warshall Algorithm
5
2
1
4
3
8
3
9
-5
-4
1
7
\(d_{4,1}^{(3)} = \min(d_{4,1}^{(2)},d_{4,3}^{(2)}+d_{3,1}^{(2)})\)
8.4 - Floyd-Warshall Algorithm
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\(= \min(\infty,-5+\infty) = \infty\)
\(d_{4,2}^{(3)} = \min(d_{4,2}^{(2)},d_{4,3}^{(2)}+d_{3,2}^{(2)})\)
\(d_{4,3}^{(3)} = \min(d_{4,3}^{(2)},d_{4,3}^{(2)}+d_{3,3}^{(2)})\)
\(d_{4,5}^{(3)} = \min(d_{4,5}^{(2)},d_{4,3}^{(2)}+d_{3,5}^{(2)})\)
\[D^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & \infty & -5 & 0 & \infty \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
\(= \min(\infty,-5+7) = 2\)
\[D^{(3)} = \left(\begin{matrix} 0 & 8 & \infty & 17 & -4 \\ \infty & 0 & \infty & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & \infty & \infty & 1 & 0\end{matrix}\right)\]
8.4 - Floyd-Warshall Algorithm
8.4 - Floyd-Warshall Algorithm
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
// Let W be the adjacency matrix
void floydWarshall(int W[][]) {
int INF = Integer.MAX_VALUE;
int n = W.length; // assume W is a square 2D array with
int D[][] = new int[n][n];
// set D^(0) = W
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
D[i][j] = W[i][j];
for (k = 0; k < n; k++){
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
// if either D[i][k] or D[k][j] is infinity,
// then D[i][j] should not be changed
if (D[i][k] != INF && D[k][j] != INF){
if (D[i][k] + D[k][j] < D[i][j])
D[i][j] = D[i][k] + D[k][j];
}
}
}
}
}
8.4 - Floyd-Warshall Algorithm
8.4 - Floyd-Warshall Algorithm
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
\[\ell^{(m)}_{ij} = \min_{1 \le k \le n} \left( l_{ik}^{(m-1)} + w(k,j)\right)\]
8.4 - Floyd-Warshall Algorithm
8.4 - Floyd-Warshall Algorithm