0% found this document useful (0 votes)
7 views48 pages

ADE Tutorial 8

The document covers the application of Prim's algorithm for constructing Minimum Spanning Trees (MSTs) and the Floyd-Warshall algorithm for solving the all-pairs shortest path problem using dynamic programming. It includes examples and explanations of how to implement these algorithms, along with important notations and initializations for the Floyd-Warshall algorithm. The session outcomes emphasize the understanding and application of these algorithms in graph theory.

Uploaded by

cemharwood
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 views48 pages

ADE Tutorial 8

The document covers the application of Prim's algorithm for constructing Minimum Spanning Trees (MSTs) and the Floyd-Warshall algorithm for solving the all-pairs shortest path problem using dynamic programming. It includes examples and explanations of how to implement these algorithms, along with important notations and initializations for the Floyd-Warshall algorithm. The session outcomes emphasize the understanding and application of these algorithms in graph theory.

Uploaded by

cemharwood
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/ 48

COMP2054 Tutorial Session 8:

MSTs & Floyd-Warshall Algorithm

Rebecca Tickle
Warren Jackson
Cas Widdershoven
Session outcomes

▪ Apply Prim’s algorithm to construct the MST from a graph.


▪ Understand how to solve all-pairs shortest path problem using
dynamic programming.
▪ Apply Floyd-Warshall to directed graphs to solve all-pairs shortest
path problem.
Minimum Spanning Trees

Prim’s Algorithm: An optimal greedy approach

3
Which of the following is an MST?

2 2
1 B D 1 1 B D 1

A 2 3 E A 2 3 E
3 3 3 3
C F C F
7 7

4
Which of the following is an MST?

3 3
A B A B
3 3
3 5 3 5
4 4

C D C D
5 5

5
MST: Prim’s Algorithm

Given the below graph and starting node A, create an associated


minimum spanning tree. You should show all steps and give the final
total cost of the MST.

7
A B
2

C 10 12

6 E
D
13
6
MST: Prim’s Algorithm
Given the below graph and starting node A, create an associated
minimum spanning tree. You should show all steps and give the final
total cost of the MST.
MST Nodes not in MST
Start at node A {B,C,D,E}
7
A B
{(A,C)} {B,D,E}
2 {(A,C), (C,D)} {B,E}
{(A,C), (C,D), (A,B)} {E}
C 10 12 {(A,C), (C,D), (A,B), (B,E)} {}

6 E
Total cost = 2 + 6 + 7 + 12 = 27
D
13
7
Floyd-Warshall
Dynamic programming algorithm for
all-pairs shortest paths

8
All-pairs shortest paths problem

▪ Given a directed or undirected graph, find the shortest paths (costs)


between all pairs of nodes.

B
2 1

A 3 D

-1 0
C
9
Floyd-Warshall algorithm

▪ Given a directed or undirected graph, find the shortest paths (costs)


between all pairs of nodes.
▪ Uses dynamic programming to build up the graph from:
▪ No intermediate nodes…
▪ …to considering all nodes being allowed as intermediate nodes.

10
Important notations

▪ 𝑑 𝑖, 𝑗, 𝑘 - the shortest distance between nodes 𝑖 and 𝑗 through some


subset (including the empty set) of 𝑉1 , … , 𝑉𝑘 .

11
Important notations

▪ 𝑑 𝑖, 𝑗, 𝑘 - the shortest distance between nodes 𝑖 and 𝑗 through some


subset (including the empty set) of 𝑉1 , … , 𝑉𝑘 .
▪ 𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝑑 𝑖, 𝑘, 𝑘−1 + 𝑑 𝑘, 𝑗, 𝑘−1

𝑉𝑘

𝑉𝑖 𝑉𝑗

12
Floyd-Warshall Example: Initialisation

▪ Initialise the adjacency matrix

𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏
▪ 𝑑(𝑖, 𝑗, 0)
𝑉1
3 𝑽𝟏 0 ▪ Allowed intermediate
7 nodes: {}
𝑽𝟐 0
𝑉3
𝑉2 𝑽𝟑 0 All 𝑑 𝑖, 𝑖 = 0
2

13
Floyd-Warshall Example: Initialisation

▪ Initialise the adjacency matrix

𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏
▪ 𝑑(𝑖, 𝑗, 0)
𝑉1
3 𝑽𝟏 0 7 3 ▪ Allowed intermediate
7
nodes: {}
𝑽𝟐 7 0
𝑉3
𝑽𝟑 3 2 0 If there is an edge
𝑉2 2 linking two nodes, add
the weight to the
adjacency matrix.

