0% found this document useful (0 votes)
7 views

Seminar 7 - Dynamic Programming Graph Algorithms

This document outlines the Week 7 lecture for the FIT2004 course at Monash University, focusing on dynamic programming and graph algorithms. It covers key topics such as the Bellman-Ford algorithm for finding shortest paths in graphs with negative weights, the Floyd-Warshall algorithm for all-pairs shortest paths, and the concept of transitive closure. The lecture also emphasizes the importance of understanding the limitations of Dijkstra's algorithm in the presence of negative weights.

Uploaded by

ttri0026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Seminar 7 - Dynamic Programming Graph Algorithms

This document outlines the Week 7 lecture for the FIT2004 course at Monash University, focusing on dynamic programming and graph algorithms. It covers key topics such as the Bellman-Ford algorithm for finding shortest paths in graphs with negative weights, the Floyd-Warshall algorithm for all-pairs shortest paths, and the concept of transitive closure. The lecture also emphasizes the importance of understanding the limitations of Dijkstra's algorithm in the presence of negative weights.

Uploaded by

ttri0026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

13/04/2025

Faculty of Information Technology,


Monash University

FIT2004: Algorithms and Data


Structures

Week 7: Dynamic Programming


Graph Algorithms

1
13/04/2025

Outline

Divide and Greedy Dynamic Data


Network flow
conquer algorithms programming structures
(W 8-9)
(W 1-3) (W 4-5) (W 6-7) (W 10-11)

 Last Lecture: DP algorithms


 Coins Change
 Knapsack
 Edit Distance

 Today’s Lecture: DP graph algorithms


 Shortest path in graphs with negative weights
 All-pairs shortest paths
 Transitive Closure
FIT2004, Lecture 7 - DP Graph Algorithms

Outline
1. Shortest path in a graph with negative weights
(Bellman-Ford Algorithm)
2. All-pairs shortest paths (Floyd-Warshall Algorithm)
3. Transitive Closure

FIT2004, Lecture 7 - DP Graph Algorithms

2
13/04/2025

Shortest path (negative weights)


 What is the shortest distance from s to v in this graph?
 If Dijkstra’s algorithm is used on this graph, what will it output as being the
shortest path from s to v?
 Dijkstra’s algorithm is not guaranteed to output the correct answer when
there are negative weights.

6 u

s -4

3
v

FIT2004, Lecture 7 - DP Graph Algorithms

Shortest path (negative weights)


 What is the shortest distance from s to v in this graph?
 If Dijkstra’s algorithm is used on this graph, what will it output as being the
shortest path from s to v?
 Dijkstra’s algorithm is not guaranteed to output the correct answer when
there are negative weights.

6 u

s -4

3
v

FIT2004, Lecture 7 - DP Graph Algorithms

3
13/04/2025

Shortest path (negative weights)


 What is the shortest distance from s to v in this graph?
 If Dijkstra’s algorithm is used on this graph, what will it output as being the
shortest path from s to v?
 Dijkstra’s algorithm is not guaranteed to output the correct answer when
there are negative weights.

6 u

s -4

3
v

FIT2004, Lecture 7 - DP Graph Algorithms

Shortest path (negative weights)


What is the shortest distance from s to x in this graph?

5
6 u t
-2
-3
s 4 7
-4
7
v x
9

FIT2004, Lecture 7 - DP Graph Algorithms

4
13/04/2025

Shortest path (negative weights)


What is the shortest distance from s to x in this graph?
 Not well-defined:
 From s, it is possible to reach the negative cycle u-->v-->t, and from this cycle it is
possible to reach x.
 Given any path P, it is possible to obtain an alternative path P’ with smaller total
weight than P: P’ goes from s to the negative cycle, include as many repetitions of
the negative cycle as necessary, and then reaches x from the negative cycle.
5
6 u t
-2
-3
s 4 7
-4
7
v x
9

FIT2004, Lecture 7 - DP Graph Algorithms

Bellman-Ford Algorithm
 Bellman-Ford's algorithm works correctly even with negative edge weights
(as long as there are no negative weight cycles).

 Bellman-Ford algorithm returns:


 shortest distances from s to all vertices in the graph if there are no negative cycles
that are reachable from s.
 an error if there is a negative cycle reachable from s (i.e., can be used to detect
negative cycles).

 It can be modified to return all valid shortest distances, and minus ∞ for
