COMP3010: Algorithm Theory and Design
Daniel Sutantyo, Department of Computing, Macquarie University
8.4 - Floyd-Warshall Algorithm
Background
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
Background
8.4 - Floyd-Warshall Algorithm
- In the matrix multiplication method, we find the shortest path from \(x_i\) to \(x_j\) using only one edge, and then two edges, and then three edges, etc
- We are going to do something very similar, so keep this in mind
- We use the usual definition of graphs
- \(G = \langle V,E \rangle\)
- no negative-weight cycle
8.4 - Floyd-Warshall Algorithm
- Let \(V = \{1,2,3\dots,n\}\)
- Take a subset \(\{1,2,3,\dots,k\}\) for some \(k < n\)
- Let \(p_{ij}^{(k)}\) be the shortest path from \(i\) to \(j\) with intermediate vertices only in \(\{1,2,3,\dots,k\}\)
- e.g. \(i = 5\), \(j = 6\), \(k=3\), \(V^* = \{1,2,3\}\)
How the Algorithm Works
6
1
5
2
3
7
4
2
2
2
3
1
12
7
11
2
8.4 - Floyd-Warshall Algorithm
- Let \(V = \{1,2,3\dots,n\}\)
- Take a subset \(\{1,2,3,\dots,k\}\) for some \(k < n\)
- Let \(p_{ij}^{(k)}\) be the shortest path from \(i\) to \(j\) with intermediate vertices only in \(\{1,2,3,\dots,k\}\)
- e.g. \(i = 5\), \(j = 6\), \(k=3\), \(V^* = \{1,2,3\}\)
\(p_{5,6}^{(3)} : 5 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 6\) (weight is 18)
How the Algorithm Works
6
1
5
2
3
7
4
2
2
2
3
1
12
7
11
2
How the Algorithm Works
8.4 - Floyd-Warshall Algorithm
- Let \(V = \{1,2,3\dots,n\}\)
- Take a subset \(\{1,2,3,\dots,k\}\) for some \(k < n\)
- Let \(p_{ij}^{(k)}\) be the shortest path from \(i\) to \(j\) with intermediate vertices only in \(\{1,2,3,\dots,k\}\)
- e.g. \(i = 6\), \(j = 8\), \(k=3\), \(V^* = \{1,2,3\}\)
\(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
How the Algorithm Works
8.4 - Floyd-Warshall Algorithm
- With matrix multiplication method, we look at paths with just one edge, and then two edges, and then three edges, and so on
- With Floyd-Warshall Algorithm, we look at paths that contains \(\{1\}\) as the intermediary vertex, then paths that have intermediate vertices in \(\{1,2\}\), then paths that have intermediate vertices in \(\{1,2,3\}\), and so on
\(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
How the Algorithm Works
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\}\)
How the Algorithm Works
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\}\)
How the Algorithm Works
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\}\)
How the Algorithm Works
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\}\)
Decomposing into Subproblems
8.4 - Floyd-Warshall Algorithm
- How does \(p_{ij}^{(k)}\) relates to \(p_{ij}^{(k-1)}\)
- the intermediate vertices of \(p_{ij}^{(k)}\) are drawn from \(\{1,2,3,\dots,k-1,k\}\)
- the intermediate vertices of \(p_{ij}^{(k-1)}\) are drawn from \(\{1,2,3,\dots,k-1\}\)
- When we add \(k\) to the set, there are two possibilities:
- \(k\) is not an intermediate vertex of \(p_{ij}^{(k)}\), so \(p_{ij}^{(k)}\) = \(p_{ij}^{(k-1)}\) (i.e. makes no difference)
-
- \(k\) is an intermediate vertex of \(p_{ij}^{(k)}\)
- this means we can decompose \(p_{ij}^{(k)}\) into two parts:
- shortest path from \(i\) to \(k\) with vertices in \(\{1,2,\dots,k-1\}\)
- shortest path from \(k\) to \(j\) with intermediate vertices in \(\{1,2,\dots, k-1\}\)
- this means we can decompose \(p_{ij}^{(k)}\) into two parts:
- \(k\) is an intermediate vertex of \(p_{ij}^{(k)}\)
Decomposing into Subproblems
8.4 - Floyd-Warshall Algorithm
- Case 1: \(k\) is not an intermediate vertex of \(p_{ij}^{(k)}\), so \(p_{ij}^{(k)}\) = \(p_{ij}^{(k-1)}\)
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\)
Decomposing into Subproblems
8.4 - Floyd-Warshall Algorithm
- Case 2: \(k\) is an intermediate vertex of \(p_{ij}^{(k)}\)
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\)
Decomposing into Subproblems
8.4 - Floyd-Warshall Algorithm
- Case 2: \(k\) is an intermediate vertex of \(p_{ij}^{(k)}\)
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\)
Decomposing into Subproblems
8.4 - Floyd-Warshall Algorithm
- Define \(d_{ij}^{(k)}\) to be the weight of a shortest path from vertex \(i\) to vertex \(j\) for which all intermediate vertices are in the set \(\{1,2,\dots,k\}\)
- i.e. it is the weight of \(p_{ij}^{(k)}\)
- What is \(d_{ij}^{(0)}\)?
- it is the weight of the shortest path from \(i\) to \(j\) with no intermediate vertices (since our vertices start at 1)
- so \(d_{ij}^{(0)} = w(i,j)\)
- does this feel similar to matrix multiplication method?
Decomposing into Subproblems
8.4 - Floyd-Warshall Algorithm
- what about \(d_{ij}^{(k)}\)?
- remember that if \(p_{ij}^{(k)}\) is the shortest path using only vertices \(\in \{1,2,\dots,k\}\), then we can decompose it by taking out \(k\)
\(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\)
Decomposing into Subproblems
8.4 - Floyd-Warshall Algorithm
- how do we decompose \(d_{ij}^{(k)}\)?
- remember that if \(p_{ij}^{(k)}\) is the shortest path using only vertices \(\in \{1,2,\dots,k\}\), then we can decompose it by taking out \(k\)
\[ 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\)
Algorithm
8.4 - Floyd-Warshall Algorithm
- As before, we compute a series of matrices \(D^{(k)}_{ij}\) for \(k =\{1,2,\dots,n\}\) where \(n\) is the number of vertices
- We start with \(D_{ij}^{(0)}\), which is just \(W\), the adjacency matrix
- We then construct \(D_{ij}^{(k)}\) using the recursive relation we defined earlier
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
- However, notice that this time we do not need to use the adjancency matrix, all the information we need to construct \(D_{ij}^{(k)}\) is already in \(D_{ij}^{(k-1)}\)
Algorithm
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)\]
- First row:
- main diagonal is unchanged
- column 2: can we go from 1 to 2 via 2?
- column 3: can we go from 1 to 3 via 2?
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
Algorithm
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)\]
- First row:
- main diagonal is unchanged
- column 2: can we go from 1 to 2 via 2?
- column 3: can we go from 1 to 3 via 2?
- column 4: can we go from 1 to 4 via 2?
- yes, it will cost 17
- column 5: can we go from 1 to 5 via 2?
\[ d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right)\]
Algorithm
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)\]
- First row:
- main diagonal is unchanged
- column 2: can we go from 1 to 2 via 2?
- column 3: can we go from 1 to 3 via 2?
- column 4: can we go from 1 to 4 via 2?
- yes, it will cost 17
- column 5: can we go from 1 to 5 via 2?
- yes, it will cost 11
\[ 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)\]
Algorithm
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)\]
Algorithm
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)\]
Algorithm
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)})\)
Algorithm
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)})\)
Algorithm
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)})\)
Algorithm
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)})\)
Algorithm
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)})\)
- Adding 3 to the set will not modify the first row because 1 cannot go to 3
- It wouldn't change the second row either
Algorithm
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\)
- Let's compute the fourth row
Algorithm
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\)
- Let's compute the fourth row
Algorithm
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\)
- Let's compute the fourth row
Algorithm
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\)
- Let's compute the fourth row
Algorithm
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];
}
}
}
}
}
Algorithm
8.4 - Floyd-Warshall Algorithm
8.4 - Floyd-Warshall Algorithm
- Complexity of the algorithm is just a triple loop, so it is \(O(V^3)\) compared to \(O(V^3\log V)\) using matrix multiplication method
- Unlike matrix multiplication method, we don't have to find the minimum among \(n\) possibilities
Complexity
\[ 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
- In terms of correctness, both methods are just brute force with dynamic programming, as in it tries every single combination
- the difference is the approach:
- matrix multiplication method: include more and more edges
- Floyd-Warshall: go through more and more vertices
- Make sure you understand the difference
Final Words
COMP3010 - 8.4 - Floyd-Warshall Algorithm
By Daniel Sutantyo
COMP3010 - 8.4 - Floyd-Warshall Algorithm
- 142