14
Floyd-Warshall Example: Initialisation

▪ Initialise the adjacency matrix

𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏
▪ 𝑑(𝑖, 𝑗, 0)
𝑉1
3 𝑽𝟏 0 7 3 ▪ Allowed intermediate
7 nodes: {}
𝑽𝟐 7 0 ∞
𝑉3
𝑉2 𝑽𝟑 3 2 0 If there is no edge, and
2
we cannot get from
one node to another
directly, we add ∞

15
Floyd-Warshall Example
▪ Using the definition of:
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝑑 𝑖, 𝑘, 𝑘−1 + 𝑑 𝑘, 𝑗, 𝑘−1
▪ Repeat for 𝑘 = 1 to 𝐾 (the number of vertices):
Insert 𝑉𝑘 as an intermediate node and update the matrix

𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏
𝑉1
3 𝑽𝟏 0 7 3
7 𝑽𝟐 7 0 ∞
𝑉3
𝑉2 𝑽𝟑 3 2 0
2
16
Floyd-Warshall Example: k=1
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝑑 𝑖, 𝑘, 𝑘−1 + 𝑑 𝑘, 𝑗, 𝑘−1

▪𝑘=1
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝑑 𝑖, 1,0 + 𝑑(1, 𝑗, 0)]
▪ Intermediate nodes = {𝑉1 }

𝑘=0
𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑉1 𝑖 𝟏
3
𝑽𝟏 0 7 3
7
𝑉3 𝑽𝟐 7 0 ∞
𝑉2 2 𝑽𝟑 3 2 0
17
Floyd-Warshall Example: k=1
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝑑 𝑖, 𝑘, 𝑘−1 + 𝑑 𝑘, 𝑗, 𝑘−1

▪𝑘=1
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝑑 𝑖, 1,0 + 𝑑(1, 𝑗, 0)]
▪ Intermediate nodes = {𝑉1 }

𝑘=0
𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑉1 𝑖 𝟏
3
𝑽𝟏 0 7 3
7
𝑉3 𝑽𝟐 7 0 ∞
𝑉2 2 𝑽𝟑 3 2 0
18
Floyd-Warshall Example: k=1
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝑑 𝑖, 𝑘, 𝑘−1 + 𝑑 𝑘, 𝑗, 𝑘−1

▪𝑘=1
𝑑 1,1,1 = min 𝑑 1,1,0 , 𝑑 1,1,0 + 𝑑 1,1,0
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝑑 𝑖, 1,0 + 𝑑(1, 𝑗, 0)] = min 0,0 + 0 = 0
▪ Intermediate nodes = {𝑉1 }

𝑘=0 𝑘=1
𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑉1 𝑖 𝟏 𝑖 𝟏
3
𝑽𝟏 0 7 3 𝑽𝟏 0
7
𝑉3 𝑽𝟐 7 0 ∞ 𝑽𝟐
𝑉2 2 𝑽𝟑 3 2 0 𝑽𝟑
19
Floyd-Warshall Example: k=1
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝑑 𝑖, 𝑘, 𝑘−1 + 𝑑 𝑘, 𝑗, 𝑘−1

▪𝑘=1
𝑑 1,2,1 = min 𝑑 1,2,0 , 𝑑 1,1,0 + 𝑑 1,2,0
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝑑 𝑖, 1,0 + 𝑑(1, 𝑗, 0)] = min 7,0 + 7 = 7
▪ Intermediate nodes = {𝑉1 }

𝑘=0 𝑘=1
𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑉1 𝑖 𝟏 𝑖 𝟏
3
𝑽𝟏 0 7 3 𝑽𝟏 0 7
7
𝑉3 𝑽𝟐 7 0 ∞ 𝑽𝟐
𝑉2 2 𝑽𝟑 3 2 0 𝑽𝟑
20
Floyd-Warshall Example: k=1
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝑑 𝑖, 𝑘, 𝑘−1 + 𝑑 𝑘, 𝑗, 𝑘−1

▪𝑘=1
𝑑 2,3,1 = min 𝑑 2,3,0 , 𝑑 2,1,0 + 𝑑 1,3,0
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝑑 𝑖, 1,0 + 𝑑(1, 𝑗, 0)] = min ∞, 7 + 3 = 10
▪ Intermediate nodes = {𝑉1 }

