Seminar 7 - Dynamic Programming Graph Algorithms
Seminar 7 - Dynamic Programming Graph Algorithms
1
13/04/2025
Outline
Outline
1. Shortest path in a graph with negative weights
(Bellman-Ford Algorithm)
2. All-pairs shortest paths (Floyd-Warshall Algorithm)
3. Transitive Closure
2
13/04/2025
6 u
s -4
3
v
6 u
s -4
3
v
3
13/04/2025
6 u
s -4
3
v
5
6 u t
-2
-3
s 4 7
-4
7
v x
9
4
13/04/2025
Bellman-Ford Algorithm
Bellman-Ford's algorithm works correctly even with negative edge weights
(as long as there are no negative weight cycles).
It can be modified to return all valid shortest distances, and minus ∞ for
vertices which are affected by the negative cycle.
5
13/04/2025
No, the shortest path from s to t will never include a cycle (either positive or
negative) if the goal is to minimize the total cost of the path.
Why is that?
s -3
5 7
-4
7
v x
9
6
13/04/2025
Bellman-Ford Algorithm
A fact from Week 5: If P is a shortest path from s to u, and v is the last vertex on
P before u, then the part of P from s to v is also a shortest path.
Proof:
Suppose there was a shorter path from s to v, say Q.
If it’s the shortest to reach u,
weight(Q) + w(v,u) < weight(P)
But P is the shortest path from s to u. (Contradiction)
Therefore, the part of P from s to v is also a shortest path.
Q
s v u
P
FIT2004, Lecture 7 - DP Graph Algorithms
Bellman-Ford Algorithm
Bellman-Ford was one of the first applications of dynamic programming.
7
13/04/2025
Bellman-Ford Algorithm
For a source node s, let OPT(i,v) denote the minimum weight of a
s-->v path with at most i edges. (Here v is for vertices)
Bellman-Ford Algorithm
Uses array M[0...V-1,1...V]
Initialize M[0,s] = 0, for all other vertices M[0,v] =
infinity
for i = 1 to V-1:
for each vertex v:
Compute M[i,v] using the recurrence
return M[V-1,1...V]
8
13/04/2025
Bellman-Ford Algorithm
Uses array M[0...V-1,1...V]
Initialize M[0,s] = 0, for all other vertices M[0,v] =
infinity
for i = 1 to V-1:
for each vertex v:
Compute M[i,v] using the recurrence
return M[V-1,1...V]
Time Complexity:
O(VE)
Bellman-Ford Algorithm
Commonly, a more space-efficient version of Bellman-Ford
algorithm is implemented.
9
13/04/2025
Bellman-Ford Algorithm
V-1 iterations are performed, but the value i is used just as a counter, and in
each iteration, for each node v, we use following update rule for the distance:
dist(s,s) = 0
Consider the following operation (relaxation):
For each edge (a, b) in the graph
dist(s, b) = min(dist(s,b) , dist(s,a) + w(a,b))
∞ ∞
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) ∞ 9
∞
FIT2004, Lecture 7 - DP Graph Algorithms
10
13/04/2025
∞ ∞
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) ∞ 9
∞
FIT2004, Lecture 7 - DP Graph Algorithms
6
5
∞
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) ∞ 9
∞
FIT2004, Lecture 7 - DP Graph Algorithms
11
13/04/2025
6
5
∞
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) ∞ 9
∞
FIT2004, Lecture 7 - DP Graph Algorithms
6
5
∞
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
∞
FIT2004, Lecture 7 - DP Graph Algorithms
12
13/04/2025
6
5
∞
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
∞
FIT2004, Lecture 7 - DP Graph Algorithms
6 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
∞
FIT2004, Lecture 7 - DP Graph Algorithms
13
13/04/2025
6 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
∞
FIT2004, Lecture 7 - DP Graph Algorithms
6 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
14
13/04/2025
6 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
6 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
15
13/04/2025
2 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
2 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
16
13/04/2025
2 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
2 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
17
13/04/2025
2 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
2 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
18
13/04/2025
2 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
-2
FIT2004, Lecture 7 - DP Graph Algorithms
2 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
-2
FIT2004, Lecture 7 - DP Graph Algorithms
19
13/04/2025
Speeding things up: All edges relaxation in the third iteration do not change
anything.
Early Stop Condition: If nothing changes in one iteration, it is possible to stop the
execution of the Bellman-Ford algorithm and output the current values.
2 4
5
6 u t
-2
-3
0 s 8 7
-4
7
Assume the following order: v x
(s,u), (t,u), (s,v), (u,v), (u,t), (v,t), (x,t), (u,x), (v,x) 7 9
-2
FIT2004, Lecture 7 - DP Graph Algorithms
0 s 4 -3
7 9
v x
7
6
FIT2004, Lecture 7 - DP Graph Algorithms
20
13/04/2025
0
Z 0
2 4
0 u t
0 -2
0
0 s 4 -3
7 9
v x
7
6
21
13/04/2025
Bellman-Ford Algorithm
For this space-efficient version of Bellman-Ford algorithm, there is a
guarantee that after i iterations dist[v] is no larger than the total weight of the
shortest path from s to v that uses at most i edges.
But there is no guarantee that these two values are equal after i iterations:
depending on the order in which the edges are relaxed, the path P from s to v
that has weight dist[v] could already contain more than i edges after the i-th
iteration of the outer loop.
e.g., in the graph that we followed a detailed execution of Bellman-Ford, the path
from s to t already has two edges after just one iteration.
6 4
5
6 u t
-2
-3
0 s 8 7
-4
7
v x
7 9
2
FIT2004, Lecture 7 - DP Graph Algorithms
22
13/04/2025
Outline
1. Shortest path in a graph with negative weights
(Bellman-Ford Algorithm)
2. All-pairs shortest paths (Floyd-Warshall Algorithm)
3. Transitive Closure
23
13/04/2025
24
13/04/2025
Floyd-Warshall Algorithm
Algorithm based on dynamic programming.
Floyd-Warshall Algorithm
Algorithm based on dynamic programming.
Computes the shortest distance between every pair of nodes as
long as there are no negative weight cycles.
If the graph has a negative cycle, it will always be detected.
Is this similar to Bellman-Ford algorithm?
No.
Bellman-Ford only detects negative cycles that are reachable from the
source node.
For a graph without negative cycles, after the k-th iteration,
dist[i][j] contains the weight of the shortest path from node i to
node j that only uses intermediate nodes from the set {1,…, k}.
25
13/04/2025
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
i= A B C D
Assume that the outer for-loop will access j= A B C D
vertices in the order A, B, C, D
dist[i][j] > dist[i][k] + dist[k][j]
First iteration of outer loop (i.e., k is A): A to A dist[A][A] > dist[A][A] + dist[A][A]
To vertex 0 > 0+0 → no update
A B C D
-2 C 2
A 0 Inf -2 Inf
From B 4 0 3 Inf A D
vertex 3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
26
13/04/2025
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
i= A B C D
The outer for-loop will access vertices in the j= A B C D
order A, B, C, D
dist[i][j] > dist[i][k] + dist[k][j]
First iteration of outer loop (i.e., k is A): A to B dist[A][B] > dist[A][A] + dist[A][B]
∞ > 0+∞ → no update
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 3 Inf A D
3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
i= A B C D
The outer for-loop will access vertices in the j= A B C D
order A, B, C, D
dist[i][j] > dist[i][k] + dist[k][j]
First iteration of outer loop (i.e., k is A): A to C dist[A][C] > dist[A][A] + dist[A][C]
-2 > 0 - 2→ no update
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 3 Inf A D
3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
27
13/04/2025
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
i= A B C D
The outer for-loop will access vertices in the j= A B C D
order A, B, C, D
dist[i][j] > dist[i][k] + dist[k][j]
First iteration of outer loop (i.e., k is A): A to D dist[A][D] > dist[A][A] + dist[A][D]
∞ > 0+∞ → no update
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 3 Inf A D
3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
i= A B C D
The outer for-loop will access vertices in the j= A B C D
order A, B, C, D
dist[i][j] > dist[i][k] + dist[k][j]
First iteration of outer loop (i.e., k is A): B to A dist[B][A] > dist[B][A] + dist[A][A]
4 > 4+0 → no update
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 3 Inf A D
3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
28
13/04/2025
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
i= A B C D Let’s speed up
The outer for-loop will access vertices in the j= A B C D
order A, B, C, D
dist[i][j] > dist[i][k] + dist[k][j]
First iteration of outer loop (i.e., k is A): B to C dist[B][C] > dist[B][A] + dist[A][C]
3 > 4-2 → yes, then update
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 3 Inf A D
3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
i= A B C D Let’s speed up
The outer for-loop will access vertices in the j= A B C D
order A, B, C, D
dist[i][j] > dist[i][k] + dist[k][j]
First iteration of outer loop (i.e., k is A): B to C dist[B][C] > dist[B][A] + dist[A][C]
3 > 4-2 → yes, then update
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 3 Inf A D
3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
29
13/04/2025
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
i= A B C D Let’s speed up
The outer for-loop will access vertices in the j= A B C D
order A, B, C, D
dist[i][j] > dist[i][k] + dist[k][j]
First iteration of outer loop (i.e., k is A): B to C dist[B][C] > dist[B][A] + dist[A][C]
3 > 4-2 → yes, then update
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 2 Inf A D
3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
i= A B C D Let’s speed up
The outer for-loop will access vertices in the j= A B C D
order A, B, C, D
dist[i][j] > dist[i][k] + dist[k][j]
First iteration of outer loop (i.e., k is A): B to D dist[B][D] > dist[B][A] + dist[A][D]
∞ > 4+∞ → no update
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 2 Inf A D
3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
30
13/04/2025
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
The outer for-loop will access vertices in the order A,
B, C, D i= A B C D
Second iteration of outer loop (i.e., k is B): D to A and D to C j= A B C D
dist[i][j] > dist[i][k] + dist[k][j]
dist[D][A] > dist[D][B] + dist[B][A] → ∞ > -1 + 4, update
dist[D][C] > dist[D][B] + dist[B][C] → ∞ > -1 + 2, update
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 2 Inf A D
3
C Inf Inf 0 2
D Inf -1 Inf 0 4 -1
B
31
13/04/2025
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
k= A B C D
The outer for-loop will access vertices in the order A,
B, C, D i= A B C D
Second iteration of outer loop (i.e., k is B): D to A and D to C j= A B C D
dist[i][j] > dist[i][k] + dist[k][j]
dist[D][A] > dist[D][B] + dist[B][A] → ∞ > -1 + 4 = 3
dist[D][C] > dist[D][B] + dist[B][C] → ∞ > -1 + 2 = 1
A B C D
-2 C 2
A 0 Inf -2 Inf
B 4 0 2 Inf A D
3
C Inf Inf 0 2
D 3 -1 1 0 4 -1
B
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
Assume that the outer for-loop will access vertices in the order A, B, C, D
Using nodes from {A, B, C} as intermediates, it is possible to update the following
distances:
A B C D
-2 C 2
A 0 Inf -2 0
B 4 0 2 4 A D
3
C Inf Inf 0 2
D 3 -1 1 0 4 -1
B
32
13/04/2025
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
Assume that the outer for-loop will access vertices in the order A, B, C, D
Using nodes from {A, B, C, D} as intermediates, it is possible to update the following
distances:
A B C D
-2 C 2
A 0 -1 -2 0
B 4 0 2 4 A D
3
C 5 1 0 2
D 3 -1 1 0 4 -1
B
Floyd-Warshall Algorithm
Initialise adjacency matrix called dist[][] considering adjacent edges only
For each vertex k in the graph
For each pair of vertices i and j in the graph
If dist[i][k] + dist[k][j] < dist[i][j] // i.e., dist(i → k → j) is smaller than the current dist(i→j)
Update dist[i][j] = dist[i][k] + dist[k][j] // create shortcut i →j with weight equal to dist(i→k→j)
Assume that the outer for-loop will access vertices in the order A, B, C, D
Final Solution:
A B C D
-2 C 2
A 0 -1 -2 0
B 4 0 2 4 A D
3
C 5 1 0 2
D 3 -1 1 0 4 -1
B
33
13/04/2025
Floyd-Warshall Algorithm
dist[][] = E # Initialize adjacency matrix using E
for vertex k in 1..V:
#Invariant: dist[i][j] corresponds to the shortest path from i
to j considering the intermediate vertices 1 to k-1
for vertex i in 1..V:
for vertex j in 1..V:
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
Time Complexity:
O(V3)
Space Complexity:
O(V2)
Inductive Step:
Assume dist[i][j] is the shortest path from i to j detouring through only vertices 1 to
k-1.
Adding the k-th vertex to the “detour pool” can only help if the best path detours
through k.
Thus, minimum of dist(i→k→j) and dist(i→j) gives the minimum distance from i to j
considering the intermediate vertices 1 to k.
34
13/04/2025
Adding the k-th vertex to the “detour pool” can only help if the best path detours
through k.
We already know the best way to get from i to k (using only vertices in 1…k-1) and
we know the best way to get from k to j (using only vertices in 1…k-1).
Thus, minimum of dist(i→k→j) and dist(i→j) gives the minimum distance from i to j
considering the intermediate vertices 1 to k.
Look at the diagonal of the final matrix and return error if a negative value is
found.
A B C D -2
C -2
A 0 -5 -3 -5
B -1 A
5 1 -1 2 D
C 3 -3 -1 -3 6
B -1
D 4 -2 0 -2
FIT2004, Lecture 7 - DP Graph Algorithms
35
13/04/2025
Outline
1. Shortest path in a graph with negative weights
2. All-pairs shortest paths
3. Transitive Closure
Solution: Assign each edge a weight 1 and then apply Floyd-Warshall algorithm.
If dist[i][j] is not infinity, this means that there is a path from i to j in the original
graph. (Or just maintain True and False as shown next).
u t u t
Transitive
s Closure s
v x v x
FIT2004, Lecture 7 - DP Graph Algorithms
36
13/04/2025
Time Complexity:
O(V3)
Space Complexity:
O(V2)
Reading
Course Notes: Chapter 8
You can also check algorithms’ textbooks for contents related to this
lecture, e.g.:
CLRS: Sections 24.1 and 25.2
KT: Sections 6.8, 6.9 and 6.10
Rou: Chapter 18
37
13/04/2025
Concluding Remarks
Take home message
Dijkstra’s algorithm works only for graphs with non-negative weights.
Bellman-Ford computes shortest paths in graphs with negative weights in
O(VE) and can also detect the negative cycles that are reachable.
Floyd-Warshall Algorithm computes all-pairs shortest paths and transitive
closure in O(V3).
Things to do (this list is not exhaustive)
Go through recommended reading and make sure you understand why
the algorithms are correct.
Implement Bellman-Ford and Floyd-Warshall Algorithms.
Coming Up Next
Network Flow
38