vertices which are affected by the negative cycle.

FIT2004, Lecture 7 - DP Graph Algorithms

5
13/04/2025

Bellman-Ford Algorithm: Core Idea


 Idea: If no negative cycles are reachable from node s, then for every node t
that is reachable from s, there is a shortest path from s to t that is simple (i.e.,
no nodes are repeated).

 Can the shortest path from s to t have a positive cycle?

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?

• A cycle should revisit at least one node.


• If the cycle is positive (i.e., the total weight of the cycle is > 0), then going
through it increases the total cost of the path.
• So a shorter path can be found by just skipping that cycle.
• Therefore, the shortest path will always be simple (i.e., no repeated nodes).

FIT2004, Lecture 7 - DP Graph Algorithms

Bellman-Ford Algorithm: Core Idea


 If no negative cycles are reachable from node s, then for every node t that is
reachable from s there is a shortest path from s to t that is simple (i.e., no
nodes are repeated).
 Cycles with positive weight cannot be a part of a shortest path.
 Given a shortest path that contains cycles of weight 0, the cycles can be removed to
obtain an alternative shortest path that is simple.
5
6 u t
-2

s -3
5 7
-4
7
v x
9

 Note that any simple path has at most V-1 edges.

FIT2004, Lecture 7 - DP Graph Algorithms

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.

Richard Bellman Lester Ford Jr.

FIT2004, Lecture 7 - DP Graph Algorithms

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)

 Let P be an optimal path with at most i edges that achieves total


weight OPT(i,v):
 If P has at most i-1 edges, then OPT(i,v) = OPT(i-1,v).
 If P has exactly i edges and (u,v) is the last edge of P, then
OPT(i,v) = OPT(i-1,u) + w(u,v), where w(u,v) denotes the weight of edge (u,v).

 Recursive formula for dynamic programming:


𝑂𝑃𝑇 𝑖, 𝑣
= min(𝑂𝑃𝑇 𝑖 − 1, 𝑣 , min (𝑂𝑃𝑇 𝑖 − 1, 𝑢 + 𝑤(𝑢, 𝑣)))
𝑢: 𝑢,𝑣 ∈𝐸

FIT2004, Lecture 7 - DP Graph Algorithms

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]

𝑂𝑃𝑇 𝑖, 𝑣 = min(𝑂𝑃𝑇 𝑖 − 1, 𝑣 , min (𝑂𝑃𝑇 𝑖 − 1, 𝑢 + 𝑤(𝑢, 𝑣)))


𝑢: 𝑢,𝑣 ∈𝐸

What is the time complexity of Bellman-ford algorithm?

FIT2004, Lecture 7 - DP Graph Algorithms

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]

What is the time complexity of Bellman-ford algorithm?

Time Complexity:
O(VE)

FIT2004, Lecture 7 - DP Graph Algorithms

Bellman-Ford Algorithm
 Commonly, a more space-efficient version of Bellman-Ford
algorithm is implemented.

 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 the rule
𝑀[𝑣] = min(𝑀 𝑣 , min (𝑀[𝑢] + 𝑤(𝑢, 𝑣)))
𝑢: 𝑢,𝑣 ∈𝐸

 In some cases, this version also provides a speed-up (but no


improvement in the worst-case time complexity).

FIT2004, Lecture 7 - DP Graph Algorithms

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:

𝑑𝑖𝑠𝑡[𝑣] = min(𝑑𝑖𝑠𝑡 𝑣 , min (𝑑𝑖𝑠𝑡[𝑢] + 𝑤(𝑢, 𝑣)))


𝑢: 𝑢,𝑣 ∈𝐸
 If vertices are updated in the order s, v, x, t, u, then we are done after 1
iteration.
 On the other hand, if vertices are updated in the order u, t, x, v, s, then we
need 4 iterations to get the right result.
 We will analyse the early stopping condition later on.
∞ ∞
i s v x t u u t x v s
u t
-2
1 0 7 16 23 21 ∞ ∞ ∞ 7 0
0
2 ∞ ∞ 16 7 0 s 7
3 ∞ 23 16 7 0
4 7
21 23 16 7 0 v x
∞ 9

FIT2004, Lecture 7 - DP Graph Algorithms

Bellman-Ford Algorithm: Example


Initialize:
 For each vertex a in the graph
 dist(s,a) = ∞

 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