𝑘=0 𝑘=1
𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑉1 𝑖 𝟏 𝑖 𝟏
3
𝑽𝟏 0 7 3 𝑽𝟏 0 7
7
𝑉3 𝑽𝟐 7 0 ∞ 𝑽𝟐 10
𝑉2 2 𝑽𝟑 3 2 0 𝑽𝟑
21
Floyd-Warshall Example: k=1
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝑑 𝑖, 𝑘, 𝑘−1 + 𝑑 𝑘, 𝑗, 𝑘−1

▪𝑘=1
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝑑 𝑖, 1,0 + 𝑑(1, 𝑗, 0)]
▪ Intermediate nodes = {𝑉1 }

𝑘=0 𝑘=1
𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑉1 𝑖 𝟏 𝑖 𝟏
3
𝑽𝟏 0 7 3 𝑽𝟏 0 7 3
7
𝑉3 𝑽𝟐 7 0 ∞ 𝑽𝟐 7 0 10
𝑉2 2 𝑽𝟑 3 2 0 𝑽𝟑 3 2 0
22
Floyd-Warshall Example
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝑑 𝑖, 𝑘, 𝑘−1 + 𝑑 𝑘, 𝑗, 𝑘−1

▪ Working cell-by-cell in this way is quite laborious and error prone.


▪ There is a “shortcut” we can use to make the working more straightforward…

𝑘=0 𝑘=1
𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑉1 𝑖 𝟏 𝑖 𝟏
3
𝑽𝟏 0 7 3 𝑽𝟏 0 7 3
7
𝑉3 𝑽𝟐 7 0 ∞ 𝑽𝟐 7 0 10
𝑉2 2 𝑽𝟑 3 2 0 𝑽𝟑 3 2 0
23
Floyd-Warshall Example: Shortcut
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝒅 𝒊, 𝒌, 𝒌−𝟏 + 𝒅 𝒌, 𝒋, 𝒌−𝟏 𝑉1
3
7
▪𝑘=1
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝒅 𝒊, 𝟏, 𝟎 + 𝒅(𝟏, 𝒋, 𝟎)]
𝑉3
𝑉2 2
▪ Intermediate nodes = {𝑉1 }

𝑘=0 𝑘=1
𝑗 𝑽 𝑽𝟐 𝑽𝟑 To calculate the cost matrix 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝑖 𝟏
for 𝑘 = 1, we want to
𝑽𝟏 0 7 3 consider 𝑉1 as the 𝑽𝟏
intermediate node(s). Hence
𝑽𝟐 7 0 ∞ we use the row and column 𝑽𝟐
𝑽𝟑 3 2 0 corresponding to 𝑉1 (left).
𝑽𝟑
24
Floyd-Warshall Example: Shortcut
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝒅 𝒊, 𝒌, 𝒌−𝟏 + 𝒅 𝒌, 𝒋, 𝒌−𝟏 𝑉1
3
7
▪𝑘=1
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝒅 𝒊, 𝟏, 𝟎 + 𝒅(𝟏, 𝒋, 𝟎)]
𝑉3
𝑉2 2
▪ Intermediate nodes = {𝑉1 }
𝑘=0 Intermediate sum 𝑘=1
𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 7 3 𝑽𝟏 0 𝑽𝟏
𝑽𝟐 7 0 ∞ 𝑽𝟐 7 𝑽𝟐
𝑽𝟑 3 2 0 𝑽𝟑 3 𝑽𝟑
0 7 3
Floyd-Warshall Example: Shortcut
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝒅 𝒊, 𝒌, 𝒌−𝟏 + 𝒅 𝒌, 𝒋, 𝒌−𝟏 𝑉1
3
7
▪𝑘=1
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝒅 𝒊, 𝟏, 𝟎 + 𝒅(𝟏, 𝒋, 𝟎)]
𝑉3
𝑉2 2
▪ Intermediate nodes = {𝑉1 }
𝑘=0 Intermediate sum 𝑘=1
𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 7 3 𝑽𝟏 0+0 0+7 0+3 0 𝑽𝟏


