Vous êtes sur la page 1sur 26

Minimum Spanning Tree,

Disjoint-set Union,
Amortized analysis
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.

p2.
Minimum Spanning Tree

We then wish to find an acyclic subset T⊆


⊆ E that connects
all of the vertices and whose total weight
w ( 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.

p3.
Minimum Spanning Tree

 G=(V,E): connected and undirected


w: ER, weight function

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

6
8 7 10

h g f
1 2

p4.
Minimum Spanning Tree

 The algorithm manages a set A be 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,
A in the sense that
A U {u, v} is also a subset of a MST. We call
such an edge a safe for A, since it can be
safely added to A without destroying the
invariant.

p5.
Minimum Spanning Tree

Generic-MST(G , w)
{
A← φ
while A does not form a spanning tree
do find an edge (u , v ) that is safe
for A;
A ← A U {(u , v )};

return A
}

p6.
Minimum Spanning Tree
8 7
b c d
4 9
2
a 11 14 e
i 4
S 6
8 7 10 a cut
h g f
V-S 1 2
 A cut (S, V-S) of an undirected graph G=(V, E) is a
partition of V
 An edge (u,v) ∈ E crosses the cut (S, V-S) if one of its
endpoints 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 ?

p7.
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:
S : {O}

x V-S: {O}
A: { - }
u
y
T’ = T – {(x,y)} U {(u,v)}

v original MST
p8.
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) ∉ A
A U {(u,v)} ⊆ T'
⇒ T' is a MST and (u, v) is safe for A

p9.
Minimum Spanning Tree

 Cor2:
G=(V,E): connected, undirected
w = real-valued weight function
A ⊆ E 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 light


edge for this cut

v
u
V-C
C
p10.
 Disjoint sets

S = {S1 , S2 , S3 ,....., S n }, Si I S j = φ , if i ≠ j

Operations:
Make-Set(x ) S ← S U {{x}}
Union(Si , S j ) S ← S − {Si , S j } U {Si U S j }
Find-Set(x ) return Si ∈ S s.t. x ∈ Si

p11.
 Eg. Minimum spanning tree
G=(V,E): connected, undirected, edge-weighted graph
w:ER
Kruskals' algorithm:

T=φ
for each v ∈ V
do Make-Set(v )

sort E by increasing edge weight w

for each (u , v ) ∈ E (in sorted order)


do if Find-Set(u ) ≠ Find-Set(v )
then T ← T U {(u, v )}
Union(Find-Set(u ), Find-Set(v ))

p12.
 Kruskal’s algorithm
(a) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(b) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2
p13.
(c) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(d) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2
p14.
(e) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(f) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

p15.
(g) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(h) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

p16.
(i) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(j) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2
p17.
(k) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(l) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

p18.
(m) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(n) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

p19.
 Prim’s algorithm:
MTS-Prim(G, w, r)
{
Q ← V[G] /* Q: priority queue */
for each u ∈ Q
O(V) do key[u] ← ∞
π [u] ← NIL
key[r] ← 0

while Q ≠ φ lg V
do u ← Extract-Min(Q)
O(V lg V) for each v ∈ adj[u]
do if v ∈ Q and w(u,v) < key[v]
O(E) then π [v] ← u
key[v] ← w(u,v)
}
O(lg V), Decrease-key involves

p20.
 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)

p21.
 Prim’s algorithm
(a) 8 7
b c d
4 9
r
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(b) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2
p22.
(c) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(d) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2
p23.
(e) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(f) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2
p24.
(g) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

(h) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2
p25.
(i) 8 7
b c d
4 9
2
a 11 14 e
i 4
6
8 7 10
h g f
1 2

p26.

Vous aimerez peut-être aussi