0% found this document useful (0 votes)
26 views20 pages

Minimum Spanning Trees

Uploaded by

k224230
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)
26 views20 pages

Minimum Spanning Trees

Uploaded by

k224230
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/ 20

Design and Analysis of Algorithms

Minimum Spanning trees

Presented By
Dr Muhammad Atif Tahir
From
Haidong Xue
What are trees in undirected graphs?
• We are familiar with a rooted directed tree,
such as:
3
– Binary search tree
5
– Heap 6
4
• They look like: 2
– You know which one the root is
– Who the parent is
– Who the children are
What are trees in undirected graphs?
• However, in undirected graphs, there is
another definition of trees
• Tree
– A undirected graph (V, E), where E is the set of
undirected edges
– All vertices are connected 3

– |E|=|V|-1
5
6
4

2
What is a spanning tree?

b c d

a i e

h g f
What is a spanning tree?
• Given the following graph, are they spanning
trees?
b c d

a i e

h g f

Is the red sub graph a tree? Yes

Is the red sub graph a spanning


tree?
What is a spanning tree?
• Given the following graph, are they spanning
trees?
b c d

a i e

h g f

Is the red sub graph a tree?

Is the red sub graph a spanning


tree?
What is a spanning tree?
• Given the following graph, are they spanning
trees?
b c d

a i e

h g f

Is the red sub graph a tree? No. Vertices are not all connected

Is the red sub graph a spanning


tree?
What is a spanning tree?
• Given the following graph, are they spanning
trees?
b c d

a i e

h g f

Is the red sub graph a tree? Yes

Is the red sub graph a spanning Yes


tree?
What is a spanning tree?
• Given the following graph, are they spanning
trees?
b c d

a i e

h g f

Is the red sub graph a tree? Yes

Is the red sub graph a spanning Yes


tree?

There could be more than one spanning trees


What is a minimum spanning tree?
• When edges are weighted
• A minimum spanning tree of G is:
– A spanning tree of G
– With a minimum sum of a edge weights sum among all
spanning trees

8 7
b c d
4 9
2
11 14
a i 4 e
7 6
8 10
h 1
g 2
f
What is a minimum spanning tree?
• Spanning trees:
8 7
b c d
4 9 The weight sum of this
2
11 14
spanning tree?
a i 4 e 48
7 6
8 10
h 1
g 2
f

b c d
The weight sum of this
4 9
2 spanning tree?
11 14
a i 4 e 47
7 6
8 10
h 1
g 2
f
What is a minimum spanning tree?
• Spanning trees:
8 7
b c d
4 9 The weight sum of this
2
11 14
spanning tree?
a i 4 e 37
7 6
8 10
h 1
g 2
f

8 7
b c d
The weight sum of this
4 9
2 spanning tree?
11 14
a i 4 e 37
7 6
8 10
These 2 are minimum spanning trees
h 1
g 2
f since there is no spanning tree with a
smaller total weight
Minimum spanning tree problem
• Minimum spanning tree (MST) problem:
given a undirected graph G, find a spanning
tree with the minimum total edge weights.
• There are 2 classic greedy algorithms to solve
this problem
– Kruskal’s algorithm
– Prim’s algorithm
Minimum spanning tree problem
• A application example of MST in computer
network
– What if PC3 wants to communicate with PC7?
– By a routing protocol:
• PC3 broadcasts this requirement to PC2, PC4
• PC2 broadcasts it to PC1, PC5 PC1 PC2 PC3
• PC1 broadcasts it to PC5, PC6

• PC5 broadcasts it to PC2 PC6 PC5 PC4

– At least, a package is in a deadlock:


PC2->PC1->PC5-PC2->PC1-> …………….
Find and use a MST can solve this problem, since there is no cycle
Minimum spanning tree problem
• The MST algorithm can be considered as
choose |V|-1 edges from E to form a
minimum spanning tree
• Both Kruskal’s algorithm and Prim’s algorithm
proposed a greedy choice about how to
choose a edge
• They have greedy choice property because of
textbook Corollary 23.2
Kruskal’s algorithm
• Input: G=(V, E)
• Output: a minimum spanning tree (V, A)
• Kruskal (G=(V, E))
Set each vertices in V as a vertex set;
Set A as an empty set;
while (there are more than 1 vertex set){
Add a smallest edge (u, v) to A where u, v are not in the same set (i.e.
avoid cycle);
Merge the set contains u with the set contains v;
}
• In most of the implementations, edges are sorted on
weights at the beginning, and then scan them once
• The running time = O(|E|lg|V|)
Kruskal’s algorithm
Kruskal (G=(V, E)) Edge Weight
Set each vertices in V as a vertex set;
Set A as an empty set; (h, g) 1
while (there are more than 1 vertex set){ (i, c) 2
Add a smallest edge (u, v) to A where u, (g, f) 2
v are not in the same set;
Merge the set contains u with the set (c, f) 4
contains v; (a, b) 4
} (i, g) 6
8 7
b c d (h, i) 7

4 9
(c, d) 7
2
(a, h) 8
11 14
4
a i e (b, c) 8
7 6
8
(d, e) 9
10
(e, f) 10
h 1
g 2
f
(b, h) 11
Total weight: 37 (d, f) 14
Prim’s algorithm
• Input: G=(V, E)
• Output: a minimum spanning tree (V, A)
• Prim(G=(V, E))
Set A as an empty set;
Choose a vertex as the current MST;
while (not all the vertices are added into the MST){
Choose the smallest edge crossing current MST vertices and
other vertices, and put it to MST ;
}
• The running time = O(|E|lg|V|)
Prim’s algorithm
Prim(G=(V, E))
Set A as an empty set;
Choose a vertex as the current MST;
while (not all the vertices are added into the MST){
Choose the smallest safe edge, and put it to MST;
}

8 7
b c d
4 9
2
11 14
4
a i e
7 6
8 10

h 1
g 2
f

Total weight: 37
Summary
• Prim’s algorithm and Kruskal’s algorithm have
the same asymptotically running time

You might also like