𝑽𝟐 7 0 ∞ 𝑽𝟐 7+0 7+7 7+3 7 𝑽𝟐
𝑽𝟑 3 2 0 𝑽𝟑 3+0 3+7 3+3 3 𝑽𝟑
0 7 3
Floyd-Warshall Example: Shortcut
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝒅 𝒊, 𝒌, 𝒌−𝟏 + 𝒅 𝒌, 𝒋, 𝒌−𝟏 𝑉1
3
7
▪𝑘=1
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝒅 𝒊, 𝟏, 𝟎 + 𝒅(𝟏, 𝒋, 𝟎)]
𝑉3
𝑉2 2
▪ Intermediate nodes = {𝑉1 }
𝑘=0 Intermediate sum 𝑘=1
𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 7 3 𝑽𝟏 0 7 3 0 𝑽𝟏
𝑽𝟐 7 0 ∞ 𝑽𝟐 7 14 10 7 𝑽𝟐
𝑽𝟑 3 2 0 𝑽𝟑 3 10 6 3 𝑽𝟑
0 7 3
Floyd-Warshall Example: Shortcut
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝒅 𝒊, 𝒌, 𝒌−𝟏 + 𝒅 𝒌, 𝒋, 𝒌−𝟏 𝑉1
3
7
▪𝑘=1
▪ 𝑑 𝑖, 𝑗, 1 = min[𝑑 𝑖, 𝑗, 0 , 𝒅 𝒊, 𝟏, 𝟎 + 𝒅(𝟏, 𝒋, 𝟎)]
𝑉3
𝑉2 2
▪ Intermediate nodes = {𝑉1 }
𝑘=0 Intermediate sum 𝑘 = 1 as 𝒎𝒊𝒏[𝒌 = 𝟎, 𝐈𝐧𝐭𝐞𝐫𝐦𝐞𝐝𝐢𝐚𝐭𝐞 𝐬𝐮𝐦]

𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 7 3 𝑽𝟏 0 7 3 0 𝑽𝟏 0 7 3
𝑽𝟐 7 0 ∞ 𝑽𝟐 7 14 10 7 𝑽𝟐 7 0 10
𝑽𝟑 3 2 0 𝑽𝟑 3 10 6 3 𝑽𝟑 3 2 0
0 7 3
Floyd-Warshall Example: Shortcut
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝒅 𝒊, 𝒌, 𝒌−𝟏 + 𝒅 𝒌, 𝒋, 𝒌−𝟏 𝑉1
3
7
▪𝑘=2
▪ 𝑑 𝑖, 𝑗, 2 = min[𝑑 𝑖, 𝑗, 1 , 𝒅 𝒊, 𝟐, 𝟏 + 𝒅(𝟐, 𝒋, 𝟏)]
𝑉3
𝑉2 2
▪ Intermediate nodes = {𝑉1 , 𝑉2 }
𝑘=1 Intermediate sum 𝑘 = 2 as 𝒎𝒊𝒏[𝒌 = 𝟏, 𝐈𝐧𝐭𝐞𝐫𝐦𝐞𝐝𝐢𝐚𝐭𝐞 𝐬𝐮𝐦]

𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 7 3 𝑽𝟏 7 𝑽𝟏
𝑽𝟐 7 0 10 𝑽𝟐 0 𝑽𝟐
𝑽𝟑 3 2 0 𝑽𝟑 2 𝑽𝟑
7 0 10
Floyd-Warshall Example: Shortcut
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝒅 𝒊, 𝒌, 𝒌−𝟏 + 𝒅 𝒌, 𝒋, 𝒌−𝟏 𝑉1
3
7
▪𝑘=2
▪ 𝑑 𝑖, 𝑗, 2 = min[𝑑 𝑖, 𝑗, 1 , 𝒅 𝒊, 𝟐, 𝟏 + 𝒅(𝟐, 𝒋, 𝟏)]
𝑉3
𝑉2 2
▪ Intermediate nodes = {𝑉1 , 𝑉2 }
𝑘=1 Intermediate sum 𝑘 = 2 as 𝒎𝒊𝒏[𝒌 = 𝟏, 𝐈𝐧𝐭𝐞𝐫𝐦𝐞𝐝𝐢𝐚𝐭𝐞 𝐬𝐮𝐦]

𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 7 3 𝑽𝟏 14 7 17 7 𝑽𝟏
𝑽𝟐 7 0 10 𝑽𝟐 7 0 10 0 𝑽𝟐
𝑽𝟑 3 2 0 𝑽𝟑 9 2 12 2 𝑽𝟑
7 0 10
Floyd-Warshall Example: Shortcut
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝒅 𝒊, 𝒌, 𝒌−𝟏 + 𝒅 𝒌, 𝒋, 𝒌−𝟏 𝑉1
3
7
▪𝑘=2
▪ 𝑑 𝑖, 𝑗, 2 = min[𝑑 𝑖, 𝑗, 1 , 𝒅 𝒊, 𝟐, 𝟏 + 𝒅(𝟐, 𝒋, 𝟏)]
𝑉3
𝑉2 2
▪ Intermediate nodes = {𝑉1 , 𝑉2 }
𝑘=1 Intermediate sum 𝑘 = 2 as 𝒎𝒊𝒏[𝒌 = 𝟏, 𝐈𝐧𝐭𝐞𝐫𝐦𝐞𝐝𝐢𝐚𝐭𝐞 𝐬𝐮𝐦]

𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 7 3 𝑽𝟏 14 7 17 7 𝑽𝟏 0 7 3
𝑽𝟐 7 0 10 𝑽𝟐 7 0 10 0 𝑽𝟐 7 0 10
𝑽𝟑 3 2 0 𝑽𝟑 9 2 12 2 𝑽𝟑 3 2 0
7 0 10
Floyd-Warshall Example: Shortcut
𝑑 𝑖, 𝑗, 𝑘 = min 𝑑 𝑖, 𝑗, 𝑘−1 , 𝒅 𝒊, 𝒌, 𝒌−𝟏 + 𝒅 𝒌, 𝒋, 𝒌−𝟏 𝑉1
3
7
▪𝑘=3
▪ 𝑑 𝑖, 𝑗, 3 = min[𝑑 𝑖, 𝑗, 2 , 𝒅 𝒊, 𝟑, 𝟐 + 𝒅(𝟑, 𝒋, 𝟐)]
𝑉3
𝑉2 2
▪ Intermediate nodes = {𝑉1 , 𝑉2 , 𝑉3 }
𝑘=2 Intermediate sum 𝑘 = 3 as 𝒎𝒊𝒏[𝒌 = 𝟐, 𝐈𝐧𝐭𝐞𝐫𝐦𝐞𝐝𝐢𝐚𝐭𝐞 𝐬𝐮𝐦]

𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑 𝑗 𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 7 3 𝑽𝟏 6 5 3 3 𝑽𝟏 0 5 3
𝑽𝟐 7 0 10 𝑽𝟐 13 12 10 10 𝑽𝟐 7 0 10
𝑽𝟑 3 2 0 𝑽𝟑 3 2 0 0 𝑽𝟑 3 2 0
3 2 0
Floyd-Warshall Example: Complete Shortcut
This slide demonstrates how each step links together from considering no intermediate nodes 𝑘 = 0
through to all nodes as intermediate nodes 𝑘 = 3 .

Intermediate sum: 𝑗𝑽 𝑽 𝑽 𝑗𝑽 𝑽 𝑽 𝑗𝑽 𝑽 𝑽
𝑖 𝟏 𝟐 𝟑 𝑖 𝟏 𝟐 𝟑 𝑖 𝟏 𝟐 𝟑
𝑽𝟏 0 7 3 𝑽𝟏 14 7 17 𝑽𝟏 6 5 3
𝑽𝟐 7 14 10 𝑽𝟐 7 0 10 𝑽𝟐 13 12 10
𝑽𝟑 3 10 6 𝑽𝟑 9 2 12 𝑽𝟑 3 2 0
𝑘=0 𝑘=1 𝑘=2 𝑘=3
𝑗𝑽 𝑽 𝑽 𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑗𝑽 𝑽 𝑽 𝑗𝑽 𝑽𝟐 𝑽𝟑
𝑖 𝟏 𝟐 𝟑 𝑖 𝟏 𝑖 𝟏 𝟐 𝟑 𝑖 𝟏
𝑽𝟏 0 7 3 𝑽𝟏 0 7 3 𝑽𝟏 0 7 3 𝑽𝟏 0 5 3
𝑽𝟐 7 0 ∞ 𝑽𝟐 7 0 10 𝑽𝟐 7 0 10 𝑽𝟐 7 0 10
𝑽𝟑 3 2 0 𝑽𝟑 3 2 0 𝑽𝟑 3 2 0 𝑽𝟑 3 2 0
Questions

34
Questions: Floyd-Warshall

Use the Floyd-Warshall algorithm to find the matrix of all-pairs shortest


paths for the graphs below.

Q1. Q2.
𝑉2 𝑉1
-2 5
4
3 1
𝑉1 𝑉4
𝑉3
-1 2 𝑉2 3
𝑉3

