You are on page 1of 51

Definition

• A Minimum Spanning Tree (MST) is a


subgraph of an undirected graph such that
the subgraph spans (includes) all nodes, is
connected, is acyclic, and has minimum
total edge weight
MST
• A minimum spanning tree connects all
nodes in a given graph
• A MST must be a connected and undirected
graph
• A MST can have weighted edges
• Multiple MSTs can exist within a given
undirected graph
More about Multiple MSTs
• Multiple MSTs can be generated depending
on which algorithm is used
• If you wish to have an MST start at a
specific node
• However, if there are weighted edges and
all weighted edges are unique, only one
MST will exist
Real Life Application of a MST
A cable TV company is laying cable in a new
neighborhood. If it is constrained to bury the cable
only along certain paths, then there would be a graph
representing which points are connected by those
paths. Some of those paths might be more expensive,
because they are longer, or require the cable to be
buried deeper; these paths would be represented by
edges with larger weights. A minimum spanning tree
would be the network with the lowest total cost.
Cycle Property
f 8
Cycle Property: 4
– Let T be a minimum C 9
spanning tree of a weighted 2 6
graph G 3 e
8 7
– Let e be an edge of G that is
not in T and let C be the
cycle formed by e with T 7
– For every edge f of C, Replacing f with e yields
weight(f) weight(e) a better spanning tree
Proof:
– By contradiction f 8
4
– If weight(f) weight(e) we C
can get a spanning tree of 9
2 6
smaller weight by replacing 3 e
e with f 7
8

Minimum Spanning Trees


7 5
Partition Property
U
7
V
f
Partition Property: 4
– Consider a partition of the vertices of G 9
into subsets U and V 2 5
8
– Let e be an edge of minimum weight
across the partition 8 e 3
– There is a minimum spanning tree of G 7
containing edge e
Proof: Replacing f with e yields
– Let T be an MST of G another MST
– If T does not contain e, consider the cycle U V
C formed by e with T and let f be an f 7
edge of C across the partition 4
– By the cycle property, 9
weight(f) weight(e) 2 5
8
– Thus, weight(f) weight(e)
8 e 3
– We obtain another MST by replacing f
with e 7
Minimum Spanning Trees 6
Borůvka’s Algorithm
• The first MST Algorithm was created by
Otakar Borůvka in 1926
• The algorithm was used to create efficient
connections between the electricity network
in the Czech Republic
• No longer used since Prim’s and Kruskal’s
algorithms were discovered
Prim’s Algorithm
• Initially discovered in 1930 by Vojtěch
Jarník, then rediscovered in 1957 by Robert
C. Prim
• Similar to Dijkstra’s Algorithm regarding a
connected graph
• Starts off by picking any node within the
graph and growing from there
Prim’s Algorithm Cont.
• Label the starting node, A, with a 0 and all
others with infinite
• Starting from A, update all the connected
nodes’ labels to A with their weighted edges
if it less than the labeled value
• Find the next smallest label and update the
corresponding connecting nodes
• Repeat until all the nodes have been visited
Prim-Jarnik’s Algorithm (cont.)

Minimum Spanning Trees 10


Analysis

Minimum Spanning Trees 11


Walk-Through
2
Initialize array
3
10
F C K dv pv

A 7
4
3 A F  
8
18 B F  
4
9
B D C F  
10
H 25
D F  
2
3 E F  
G 7
E F F  
G F  
H F  
2
Start with any node, say D
3
10
F C K dv pv

A 7
4
3 A
8
18 B
4
9
B D C
10
H 25
D T 0 
2
3 E
G 7
E F
G
H
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv

A 7
4
3 A
8
18 B
4
9
B D C 3 D
10
H 25
D T 0 
2
3 E 25 D
G 7
E F 18 D
G 2 D
H
2 Select node with
minimum distance
3
10
F C K dv pv

A 7
4
3 A
8
18 B
4
9
B D C 3 D
10
H 25
D T 0 
2
3 E 25 D
G 7
E F 18 D
G T 2 D
H
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv

A 7
4
3 A
8
18 B
4
9
B D C 3 D
10
H 25
D T 0 
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
2 Select node with
minimum distance
3
10
F C K dv pv

A 7
4
3 A
8
18 B
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv

A 7
4
3 A
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E 7 G
G 7
E F 3 C
G T 2 D
H 3 G
2 Select node with
minimum distance
3
10
F C K dv pv

A 7
4
3 A
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E 7 G
G 7
E F T 3 C
G T 2 D
H 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv

A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
2 Select node with
minimum distance
3
10
F C K dv pv

A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv

A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G

Table entries unchanged


2 Select node with
minimum distance
3
10
F C K dv pv

A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv

