Daniel Sutantyo
Department of Computing
Macquarie University
overview
a
s
c
16
t
b
d
12
13
14
4
20
7
9
4
source
sink
overview
a
s
c
16
t
b
d
12
13
14
4
20
7
9
4
source
sink
overview
a
s
c
16
t
b
d
12
13
14
4
20
7
9
4
source
sink
a
s
c
12/16
t
b
d
12/12
4/13
4/14
4/4
12/20
0/7
0/9
0/4
source
sink
example of a flow
more examples
a
s
c
11/16
t
b
d
12/12
8/13
11/14
4/4
15/20
7/7
4/9
1/4
source
sink
a
s
c
12/16
t
b
d
12/12
4/13
4/14
4/4
12/20
0/7
0/9
0/4
source
sink
definitions
(because each vertex has degree at least 1)
definitions
\[\sum_{v\in V}f(v,u) = \sum_{v\in V} f(u,v)\]
definitions
\[|f| = \sum_{v \in V} f(s,v) \]
\[|f| = \sum_{v \in V} f(s,v) - \sum_{v \in V} f(v,s)\]
definitions
a
s
c
11/16
t
b
d
12/12
8/13
11/14
4/4
15/20
7/7
4/9
1/4
source
sink
a
s
c
11/16
t
b
d
11/12
8/13
11/14
4/4
15/20
7/7
3/9
0/4
source
sink
this is a flow
this is a different flow
definitions
a
s
c
11/16
t
b
d
11/12
8/13
11/14
4/4
15/20
7/7
3/9
0/4
source
sink
\(f(s,a) = 11\)
\(f(s,c) = 8\)
\(f(c,a) = 0\)
\(f(a,b) = 11\)
\(f(b,c) = 3\)
\(f(c,d) = 11\)
\(f(d,b) = 7\)
\(f(b,t) = 15\)
\(f(d,t) = 4\)
the value of this flow is \(19\) (i.e \(|f| = 19\) )
definitions
a
s
c
11/16
t
b
d
12/12
4/13
7/14
0/4
15/20
7/7
4/9
1/4
source
sink
a
s
c
1/16
t
b
d
1/12
1/13
1/14
1/4
1/20
0/7
0/9
0/4
source
sink
this is also a flow
this is yet another flow
definitions
a
s
c
1/16
t
b
d
1/12
1/13
1/14
1/4
1/20
0/7
0/9
0/4
source
sink
\(f(s,a) = 1\)
\(f(s,c) = 1\)
\(f(c,a) = 0\)
\(f(a,b) = 1\)
\(f(b,c) = 0\)
\(f(c,d) = 1\)
\(f(d,b) = 0\)
\(f(b,t) = 1\)
\(f(d,t) = 1\)
the value of this flow is \(19\) (i.e \(|f| = 2\) )
definitions
residual networks
residual networks
a
s
c
11/16
t
b
d
12/12
8/13
11/14
4/4
15/20
7/7
4/9
1/4
\(G\):
\(G_f\):
a
s
c
5
t
b
d
5
3
4
5
7
4
1
11
8
11
12
5
15
3
8
formal definitions for residual networks
\[c_f(u,v)=\begin{cases} c(u,v)-f(u,v)& \text{if $(u,v)\in E$,} \\ f(v,u) & \text{if $(v,u)\in E$,}\\ 0 & \text{otherwise} \end{cases} \]
augmenting path
\(G_f\):
a
s
c
5
t
b
d
5
3
4
5
7
4
1
11
11
12
5
15
3
8
augmenting path
a
s
c
5
t
b
d
3
4
7
4/4
1
11
11
12
5
15
3
8
4/5
4/5
a
s
c
5
t
b
d
5
3
4
5
7
4
1
11
11
12
5
15
3
8
augmenting path
a
s
c
5
t
b
d
3
4
7
4/4
1
11
11
12
5
15
3
8
4/5
4/5
\[f_p(u,v) = \begin{cases}\min\{c_f(u,v)\} & \text{if $(u,v)$ is on $p$,}\\ 0 & \text{otherwise}\end{cases}\]
augmenting path
\[(f \uparrow f_p)(u,v) = \begin{cases} f(u,v) + f_p(u,v) - f_p(v,u) &\text{if $(u,v) \in E$,} \\ 0 & \text{otherwise.}\end{cases}\]
\(|f\uparrow f_p| = |f| + |f_p| > |f|\)
(Lemma 26.1, Corollary 26.3 of CLRS, page 717, 720)
the algorithm
example run
a
s
c
16
t
b
d
12
13
14
4
20
7
9
4
\(G:\)
\(G_f:\)
a
s
c
16
t
b
d
12
13
14
4
20
7
9
4
example run
a
s
c
12/16
t
b
d
12/12
0/13
0/14
0/4
12/20
0/7
0/9
0/4
\(G:\)
\(G_f:\)
a
s
c
4
t
b
d
12
13
14
4
12
7
9
4
12
8
example run
a
s
c
12/16
t
b
d
12/12
4/13
4/14
4/4
12/20
0/7
0/9
0/4
\(G:\)
\(G_f:\)
a
s
c
4
t
b
d
12
9
10
12
7
9
4
12
8
4
4
4
example run
a
s
c
12/16
t
b
d
12/12
11/13
11/14
4/4
19/20
7/7
0/9
0/4
\(G:\)
\(G_f:\)
a
s
c
4
t
b
d
19
2
3
12
7
9
4
12
1
11
11
4
another example
another example
a
s
c
4/16
t
b
d
8/12
4/13
4/14
4/4
4/20
0/7
4/9
4/4
\(G\):
\(G_f\):
a
s
c
12
t
b
d
9
10
4
16
7
4
4
4
8
4
8
5
4
4
4
another example
a
s
c
4/16
t
b
d
8/12
4/13
4/14
4/4
4/20
0/7
4/9
4/4
\(G\):
8
a
s
c
8/16
t
b
d
8/12
4/13
4/14
4/4
8/20
0/7
0/9
0/4
\(G\):
8
complexity analysis
complexity analysis
correctness
definitions
decomposition into subproblems
ShortestPath(\(u\),\(v\))
ShortestPath(\(y_1\),\(v\))
ShortestPath(\(y_2\),\(v\))
ShortestPath(\(y_\ell\),\(v\))
\(\dots\)
(where the vertices \(y_i\), \(1 \le i \le \ell\), are the other vertices in the graph)
decomposition into subproblems
ShortestPath(\(u\),\(v\))
\(w(u,y_1)\) + ShortestPath(\(y_1\),\(v\))
\(\dots\)
\(w(u,y_2)\) + ShortestPath(\(y_2\),\(v\))
\(w(u,y_\ell)\) + ShortestPath(\(y\ell\),\(v\))
\[\text{ShortestPath}(u,v) = \begin{cases} \displaystyle \min_{1 \le i \le \ell} \left(w(u,y_i) + \text{ShortestPath}(y_i,v)\right) & \text{if $u \ne v$} \\ 0 &\text{if $u = v$} \end{cases}\]
optimal substructure
ShortestPath(\(u\),\(v\))
\(w(u,y_1)\) + ShortestPath(\(y_1\),\(v\))
\(\dots\)
\(w(u,y_2)\) + ShortestPath(\(y_2\),\(v\))
\(w(u,y_\ell)\) + ShortestPath(\(y\ell\),\(v\))
optimal substructure
negative values
5
2
1
4
3
8
3
9
-5
-4
1
7
negative values
5
2
1
4
3
8
1
9
-5
-4
1
2
cycles
5
2
1
4
3
8
1
9
-5
-4
1
2
cycles
5
2
1
4
3
8
1
9
-5
-4
1
2
cycles
relaxation
relaxation
relaxation
a
s
4
10
1
b
the algorithm
the algorithm
example
e
b
s
d
c
8
3
9
-5
-4
1
7
\(\infty\)
\(\infty\)
\(\infty\)
\(\infty\)
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(\delta(e) = -4\)
\(\delta(b) = 8\)
no change
\(\delta(d) = 17\)
no change
\(\delta(c) = 12\)
\(\delta(d) = -3\)
example
e
b
s
d
c
8
3
9
-5
-4
1
7
\(8\)
\(-3\)
\(12\)
\(-4\)
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
no change
no change
\(\delta(c) = -8\)
no change
no change
no change
no change
example
e
b
s
d
c
8
3
9
-5
-4
1
7
\(8\)
\(-3\)
\(-8\)
\(-4\)
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
no change
no change
no change
no change
no change
\(\delta(b) = -1\)
no change
example
e
b
s
d
c
8
3
9
-5
-4
1
7
\(-1\)
\(-3\)
\(-8\)
\(-4\)
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
no change
no change
no change
no change
no change
\(\delta(b) = -1\)
no change
ShortestPath(\(i\),\(j\))
\(w(i,y_1)\) + ShortestPath(\(y_1\),\(j\))
\(\dots\)
\(w(i,y_2)\) + ShortestPath(\(y_2\),\(j\))
\(w(i,y_\ell)\) + ShortestPath(\(y\ell\),\(j\))
ShortestPath(\(i\),\(j\)) with at most \(m\) edges
ShortestPath\((i,y_1) + w(y_1,j)\)
\(\dots\)
ShortestPath\((i,y_2) + w(y_2,j)\)
ShortestPath\((i,y_\ell) + w(y_\ell,j)\)
ShortestPath(\(i\),\(j\))
\(w(i,y_1)\) + ShortestPath(\(y_1\),\(j\))
\(\dots\)
\(w(i,y_2)\) + ShortestPath(\(y_2\),\(j\))
\(w(i,y_\ell)\) + ShortestPath(\(y\ell\),\(j\))
with at most \((m-1)\) edges
with at most \((m-1)\) edges
with at most \((m-1)\) edges
recursive relation
\[ l_{ij}^{(0)} = \begin{cases}0 & \text{if $i = j$},\\ \infty & \text{otherwise.}\end{cases} \]
recursive relation
\[ l_{ij}^{(1)} = \begin{cases}0 & \text{if $i = j$},\\ ? & \text{otherwise.}\end{cases} \]
\[ l_{ij}^{(1)} = \begin{cases}0 & \text{if $i = j$},\\ w(i,j) & \text{otherwise.}\end{cases} \]
\[ l_{ij}^{(2)} =\ ? \]
recursive relation
\[ l_{ij}^{(2)} =\ ? \]
i
j
\[ l_{ij}^{(2)} =\min_{1 \le k \le n} \left(l_{ik}^{(1)} + w(k,j)\right) \]
note: \(w(j,j) = 0\)
recursive relation
\(l_{ij}^{(m)} = \)
\[\min_{1 \le k \le n} \left( l_{ik}^{(m-1)} + w(k,j)\right)\]
matrix multiplication algorithm
example
5
2
1
4
3
8
3
9
-5
\[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)\]
-4
1
7
\[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)\]
example
5
2
1
4
3
8
3
9
-5
\[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)\]
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
example
5
2
1
4
3
8
3
9
-5
\[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)\]
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & ? & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
example
5
2
1
4
3
8
3
9
-5
\[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)\]
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & ? & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
example
5
2
1
4
3
8
3
9
-5
\[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)\]
-4
1
7
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & ? & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
\[L^{(2)} = \left(\begin{matrix} 0 & 8 & \infty & -3 & ? \\ ? & 0 & ? & ? & ? \\ ? & ? & 0 & ? & ? \\ ? & ? & ? & 0 & ? \\ ? & ? & ? & ? & 0\end{matrix}\right)\]
example
ShortestPath(\(i\),\(j\)) with at most \(m\) edges
ShortestPath\((i,y_1) + w(y_1,j)\)
\(\dots\)
ShortestPath\((i,y_2) + w(y_2,j)\)
ShortestPath\((i,y_\ell) + w(y_\ell,j)\)
with at most \((m-1)\) edges
with at most \((m-1)\) edges
with at most \((m-1)\) edges
example
5
2
1
4
3
8
3
9
-5
\[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)\]
-4
1
7
\[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)\]
algorithm
// 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;
}
complexity
5
2
1
4
3
8
3
9
-5
\[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)\]
1
7
-4
\[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)\]
complexity
\[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)\]
\[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^{(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)\]
complexity
\[L^{(2)}_{14} = \left(\begin{matrix}0 & 8 &\infty & \infty & -4\end{matrix}\right)\left(\begin{matrix}\infty \\ 9 \\ \infty \\ 0 \\ 1\end{matrix}\right)\]
\[L^{(2)}_{14} = (0\times \infty) + (8 \times 9) + (\infty \times \infty) + (\infty * 0) + (-4 * 1)\]
\[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)\]
\[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^{(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)\]
complexity
\[L^{(2)}_{14} = (0\times \infty) + (8 \times 9) + (\infty \times \infty) + (\infty * 0) + (-4 * 1)\]
do you see the similarity?
complexity
\[L^{(2)}_{14} = (0\times \infty) + (8 \times 9) + (\infty \times \infty) + (\infty * 0) + (-4 * 1)\]
complexity
complexity
complexity
ShortestPath(\(i\),\(j\)) with at most \(m\) edges
ShortestPath\((i,y_1) + w(y_1,j)\)
\(\dots\)
ShortestPath\((i,y_2) + w(y_2,j)\)
ShortestPath\((i,y_\ell) + w(y_\ell,j)\)
with at most \((m-1)\) edges
with at most \((m-1)\) edges
with at most \((m-1)\) edges
8
1
6
2
3
2
7
3
2
1
7
7
2
11
4
2
\(p : 6 \rightarrow 1 \rightarrow 3 \rightarrow 2 \rightarrow 8\)
8
1
6
2
3
2
7
3
6
1
7
7
2
2
4
6
\(p : 6 \rightarrow 1 \rightarrow 2 \rightarrow 8\)
\(p^\prime : 6 \rightarrow 1 \rightarrow 2 \rightarrow 8\)
8
1
6
2
3
2
7
3
2
1
7
7
2
11
4
2
\(p : 6 \rightarrow 1 \rightarrow 3\rightarrow 2 \rightarrow 8\)
\(p^\prime : 6 \rightarrow 1 \rightarrow 2 \rightarrow 8\)
\[ 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\))
\[ 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\))
example
5
2
1
4
3
8
3
9
-5
\[D^{(0)} = \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)\]
-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)\]
example
5
2
1
4
3
8
3
9
-5
\[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)\]
-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)\]
example
5
2
1
4
3
8
3
9
-5
\[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)\]
-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^{(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)\]
example
5
2
1
4
3
8
3
9
-5
-4
1
7
\[D^{(5)} = \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)\]
\[D^{(4)} = \left(\begin{matrix} 0 & 8 & 12 & 17 & -4 \\ \infty & 0 & 4 & 9 & 3 \\ \infty & 7 & 0 & 16 & 10 \\ \infty & 2 & -5 & 0 & 5 \\ \infty & 3 & -4 & 1 & 0\end{matrix}\right)\]
\[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)\]
// 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];
}
}
}
}
}
comparison with matrix multiplication method
comparison with matrix multiplication method
5
2
1
4
3
8
3
9
-5
\[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)\]
-4
1
7
\[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)\]
\[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)\]
make sure you understand the difference!