35
Answers – Q1
This graph has 4 vertices so we should construct a 4x4 matrix using the “shortcut”
method. First initialise with the costs of direct edges from each node to each other
node.
𝑑 𝑖, 𝑖, 0 = 0
𝑘=0
𝑗𝑽 𝑽 𝟐 𝑽 𝟑 𝑽𝟒
𝑖 𝟏 𝑉2
-2
𝑽𝟏 0 4
3
𝑽𝟐 0 𝑉1 𝑉4
𝑽𝟑 0 -1 2
𝑽𝟒 0 𝑉3
Answers – Q1
This graph has 4 vertices so we should construct a 4x4 matrix using the “shortcut”
method. First initialise with the costs of direct edges from each node to each other
node.

Enter all direct edges


𝑘=0
𝑗𝑽 𝑽 𝟐 𝑽 𝟑 𝑽𝟒
𝑖 𝟏 𝑉2
-2
𝑽𝟏 0 4 3 4
3
𝑽𝟐 0 -2 𝑉1 𝑉4
𝑽𝟑 -1 0 -1 2
𝑽𝟒 2 0 𝑉3
Answers – Q1
This graph has 4 vertices so we should construct a 4x4 matrix using the “shortcut”
method. First initialise with the costs of direct edges from each node to each other
node.
…and fill in the blanks (no direct
edges) with infinity.
𝑘=0
𝑗𝑽 𝑽 𝟐 𝑽 𝟑 𝑽𝟒
𝑖 𝟏 𝑉2
-2
𝑽𝟏 0 4 ∞ 3 4
3
𝑽𝟐 ∞ 0 ∞ -2 𝑉1 𝑉4
𝑽𝟑 -1 ∞ 0 ∞ -1 2
𝑽𝟒 ∞ ∞ 2 0 𝑉3
Answers – Q1

𝑘 = 1, calculate intermediate sum for 𝑉1 and take min of 𝑘 = 0 and


intermediate sum to construct matrix for 𝑘 = 1.
𝑗 0 4 ∞ 3
𝑖
0 0 4 ∞ 3
∞ ∞ ∞ ∞ ∞
-1 -1 3 ∞ 2 𝑉2
∞ ∞ ∞ ∞ ∞ -2
𝑘=0 𝑘=1
4
𝑗𝑽 𝑗𝑽 3
𝑖 𝟏 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑖 𝟏 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑉1 𝑉4
𝑽𝟏 0 4 ∞ 3 𝑽𝟏 0 4 ∞ 3
-1 2
𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 ∞ 0 ∞ -2
𝑽𝟑 -1 ∞ 0 ∞ 𝑽𝟑 -1 3 0 2
𝑉3
𝑽𝟒 ∞ ∞ 2 0 𝑽𝟒 ∞ ∞ 2 0
Answers – Q1

𝑘 = 2, calculate intermediate sum for 𝑉1 , 𝑉2 and take min of 𝑘 = 1 and


intermediate sum to construct matrix for 𝑘 = 2.
𝑗 0 4 ∞ 3 𝑗 0 ∞ -2
𝑖 𝑖 ∞
0 0 4 ∞ 3 4 ∞ 4 ∞ 2
∞ ∞ ∞ ∞ ∞ 0 ∞ 0 ∞ -2
-1 -1 3 ∞ 2 3 ∞ 3 ∞ 1 𝑉2
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ -2
𝑘=0 𝑘=1 𝑘=2
4
𝑗𝑽 𝑗𝑽 𝑗𝑽 3
𝑖 𝟏 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑖 𝟏 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑖 𝟏 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑉1 𝑉4
𝑽𝟏 0 4 ∞ 3 𝑽𝟏 0 4 ∞ 3 𝑽𝟏 0 4 ∞ 2
-1 2
𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 ∞ 0 ∞ -2
𝑽𝟑 -1 ∞ 0 ∞ 𝑽𝟑 -1 3 0 2 𝑽𝟑 -1 3 0 1
𝑉3
𝑽𝟒 ∞ ∞ 2 0 𝑽𝟒 ∞ ∞ 2 0 𝑽𝟒 ∞ ∞ 2 0
Answers – Q1

𝑘 = 3, calculate intermediate sum for 𝑉1 , 𝑉2 , 𝑉3 and take min of 𝑘 = 2


