12 - Chapter 21
12 - Chapter 21
Algorithms
Chapter 21
2
Spanning trees
A connected, undirected, acyclic (without cycles) graph is called a spanning tree, and a set of spann
is called a forest
A graph G is a spanning tree, if there is exactly one path between every pair of
vertices in G
SPANNING TREES
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Minimum Spanning
Trees
7
Problem: Laying Telephone
Wire
Central office
8
Wiring: Naïve Approach
Central office
Expensive!
9
Wiring: Better Approach
Central office
10
Minimum Spanning Tree (MST)
it is a tree (i.e., it is acyclic)
it covers all the vertices V
contains |V| - 1 edges
the total cost associated with tree edges
is the minimum among all possible
spanning trees
not necessarily unique
12
How Can We Generate a
MST?
9 b 9 b
a 2 6 a 2 6
d d
4 5 4 5
5 4 5 4
5 e 5 e
c c
13
MINIMUM SPANNING
TREES
Given a connected,
undirected graph
G=(V,E) with edge
T may not be unique
weights w(u,v), find
acyclic subset T ⊆ E
T will have |V|-1
that connects all edges
vertices and minimizes
total weight
Any subset of E
containing |V|-1
edges that connects
T is called a minimum all vertices is called
spanning tree a spanning tree
T is minimum
because it has
minimum weight
TWO ALGORITHMS
Kruskal’s and Prim’s
Both algorithms are greedy and have same general format
Add safe edges to A one by one until |A| = |V|-1
Given a subset A of E, where A is a subset of some minimum
spanning tree, we say (u,v) is safe for A if A∪{(u,v)} is also a
subset of a minimum spanning tree
We’ll first look at generic properties of MSTs and then discuss
Kruskal’s and Prim’s algorithms
GENERIC-MST(G,W)
1. A = Ø
2. while A does not form a spanning tree
3. find an edge (u,v) that is safe for A
4. A = A ∪ {(u,v)}
5. return A
6 3 1
3
2 f
e
1 b 3
c
f
3 8 7 4
Proof by contradiction
Assume we have a MST T such that A T and (u,v)
T
Let P be the unique simple path from u to v in T
Let (x,y) be an edge in P that crosses the cut (S,V-S)
We construct another MST T’ that contains (u,v)
1 b 3
c
(U,V) 6
u
3
2
3
v
1
T’ IS A SPANNING TREE
3 6
7
d x 3 y
3 1
6 3
2 v
u
2 THINGS TO SHOW
c b 3
f
3 8 7 4
6
7
ABOUT T’
d x 3 y
3 1
6 3
2 v
u
function w:E → ℝ.
with weight A ⊆ T. tree) of the forest
GA = (V,A).
Then if (u,v) is a light edge connecting C to some other
component of GA, then (u,v) is safe for A.
C V-C
a 5
u d f
3
b
v
c 7
e
KRUSKAL(G,W)
1. A=Ø
2. for each v in V
3. MAKE-SET(v) // uses the disjoint forest data structure
4. sort the edges of E in nondecreasing order by w
5. for each (u,v) ∈ G.E // consider in sorted order
6. if FIND-SET(u) ≠ FIND-SET(v)
7. A = A ∪ {(u,v)}
8. UNION(u,v)
9. return A
Minimum Spanning Trees
Kruskal’s Algorithm
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
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 3 (D,E) 1 (B,E) 4
8 4
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 3 (D,E) 1 (B,E) 4
8 4
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 3 (D,E) 1 (B,E) 4
8 4
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
A 4 3 (D,E) 1 (B,E) 4
8 4
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 3 (D,E) 1 (B,E) 4
8 4
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 3 (D,E) 1 (B,E) 4
8 4
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 3 (D,E) 1 (B,E) 4
8 4
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 3 (D,E) 1 (B,E) 4
8 4
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 3 (D,E) 1 (B,E) 4
8 4
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 3 (D,E) 1 (B,E) 4
8 4
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 3 (D,E) 1 (B,E) 4
8 4
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
KRUSKAL RUNTIME
1. A=Ø
2. for each v in V
3. MAKE-SET(v) // uses the disjoint forest data structure
4. sort the edges of E in nondecreasing order by w
5. for each (u,v) ∈ G.E // consider in sorted order
6. if FIND-SET(u) ≠ FIND-SET(v)
7. A = A ∪ {(u,v)}
8. UNION(u,v)
9. return A
CORRECTNESS
• Kruskal’s algorithm builds the MST based on
the corollary presented earlier.
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
Walk-Through
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
F C
A 4
3
4
B D
H
2
3
G E