A 7
4
3 A 4 H
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2 Select node with
minimum distance
3
10
F C K dv pv

A 7
4
3 A T 4 H
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv

A 7
4
3 A T 4 H
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G

Table entries unchanged


2 Select node with
minimum distance
3
10
F C K dv pv

A 7
4
3 A T 4 H
8
18 B T 4 C
4
9
B D C T 3 D
10
H 25
D T 0 
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2 Cost of Minimum
Spanning Tree =  dv = 21
3
F C K dv pv

A 4
3 A T 4 H
B T 4 C
4
B D C T 3 D
H D T 0 
2
3 E T 2 F
G E F T 3 C
G T 2 D
H T 3 G

Done
Kruskal’s Algorithm
• Created in 1957 by Joseph Kruskal
• Finds the MST by taking the smallest weight in the graph
and connecting the two nodes and repeating until all nodes
are connected to just one tree
• This is done by creating a priority queue using the weights
as keys
• Each node starts off as it’s own tree
• While the queue is not empty, if the edge retrieved
connects two trees, connect them, if not, discard it
• Once the queue is empty, you are left with the minimum
spanning tree
Kruskal’s Algorithm
A priority queue stores Algorithm KruskalMST(G)
the edges outside the for each vertex V in G do
define a Cloud(v)  {v}
cloud let Q be a priority queue.
 Key: weight Insert all edges into Q using their
 Element: edge weights as the key
T
At the end of the while T has fewer than n-1 edges do
algorithm edge e = T.removeMin()
Let u, v be the endpoints of e
 We are left with one if Cloud(v)  Cloud(u) then
cloud that encompasses Add edge e to T
the MST Merge Cloud(v) and Cloud(u)
 A tree T which is our return T
MST

Minimum Spanning Trees 32


Data Structure for Kruskal
Algortihm
• The algorithm maintains a forest of trees
• An edge is accepted it if connects distinct trees
• We need a data structure that maintains a partition, i.e., a
collection of disjoint sets, with the operations:
-find(u): return the set storing u
-union(u,v): replace the sets storing u and v with their
union

Minimum Spanning Trees 33


Representation of a
Partition
• Each set is stored in a sequence
• Each element has a reference back to the set
– operation find(u) takes O(1) time, and returns the set of which
u is a member.
– in operation union(u,v), we move the elements of the smaller
set to the sequence of the larger set and update their references
– the time for operation union(u,v) is min(nu,nv), where nu and nv
are the sizes of the sets storing u and v
• Whenever an element is processed, it goes into a set of
size at least double, hence each element is processed at
most log n times

Minimum Spanning Trees 34


Partition-Based Implementation
• A partition-based version of Kruskal’s Algorithm performs
cloud merges as unions and tests as finds.
Algorithm Kruskal(G):
Input: A weighted graph G.
Output: An MST T for G.
Let P be a partition of the vertices of G, where each vertex forms a separate set.
Let Q be a priority queue storing the edges of G, sorted by their weights
Let T be an initially-empty tree
while Q is not empty do
(u,v)  Q.removeMinElement()
if P.find(u) != P.find(v) then Running time: O((n+m)log n)
Add (u,v) to T
P.union(u,v)
return T
Minimum Spanning Trees 35
Kruskal’s Algorithm

• Work with edges, rather than nodes


• Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not
generate a cycle
Walk-Through
Consider an undirected, weight graph
3
10
F C
A 4
4
3
8
6
5
4
B D
4
H 1
2
3
G 3
E
Sort the edges by increasing edge weight
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8 (B,F) 4
6 (D,G) 2 
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8 (B,F) 4
6 (D,G) 2 
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10

Accepting edge (E,G) would create a cycle


Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8 (B,F) 4
6 (D,G) 2 
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8 (B,F) 4
6 (D,G) 2 
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8 (B,F) 4
6 (D,G) 2 
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8 (B,F) 4
6 (D,G) 2 
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4 
8 (B,F) 4
6 (D,G) 2 
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
4
H 1
(C,D) 3  (A,H) 5 
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
F C edge dv edge dv

A 4
3 (D,E) 1  (B,E) 4 
(D,G) 2  (B,F) 4 
5
B D (E,G) 3  (B,H) 4 
H 1
(C,D) 3  (A,H) 5 
2

}
3 (G,H) 3  (D,F) 6
G E (C,F) 3  (A,B) 8
not
considered
(B,C) 4  (A,F) 10

Done
Total Cost =  dv = 21
Comparison of MST algorithms
• Each has the same worst-case running time
• The constant factors are similar for both
algorithms
• There is no clear winner
• Prim-Jarnik: A priority queue to store vertices
• Kruskal’s: A priority queue to store edges and a
collections of sets, implemented with lists

Minimum Spanning Trees 51

You might also like