and intermediate sum to construct matrix for 𝑘 = 3.
𝑗 0 4 ∞ 3 𝑗 0 ∞ -2 𝑗 -1 3 0 1
𝑉2 𝑖 𝑖 ∞ 𝑖
-2
4 0 0 4 ∞ 3 4 ∞ 4 ∞ 2 ∞ ∞ ∞ ∞ ∞
3
𝑉1 𝑉4 ∞ ∞ ∞ ∞ ∞ 0 ∞ 0 ∞ -2 ∞ ∞ ∞ ∞ ∞
-1 2 -1 -1 3 ∞ 2 3 ∞ 3 ∞ 1 0 -1 3 0 1
𝑉3 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 2 1 5 2 3
𝑘=0 𝑘=1 𝑘=2 𝑘=3
𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑽𝟒
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 4 ∞ 3 𝑽𝟏 0 4 ∞ 3 𝑽𝟏 0 4 ∞ 2 𝑽𝟏 0 4 ∞ 2
𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 ∞ 0 ∞ -2
𝑽𝟑 -1 ∞ 0 ∞ 𝑽𝟑 -1 3 0 2 𝑽𝟑 -1 3 0 1 𝑽𝟑 -1 3 0 1
𝑽𝟒 ∞ ∞ 2 0 𝑽𝟒 ∞ ∞ 2 0 𝑽𝟒 ∞ ∞ 2 0 𝑽𝟒 1 5 2 0
Answers – Q1
𝑘 = 4, calculate intermediate sum for 𝑉1 , 𝑉2 , 𝑉3 , 𝑉4 and take min of 𝑘 = 3 and
intermediate sum to construct matrix for 𝑘 = 4. This matrix is the final matrix which
gives the shortest path of all pairs of nodes in the graph.
𝑗 0 4 ∞ 3 𝑗 ∞ 0 ∞ -2 𝑗 -1 3 0 1 𝑗 1 5 2 0
𝑉2 𝑖 𝑖 𝑖 𝑖
-2
4 0 0 4 ∞ 3 4 ∞ 4 ∞ 2 ∞ ∞ ∞ ∞ ∞ 2 3 7 4 2
3
𝑉1 𝑉4 ∞ ∞ ∞ ∞ ∞ 0 ∞ 0 ∞ -2 ∞ ∞ ∞ ∞ ∞ -2 -1 3 0 -2
-1 2 -1 -1 3 ∞ 2 3 ∞ 3 ∞ 1 0 -1 3 0 1 1 2 6 3 1
𝑉3 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 2 1 5 2 3 0 1 5 2 0
𝑘=0 𝑘=1 𝑘=2 𝑘=3 𝑘=4
𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑗𝑽 𝑽𝟐 𝑽𝟑 𝑽𝟒
𝑖 𝟏 𝑖 𝟏 𝑖 𝟏 𝑖 𝟏 𝑖 𝟏

𝑽𝟏 0 4 ∞ 3 𝑽𝟏 0 4 ∞ 3 𝑽𝟏 0 4 ∞ 2 𝑽𝟏 0 4 ∞ 2 𝑽𝟏 0 4 4 2
𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 ∞ 0 ∞ -2 𝑽𝟐 -1 0 0 -2
𝑽𝟑 -1 ∞ 0 ∞ 𝑽𝟑 -1 3 0 2 𝑽𝟑 -1 3 0 1 𝑽𝟑 -1 3 0 1 𝑽𝟑 -1 3 0 1
𝑽𝟒 ∞ ∞ 2 0 𝑽𝟒 ∞ ∞ 2 0 𝑽𝟒 ∞ ∞ 2 0 𝑽𝟒 1 5 2 0 𝑽𝟒 1 5 2 0
Answers – Q2

𝑘 = 0, calculate the initial matrix for direct edges only.

𝑉1
5
1
𝑘=0
𝑗𝑽
𝑉3
𝑖 𝑽𝟐 𝑽𝟑
𝟏
𝑉2 3
𝑽𝟏 0 1 ∞
𝑽𝟐 1 0 ∞
𝑽𝟑 5 3 0
Answers – Q2

𝑘 = 1, calculate intermediate sum for 𝑉1 and take min of 𝑘 = 0 and