Bellman-Ford Algorithm: Example


First iteration:

Relaxing incoming edges of node u

∞ ∞
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

Bellman-Ford Algorithm: Example


First iteration:

Done relaxing incoming edges of node u

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

Bellman-Ford Algorithm: Example


First iteration:

Relaxing incoming edges of node v

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

Bellman-Ford Algorithm: Example


First iteration:

Done relaxing incoming edges of node v

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

Bellman-Ford Algorithm: Example


First iteration:

Relaxing incoming edges of node t

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

Bellman-Ford Algorithm: Example


First iteration:

Done relaxing incoming edges of node t

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

Bellman-Ford Algorithm: Example


First iteration:

Relaxing incoming edges of node x

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

Bellman-Ford Algorithm: Example


First iteration:

Done relaxing incoming edges of node x

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

Bellman-Ford Algorithm: Example


First iteration finished:

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

Bellman-Ford Algorithm: Example


Second iteration:

Relaxing incoming edges of node u

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

Bellman-Ford Algorithm: Example


Second iteration:

Done relaxing incoming edges of node u

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

Bellman-Ford Algorithm: Example


Second iteration:

Relaxing incoming edges of node v

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

Bellman-Ford Algorithm: Example


Second iteration:

Done relaxing incoming edges of node v

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

Bellman-Ford Algorithm: Example


Second iteration:

Relaxing incoming edges of node t

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

Bellman-Ford Algorithm: Example


Second iteration:

Done Relaxing incoming edges of node t

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

Bellman-Ford Algorithm: Example


Second iteration:

Relaxing incoming edges of node x

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

Bellman-Ford Algorithm: Example


Second iteration:

Done Relaxing incoming edges of node x

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

Bellman-Ford Algorithm: Example


Second iteration finished:

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

Bellman-Ford Algorithm: Example


Third iteration:

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

Finding Negative Cycles


 If V-th iteration reduces the distance of a vertex, it tells us that there is a
shorter path with at least V edges.
 This implies that there is a negative cycle.
 For example, consider the graph with 4 vertices s, u, v, and t and assume
we have run (V-1 = 3) iterations.
 In the 4th iteration, the weight of at least one vertex will be reduced
(due to the presence of a negative cycle).
 Important: Bellman-Ford Algorithm finds negative cycles only if such
cycle is reachable from the source vertex.
 E.g., if x is the source vertex, the algorithm will not detect the negative cycle
2 4
u t
-2

0 s 4 -3

7 9
v x
7
6
FIT2004, Lecture 7 - DP Graph Algorithms

20
13/04/2025

Detecting Negative Cycles


To detect if a graph G has a negative cycle:
Just add one extra node (z) to G and edges from it to every
other node, and run Bellman-Ford on the added node, z.

0
Z 0
2 4
0 u t
0 -2
0
0 s 4 -3

7 9
v x
7
6

FIT2004, Lecture 7 - DP Graph Algorithms

Updated Bellman-Ford Algorithm


# STEP 1: Initializations
dist[1...V] = infinity
pred[1...V] = Null
dist[s] = 0
# STEP 2: Iteratively estimate dist[v] (from source s)
for i = 1 to V-1:
for each edge <u,v> in the whole graph:
est = dist[u] + w(u,v)
if est < dist[v]:
dist[v] = est
pred[v] = u

# STEP 3: Checks and returns false if a negative weight cycle


# is along the path from s to any other vertex
for each edge <u,v> in the whole graph:
if dist[u]+w(u,v) < dist[v] :
return error; # negative edge cycle found in this graph

return dist[...], pred[...] Time Complexity:


O(VE)

FIT2004, Lecture 7 - DP Graph Algorithms

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

Handling Negative Cycles


 How could we modify Bellman-Ford to determine which
vertices have valid distances, and which are affected by
the negative cycle?
 Execute V additional iterations, and for each node
whose distance would be updated, just mark its distance
as -∞.
 By continuing to relax the graph for V more iterations, the effect
of negative cycles is allowed to propagate across the graph.
 Therefore, any vertex that can be reached from a negative cycle
will eventually get -∞ as its distance.

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

FIT2004, Lecture 7 - DP Graph Algorithms

All-Pairs Shortest Paths


Problem
 Return shortest distances between all pairs of vertices in a
connected graph.
For unweighted graphs:
 For each vertex v in the graph
 Call Breadth-First Search (BFS) for v
