Minimum Spanning Trees
CME4422 – Introduction to Graph Theory
Asst. Prof. Dr. Feriştah DALKILIÇ
[email protected] Outline
◘ Minimum Spanning Tree (MST)
– Kruskal’s Algorithm
– Prim’s Algorithm
◘ Graph Enumeration Problem
– Cayley’s Theorem
Trees
Let T be a graph with n vertices
◘ T is a tree;
◘ T contains no cycles, has n-1 edges;
◘ T is connected;
◘ Every edge in T is a bridge. Removal of any edge in T
divides the graph into two disjoint sets.
◘ Any two vertices of T are connected by exactly one
unique path.
Spanning Tree
◘ A spanning tree T connects all vertices of a graph G
without any cycles.
◘ To find a spanning tree for any connected graph G,
examine cycles in the graph and remove edges which are
not bridges.
◘ A spanning tree for G is NOT unique! There are other
spanning trees for G.
Example
v w This spanning tree for G is
NOT unique!
x y z There are other spanning
trees for G.
v w
For example
x y z
Minimum Spanning Tree (MST)
◘ Undirected graph G = (V, E)
◘ Weight on each edge
◘ Find such that:
1. connects all vertices ( is spanning tree), and
2. is minimized
A spanning tree whose weight is minimum over all
spanning trees is called a Minimum Spanning Tree (MST)
Algorithms for MST Problem
◘ Kruskal’s Algorithm
◘ Prim’s Algorithm
Kruskal’s Algorithm
Let G ═ (V, E) be a weighted connected graph.
◘ Step-1: Select one edge ei of G such that its weight w(ei) is
minimum.
◘ Step-2:
1. If the edges e1, e2, . . ., ek have been chosen then select an
edge ek+1 such that ek+1 ≠ ei for i ═ 1, 2, . . ., k
2. The edges e1, e2, . . ., ek, ek+1 does not form a circuit.
3. The weight of w(ek+1) is as small as possible subject to the
condition number 2 of step-2 above.
◘ Step-3: Stop, when all the vertices of G are in T which is the
required spanning tree of G with n─1 edges.
Kruskal’s Algorithm
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
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
FIND-SET(u) == FIND-SET(v) => 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
FIND-SET(u) == FIND-SET(v) => 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
FIND-SET(u) == FIND-SET(v) => 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
FIND-SET(u) == FIND-SET(v) => 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
MST path length : 37
Prim's Algorithm
Input: a weighted connected graph G and starting vertex v.
Output: a minimum spanning tree T.
Initialize tree T as vertex v.
Initialize S as the set of proper edges incident on v.
While S
Let = Prim-nextEdge(G, S).
Let be the non-tree endpoint of edge .
Add edge and vertex to tree T.
update Frontier(G; S).
Return tree T.
Let S be the current set of frontier edges.
Prim-nextEdge(G, S) selects and returns as its value the frontier edge
with smallest edge-weight.
Prim's Algorithm
◘ Like Kruskal’s algorithm, Prim’s algorithm is also a greedy
algorithm.
◘ It operates much like Dijkstra’s algorithm.
Prim's Algorithm
Input: A weighted connected graph G = (V, E)
Output: A minimum spanning tree T of G.
1 for each V do
2 cost[]
3 parent[] NULL
4 r arbitrary vertex of V
5 cost[r] 0
6 Q V
7 while Q do
8 extractMin(Q)
9 for each adj() do
10 if Q and (, ) < cost[] then
11 cost[] (, )
12 parent[]
13 T
14 return T
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
A B C D E F G H I
NULL NULL NULL NULL NULL NULL NULL NULL NULL
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
Kruskal’s Algorithm Prim’s Algorithm
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’s time complexity is Prim’s algorithm has a time complexity of
O(E log V), V being the number of vertices. O(V2), V being the number of vertices and
can be improved up to O(E + log V) using
Fibonacci heaps.
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
◘ Phone network design. Connect all offices of your
company via a landline phone system by paying
minimum cost for telephone leasing.
◘ Travelling salesman
◘ Clustering
◘ Broadcast in computer networks
◘ Image segmentation
◘ Circuit design
◘ …
Travelling Salesman Problem
◘ A salesman visits each city in an efficient way = least distance
◘ No general solution, NP-Hard.
TSP Solution - MST
1. Assign A as start and end city of the tour.
2. Construct MST from A
3. List cities visited in preorder walk of the MST, include A to the end
of the tour.
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
◘ A traversal visits the nodes of a tree in a systematic manner
◘ In a preorder traversal, a node is visited before its descendants
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
The preorder of MST is A-C-B-D
TSP tour: A-C-B-D-A
Road length : 80
Graph Enumeration Problem
◘ How many graphs with a given property can be drawn
with a given number of vertices?
◘ The problem was first discussed by Arthur Cayley in
1850s.
Counting Chemical Molecules
𝐶 𝐻 4 𝐶2 𝐻 6 𝐶3 𝐻 8 𝐶 4 𝐻 10 𝐶 4 𝐻 10
◘ An alkane is a molecule with
formula .
◘ butane and isobutane have the
same chemical formula , but they
are different molecules.
◘ How many different molecules are
there with the formula ?
◘ The graph of any molecule is a tree
with 3n+2 vertices and 3n+1 edges. butane
isobutone
◘ We can discard hydrogen atoms
and reduce the problem to finding
the number of trees with n vertices,
each of degree 4 or less.
Counting Graphs with n Vertices
n=3 Labelled Graphs
for n=3, 8
Labelled Trees
,
for n=3, 3
(Cayley’s Formula 1889)
contains exactly
spanning trees.
Unlabelled Graphs
4
Example
◘ Draw the non-isomorphic unlabeled spanning trees with
n=6.
Questions?
Thank you for listening!