intermediate sum to construct matrix for 𝑘 = 1.
𝑗 0 1 ∞
𝑖
0 0 1 ∞ 𝑉1
1 1 2 ∞
5
5 5 6 ∞
1
𝑘=0
𝑗𝑽
𝑘=1
𝑗𝑽
𝑉3
𝑖 𝑽𝟐 𝑽𝟑 𝑖 𝑽𝟐 𝑽𝟑
𝟏 𝟏
𝑉2 3
𝑽𝟏 0 1 ∞ 𝑽𝟏 0 1 ∞
𝑽𝟐 1 0 ∞ 𝑽𝟐 1 0 ∞
𝑽𝟑 5 3 0 𝑽𝟑 5 3 0
Answers – Q2

𝑘 = 2, calculate intermediate sum for 𝑉1 , 𝑉2 and take min of 𝑘 = 1 and


intermediate sum to construct matrix for 𝑘 = 2.
𝑗 0 1 ∞ 𝑗 1 0 ∞
𝑖 𝑖
0 0 1 ∞ 1 2 1 ∞ 𝑉1
1 1 2 ∞ 0 1 0 ∞
5
5 5 6 ∞ 3 4 3 ∞
1
𝑘=0
𝑗𝑽
𝑘=1
𝑗𝑽
𝑘=2
𝑗𝑽
𝑉3
𝑖 𝑽𝟐 𝑽𝟑 𝑖 𝑽𝟐 𝑽𝟑 𝑖 𝑽𝟐 𝑽𝟑
𝟏 𝟏 𝟏
𝑉2 3
𝑽𝟏 0 1 ∞ 𝑽𝟏 0 1 ∞ 𝑽𝟏 0 1 ∞
𝑽𝟐 1 0 ∞ 𝑽𝟐 1 0 ∞ 𝑽𝟐 1 0 ∞
𝑽𝟑 5 3 0 𝑽𝟑 5 3 0 𝑽𝟑 4 3 0
Answers – Q2
𝑘 = 3, calculate intermediate sum for 𝑉1 , 𝑉2 , 𝑉3 and take min of 𝑘 = 2 and
intermediate sum to construct matrix for 𝑘 = 3. This matrix is the final matrix which
gives the shortest path of all pairs of nodes in the graph.
𝑗 0 1 ∞ 𝑗 1 0 ∞ 𝑗 4 3 0
𝑖 𝑖 𝑖
0 0 1 ∞ 1 2 1 ∞ ∞ ∞ ∞ ∞ 𝑉1
1 1 2 ∞ 0 1 0 ∞ ∞ ∞ ∞ ∞
5
5 5 6 ∞ 3 4 3 ∞ 0 4 3 0
1
𝑘=0
𝑗𝑽
𝑘=1
𝑗𝑽
𝑘=2
𝑗𝑽
𝑘 =3
𝑗𝑽
𝑉3
𝑖 𝑽𝟐 𝑽𝟑 𝑖 𝑽𝟐 𝑽𝟑 𝑖 𝑽𝟐 𝑽𝟑 𝑖 𝑽𝟐 𝑽𝟑
𝟏 𝟏 𝟏 𝟏
𝑉2 3
𝑽𝟏 0 1 ∞ 𝑽𝟏 0 1 ∞ 𝑽𝟏 0 1 ∞ 𝑽𝟏 0 1 ∞
𝑽𝟐 1 0 ∞ 𝑽𝟐 1 0 ∞ 𝑽𝟐 1 0 ∞ 𝑽𝟐 1 0 ∞ Notice how there are still ∞ in
The final matrix. This represents
𝑽𝟑 5 3 0 𝑽𝟑 5 3 0 𝑽𝟑 4 3 0 𝑽𝟑 4 3 0 The fact there are no paths to 𝑉3.
Questions: MST
Q3. Given the below graph and starting node D, create an associated minimum
spanning tree. You should show all steps and give the final total cost of the MST.

3
A B

2 5
4 2
5
7 D F
C 12 5
5
E
47
Answer – Q3
Given the below graph and starting node D, create an associated minimum
spanning tree. You should show all steps and give the final total cost of the MST.
MST Nodes not in MST
3 Start at node D {A,B,C,E,F}
A B {(D,B)} {A,C,E,F}

2 5 {(D,B), (B,C)} {A,E,F}


4 2 {(D,B), (B,C), (B,A)} {E,F}
5 {(D,B), (B,C), (B,A), {F}
7 D F (C,E)}
C 12 5
{(D,B), (B,C), (B,A), {}
(C,E), (D,F)}
5
E Total cost = 2 + 2 + 3 + 5 + 5 = 17
Note: there are multiple possible MSTs for this question. If you have
48
two candidate edges with the same cost, you can randomly pick one.

You might also like