Time complexity:
O(V(V+E)) = O(V2 + EV) ➔ O(EV) [for connected graphs, O(V) ≤ O(E)]
For dense graphs: E  O(V2), therefore total cost is O(V3 ) for dense
graphs

FIT2004, Lecture 7 - DP Graph Algorithms

23
13/04/2025

All-Pairs Shortest Paths


For weighted graphs (with non-negative weights):
 For each vertex v in the graph
 Call Dijkstra’s algorithm for v
Time complexity:
O(V(E log V)) = O(EV log V)
For dense graphs: O(V3 log V) since E  O(V2)

FIT2004, Lecture 7 - DP Graph Algorithms

All-Pairs Shortest Paths


For weighted graphs (allowing negative weights):
 For each vertex v in the graph
 Call Bellman-Ford algorithm for v
Time complexity:
O(V(VE)) = O(V2 E)
For dense graphs: O(V4 ) since E  O(V2)
Can we do better?
 Yes, Floyd-Warshall algorithm returns all-pairs
shortest distances in O(V3) (even for graphs with
negative weights).

FIT2004, Lecture 7 - DP Graph Algorithms

24
13/04/2025

Floyd-Warshall Algorithm
 Algorithm based on dynamic programming.

Robert W. Floyd Stephen Warshall


(Turing Award 1978)

FIT2004, Lecture 7 - DP Graph Algorithms

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}.

FIT2004, Lecture 7 - DP Graph Algorithms

25
13/04/2025

Key idea behind Floyd-Warshall Algorithm

 For each pair of vertices(i,j), find whether there is a


shorter path from i to j by going through an intermediate
vertex k.
 All variables i,j,k refer to vertices.
 At each step, it updates:
dist[i][j] = min(dist[i][j],
dist[i][k]+dist[k][j])
 It does this for every possible value of k from 0 to V - 1.
 After the algorithm finishes, check for all vertices:
dist[i][i] < 0 which tells that a Negative weight cycle is
detected.

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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)

First iteration of outer loop (i.e., k is A): k= A B C D


The remaining pairs are (C,A), (C,B), (C,C), (C,D), (D,A), (D,B), i= A B C D
(D,C) and (D,D).
j= A B C D
It is not possible to improve the distance between any
of these pairs of nodes using only A as intermediate. dist[i][j] > dist[i][k] + dist[k][j]
Hence, we move to next iteration.
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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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)

FIT2004, Lecture 7 - DP Graph Algorithms

Floyd-Warshall Algorithm: Correctness


Invariant: dist[i][j] corresponds to the shortest path from
i to j considering only intermediate vertices 1 to k-1.

Base Case k = 1 (i.e. there are no intermediate vertices yet):


 It is true because dist[][] is initialized based only on the adjacent edges.

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.

FIT2004, Lecture 7 - DP Graph Algorithms

34
13/04/2025

Floyd-Warshall Algorithm: Correctness


Invariant: dist[i][j] corresponds to the shortest path from
i to j considering only intermediate vertices 1 to k-1

 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.

FIT2004, Lecture 7 - DP Graph Algorithms

Floyd-Warshall Algorithm: Negative Cycles


 If there is a negative cycle, there will be a vertex v such that dist[v][v] is
negative.

 Look at the diagonal of the final matrix and return error if a negative value is
found.

 How could you modify the algorithm to return the paths?


 Add a "predecessor" matrix that stores the path information when the
shortest distances are updated.

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

FIT2004, Lecture 7 - DP Graph Algorithms

Transitive Closure of a Graph


 Given a graph G = (V,E), its transitive closure is another graph (V,E’) that
contains the same vertices V but contains an edge from node u to node v if
there is a path from u to v in the original graph.

 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

Floyd-Warshall Algorithm for Transitive Closure


# Modify Floyd-Warshall Algorithm to compute Transitive Closure
# initialization
for vertex i in 1..V:
for vertex j in 1..V:
if there is an edge between i and j or i == j:
TC[i][j] = True
else:
TC[i][j] = False
for vertex k in 1..V:
# Invariant: TC[i][j] corresponds to the existence of 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:
TC[i][j] = TC[i][j] or (TC[i][k] and TC[k][j])

Time Complexity:
O(V3)
Space Complexity:
O(V2)

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

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

FIT2004, Lecture 7 - DP Graph Algorithms

38

You might also like