04 CME4422 MinimumSpanningTree
04 CME4422 MinimumSpanningTree
v w
For example
x y z
Minimum Spanning Tree (MST)
◘ Kruskal’s Algorithm
◘ Prim’s Algorithm
Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 ✖ 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Otherwise it would
FIND-SET(u) == FIND-SET(v)
create a cycle in MST! => not add (u,v) to A
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7✖ 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Otherwise it would
FIND-SET(u) == FIND-SET(v)
create a cycle in MST! => not add (u,v) to A
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Example: Kruskal’s Algorithm
✖ 8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
Example: Kruskal’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6 ✖
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
8 7
4 B C D
9
2
A 11✖ I 4 14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
8 7
4 B C D
9
2
A 11 I 4 ✖
14 E
7 6
8 10
H G F
1 2
E: 1 2 2 4 4 6 7 7 8 8 9 10 11 14
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
A B C D E F G H I
A B C D E F G H I
During execution of algorithm, all vertices that are not in MST reside in min-
priority queue Q based on the cost attribure.
Example: Prim’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
Q a [] A B C D E F G H I
(,)<cost[] cost[](, )
A B TRUE cost[] 4
[]
NU
LL A NU
LL
NU
LL
NU
LL
NU
LL
NU
LL A NU
LL
A H TRUE cost[] 8 B C D E F G H I
[]
Example: Prim’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
Q a [] A B C D E F G H I
(,)<cost[] cost[](, )
B C TRUE cost[] 8
[]
NU
LL A B NU
LL
NU
LL
NU
LL
NU
LL A NU
LL
B H FALSE Do nothing C D E F G H I
B A FALSE Do nothing
Example: Prim’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
Q a []
(,)<cost[] cost[](, )
C I TRUE cost[] 2
[] A B C D E F G H I
C F TRUE cost[] 4
[]
NU
LL A B C NU
LL C NU
LL A C
C D TRUE cost[] 7
D E F G H I
[]
C B FALSE Do nothing
Example: Prim’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
Q a []
(,)<cost[] cost[](, ) A B C D E F G H I
I H TRUE cost[] 7 7 7
[] NU
LL A B C NU
LL C I I C
I G TRUE cost[] 6
[] D E F G H
I C FALSE Do nothing I
Example: Prim’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
Q a []
(,)<cost[] cost[](, )
F E TRUE cost[] 10 A B C D E F G H I
[]
10 7
F G TRUE cost[] 2 NU
A B C F C F I C
[]
LL
F C FALSE Do nothing D E G H
F D FALSE Do nothing I
Example: Prim’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
Q a [] A B C D E F G H I
(,)<cost[] cost[](, )
2
G H TRUE cost[] 1 NU
A B C F C F G C
[] LL
G I FALSE Do nothing D E H
G F FALSE Do nothing I
Example: Prim’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
Q a []
(,)<cost[] cost[](, )
A B C D E F G H I
H A FALSE Do nothing NU
LL A B C F C F G C
H B FALSE Do nothing
H I FALSE Do nothing D E
H G FALSE Do nothing I H
Example: Prim’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
Q a []
(,)<cost[] cost[](, )
A B C D E F G H I
D E TRUE cost[] 9
[]
NU
LL A B C D C F G C
D C FALSE Do nothing E
D F FALSE Do nothing I H
Example: Prim’s Algorithm
8 7
4 B C D
9
2
A 11 I 4 14 E
7 6
8 10
H G F
1 2
MST path length : 37
Q a [] A B C D E F G H I
(,)<cost[] cost[](, )
E D FALSE Do nothing NU
LL A B C D C F G C
E F FALSE Do nothing
I H E
Example: Prim’s Algorithm
Comparison of Kruskal and Prim
It starts to build the Minimum Spanning Tree It starts to build the Minimum Spanning Tree
from the vertex carrying minimum weight in the from any vertex in the graph.
graph.
It traverses one node only once. It traverses one node more than one time to
get the minimum distance.
Kruskal’s algorithm can generate forest Prim’s algorithm gives connected component
(disconnected components) at any instant as as well as it works only on connected graph.
well as it can work on disconnected
components
Kruskal’s algorithm runs faster in sparse Prim’s algorithm runs faster in dense graphs.
graphs.
Applications of MST
The city tour found by MST always returns a tour whose road length is
less than (or equal) twice the cost of an optimal tour!
Preorder Walk
Algorithm preOrder(v)
visit(v)
for each child w of v
preOrder (w)
TSP Solution - MST
A A
20 20
10 15 10 15
B B
25 30
C D C D
35
for n=3, 8
Labelled Trees
,
for n=3, 3
Unlabelled Graphs
4
Example