min spanning tree
TRANSCRIPT
-
8/9/2019 Min Spanning Tree
1/26
Minimum Spanning Tree,
Disjoint-set Union,
Amortized analysis
-
8/9/2019 Min Spanning Tree
2/26
p2.
Minimum Spanning Tree
We model the wiring problem with a connected,
undirected graphs G = ( V, E) where V is the set
of pins, E is the set possible interconnections
between pairs of pins, and for each edge (u, v )
R, we have a weight w ( u, v ) specifying the cost
( amount of wire needed) to connect u and v.
-
8/9/2019 Min Spanning Tree
3/26
p3.
Minimum Spanning Tree
We then wish to find an acyclic subset T E that connects
all of the vertices and whose total weightw ( T ) = w ( u, v )
( u, v ) T
is minimized.
Since T is acyclic and connects all of the vertices, it must
form a tree, which we a spanning tree since it spans the
graph.
-
8/9/2019 Min Spanning Tree
4/26
p4.
Minimum Spanning Tree
G=(V,E): connected and undirected
w: ER, weight function
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
-
8/9/2019 Min Spanning Tree
5/26
p5.
Minimum Spanning Tree
The algorithm manages a set Abe that is
always a subset of some minimum spanning
tree. At each step, an edge (u,v) is
determined that can be added to A without
violating this invariant, in the sense that
A U {u, v}isalso a subset of a MST. We call
such an edge a safe for A, since it can be
safely added to Awithout destroying the
invariant.
A
-
8/9/2019 Min Spanning Tree
6/26
p6.
Minimum Spanning Tree
,
,
,
Generic-MST( ){
while A does not form a spanning tree
do find an edge ( ) that is safefor ;{( )};
G w
A
u vA A A u v
U
return}
A
-
8/9/2019 Min Spanning Tree
7/26
p7.
Minimum Spanning Tree
A cut (S, V-S) of an undirected graph G=(V, E) is apartition of V
An edge crosses the cut (S, V-S) if one of itsendpoints is in S and the other is in V-S
A cut respects the set A of edges if no edge in A crosses the
cut An edge is a light edge crossing a cut if its weight is the
minimum of any edge crossing the cut
What is the light edge in the above graph ?
1 2
a
b
gh
i
c
f
d
e
4
8
11
8
2
76
4
7
14
9
10 a cutS
V-S
(u,v) E
-
8/9/2019 Min Spanning Tree
8/26
p8.
Minimum Spanning Tree
Thm1:
G=(V,E): connected, undirected
w: real-valued weight function on E
A: a subset of E and is included in some MST
(S, V-S): any cut of G and respects A
(u,v): a light edge crossing (S, V-S)
Then (u,v) is safe for A.
pf:
yu
x
v
S : {O}
V-S: {O}
A: { - }
T = T {(x,y)} U {(u,v)}
original MST
-
8/9/2019 Min Spanning Tree
9/26
p9.
Minimum Spanning Tree
w(T) = w(T) w(x,y) + w(u,v)
= w(T)
Thus, T is a MST
A T, and (x,y) AA {(u,v)} T'T' is a MST and (u, v) is safe for A
U
-
8/9/2019 Min Spanning Tree
10/26
p10.
Minimum Spanning Tree
Cor2:
G=(V,E): connected, undirected
w = real-valued weight function
and A is in some MST
C: a connected component in GA=(V,A)
if (u,v) is a light edge connecting C to some other
component in GA, then (u,v) is safe for A
Pf: The cut(C, V-C) respects A, and (u,v) is therefore a lightedge for this cut
A E
v
u
V-C
C
-
8/9/2019 Min Spanning Tree
11/26
p11.
Disjoint sets
{ , , ,....., }, ,
{{ }}, { , } { }
n i j
i j i j i j
i i
S S S S S S S i j
x S S xS S S S S S S S
x S S x S
= =
if
Operations:
Make-Set( )Union( )
Find-Set( ) return s.t.
I
U
U U
1 2 3
-
8/9/2019 Min Spanning Tree
12/26
p12.
Eg. Minimum spanning tree
G=(V,E): connected, undirected, edge-weighted graph
w: E R
,
Kruskals' algorithm:
T=for each
do Make-Set( )
sort E by increasing edge weight
for each ( ) (in sorted order)
do if Find-Set( )
v V
v
w
u v E
u
{ , }Find-Set( )
then T T ( )Union(Find-Set( ), Find-Set( ))
vu v
u v
U
-
8/9/2019 Min Spanning Tree
13/26
p13.
Kruskals algorithm8 7(a)
a
b
gh
i
c
f
d
e
4
8
11
2
7
1 2
6
414
9
10
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(b)
-
8/9/2019 Min Spanning Tree
14/26
p14.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(c)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(d)
-
8/9/2019 Min Spanning Tree
15/26
p15.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(e)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(f)
-
8/9/2019 Min Spanning Tree
16/26
p16.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(g)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(h)
-
8/9/2019 Min Spanning Tree
17/26
p17.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(i)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(j)
-
8/9/2019 Min Spanning Tree
18/26
p18.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(k)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(l)
-
8/9/2019 Min Spanning Tree
19/26
p19.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(m)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(n)
-
8/9/2019 Min Spanning Tree
20/26
p20.
Prims algorithm:
MTS-Prim(G, w, r)
{ Q V[G] /* Q: priority queue */for each u Q
do key[u][u] NIL
key[r] 0
while Qdo u Extrac
t-Min(Q)for each v adj[u]
do if v Q and w(u,v) < key[v]then [v] u
key[v] w(u,v)}
O(V)
O(V lg V)
O(E)
lg V
O(lg V), Decrease-key involves
-
8/9/2019 Min Spanning Tree
21/26
p21.
Analysis Binary heap: O(V lg V + E lg V)
= O(E lg V)
Fibonacci heap:
Decrease-key: O(1) amortized time
O(V lg V + E)
-
8/9/2019 Min Spanning Tree
22/26
p22.
Prims algorithm
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(a)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(b)
r
-
8/9/2019 Min Spanning Tree
23/26
p23.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(c)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(d)
-
8/9/2019 Min Spanning Tree
24/26
p24.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(e)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(f)
-
8/9/2019 Min Spanning Tree
25/26
p25.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(g)
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(h)
-
8/9/2019 Min Spanning Tree
26/26
p26.
a
b
gh
i
c
f
d
e
4
8
11
8
2
7
1 2
6
4
7
14
9
10
(i)