슬라이드 1 - Go into The Algorithm

Download Report

Transcript 슬라이드 1 - Go into The Algorithm

23. Minimum Spanning Trees
23 Minimum Spanning Trees
Weighted undirected graph G = (V, E)

For each edge (u, v) ∈ E, we have a weight w (u, v) specifying
the cost to connect u and v.
8
b
7
c
d
4
9
2
11
a
i
14
4
6
7
10
8
h
e
1
g
2
f
23 Minimum Spanning Trees
A spanning tree for G.


Find a subset T ⊆ E that connects all of the vertices
There can be many spanning trees.
8
b
7
c
d
4
9
2
11
a
i
14
4
6
7
10
8
h
e
1
g
2
f
23 Minimum Spanning Trees
Cost of a spanning tree
w(T ) 
 w(u, v)
( u ,v )T
minimum-spanning-tree problem


A spanning tree whose cost is the smallest.
T is acyclic and connects all of the vertices → a tree
23.1 Growing a minimum spanning tree
GENERIC-MST



grows the minimum spanning tree one edge at a time.
A is a subset of some minimum spanning tree.
determine an edge (u, v) that can be added to A without
violating this invariant, in the sense that A∪ {(u, v)} is also a
subset of a minimum spanning tree.
 Call such an edge a safe edge for A,
23.1 Growing a minimum spanning tree
GENERIC-MST(G, w)
1 A←Ø
2 while A does not form a spanning tree
3
do find an edge (u, v) that is safe for A
4
A ← A {(u, v)}
5 return A
23.1 Growing a minimum spanning tree


A cut (S, V - S) of an undirected graph G = (V, E)
 A partition of V
an edge (u, v) ∈ E crosses the cut (S, V - S)
 if one of edge (u, v) ∈ E endpoints is in S and the other is
in V - S.
8
b
7
c
d
4
9
2
S a
V-S
11
i
6
7
10
8
h
Figure 23.2
1
e S
V-S
14
4
g
(a)
2
f
23.1 Growing a minimum spanning tree
a cut respects a set A of edges

if no edge in A crosses the cut.
a light edge

if its weight is the minimum of any edge crossing the cut.
23.1 Growing a minimum spanning tree
8
h
a
x
7
4
11
i
b
1
6
2
p
u
y
g
8
c
7
c
9
14
4
d
10
S V-S
Figure 23.2
2
v
f
(b)
Figure 23.3
23.1 Growing a minimum spanning tree

Theorem 23.1

Let G = (V, E) be a connected, undirected graph with a real-valued
weight function w defined on E.
Let A be a subset of E that is included in some minimum spanning
tree for G,
let (S, V -S) be any cut of G that respects A,
let (u, v) be a light edge crossing (S, V - S).

Then, edge (u, v) is safe for A.



23.1 Growing a minimum spanning tree

Let T be a minimum spanning tree that includes A,
 Assume that T does not contain the light edge (u, v)

construct another minimum spanning tree T′
that includes A {(u, v)} by using a cut-and-paste technique
23.1 Growing a minimum spanning tree


The edge (u, v) forms a
cycle with the edges on the
path p from u to v in T
u and v are on opposite
sides of the cut (S, V - S),
 there is at least one edge
in T on the path p
that also crosses the cut.
x
p
u
y
v
Figure 23.3
23.1 Growing a minimum spanning tree


The edge (u, v) forms a cycle
with the edges on the path p
from u to v in T
u and v are on opposite sides
of the cut (S, V - S),
 there is at least one edge in
T on the path p
that also crosses the cut.
 Let (x, y) be any such edge.
x
p
u
y
v
Figure 23.3
23.1 Growing a minimum spanning tree

The edge (x, y) is not in A

because the cut respects A.
x

Removing (x, y) breaks T
into two components.

p
u
y
Because (x, y) is on the
unique path from u to v in T
v
Figure 23.3
23.1 Growing a minimum spanning tree

Adding (u, v) reconnects
them to form a new
spanning

x
tree T′ = T - {(x, y)}∪{(u, v)}.
p
u
y
v
Figure 23.3
23.1 Growing a minimum spanning tree

We next show that T′ is a minimum spanning tree.
 Since (u, v) is a light edge crossing (S, V -S) and (x, y)
also crosses this cut, w(u, v) ≤ w(x, y).
w(T ' )  w(T )  w( x, y )  w(u, v)
 w(T)

But T is a minimum spanning tree, so that w(T) ≤ w(T′);
thus, T′ must be a minimum spanning tree also.
23.1 Growing a minimum spanning tree
show that (u, v) is actually a safe edge for A.


A ∉ T and (x, y) ∉ A ⇒ A ⊆T′
 Thus A ∪{(u, v)}⊆T′.
T′ is a minimum spanning tree,
 (u, v) is safe for A.
23.1 Growing a minimum spanning tree

Corollary 23.2
 Let G = (V, E) be a connected, undirected graph with a
real-valued weight function w defined on E.
 Let A be a subset of E that is included in some minimum
spanning tree for G
 Let C = (VC, EC) be a connected component (tree) in the
forest 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.
23.1 Growing a minimum spanning tree
Proof


The cut (VC, V - VC) respects A, and (u, v) is a light edge
for this cut.
Therefore, (u, v) is safe for A.
Kruskal's algorithm



Finds a safe edge to add to the growing forest by finding,
of all the edges that connect any two trees in the forest,
an edge (u, v) of least weight.
Let C1 and C2 denote the two trees that are connected by (u, v).
Since (u, v) must be a light edge connecting C1 to some other
tree, Corollary 23.2 implies that (u, v) is a safe edge for C1.
Kruskal's algorithm
8
7
b
c
d
4
9
2
(a)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(b)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(c)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(d)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(e)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(f)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(g)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(h)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(i)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(j)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(k)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(l)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(m)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
8
7
b
c
d
4
9
2
(n)
11
a
i
e
14
4
6
7
10
8
h
g
1
Kruskal’s algorithm
f
2
Kruskal's algorithm
MST-KRUSKAL(G, w)
1 A←Ø
2 for each vertex v V[G]
3
do MAKE-SET(v)
4 sort the edges of E into nondecreasing order by weight w
5 for each edge (u, v) ∈ E, taken in nondecreasing order by weight
6
do if FIND-SET(u) ≠ FIND-SET(v)
7
then A ← A ∪ {(u, v)}
8
UNION(u, v)
9 return A
Prim’s algorithm




the tree starts from an arbitrary root vertex r and grows until
the tree spans all the vertices in V
At each step, a light edge is added to the tree A that connects A
to an isolated vertex of GA = (V, A)
By Corollary 23.2, this rule adds only edges that are safe for A
therefore, when the algorithm terminates, the edges in A form
a minimum spanning tree
Prim’s algorithm
8
7
b
c
d
4
9
2
(a)
11
a
i
e
14
4
6
7
10
8
h
g
1
Prim’s algorithm
f
2
Prim’s algorithm
8
7
b
c
d
4
9
2
(b)
11
a
i
e
14
4
6
7
10
8
h
g
1
Prim’s algorithm
f
2
Prim’s algorithm
8
7
b
c
d
4
9
2
(c)
11
a
i
e
14
4
6
7
10
8
h
g
1
Prim’s algorithm
f
2
Prim’s algorithm
8
7
b
c
d
4
9
2
(d)
11
a
i
e
14
4
6
7
10
8
h
g
1
Prim’s algorithm
f
2
Prim’s algorithm
8
7
b
c
d
4
9
2
(e)
11
a
i
e
14
4
6
7
10
8
h
g
1
Prim’s algorithm
f
2
Prim’s algorithm
8
7
b
c
d
4
9
2
(f)
11
a
i
e
14
4
6
7
10
8
h
g
1
Prim’s algorithm
f
2
Prim’s algorithm
8
7
b
c
d
4
9
2
(g)
11
a
i
e
14
4
6
7
10
8
h
g
1
Prim’s algorithm
f
2
Prim’s algorithm
8
7
b
c
d
4
9
2
(h)
11
a
i
e
14
4
6
7
10
8
h
g
1
Prim’s algorithm
f
2
Prim’s algorithm
8
7
b
c
d
4
9
2
(i)
11
a
i
e
14
4
6
7
10
8
h
g
1
Prim’s algorithm
f
2
Prim’s algorithm
MST-PRIM(G, w, r)
1
for each u V [G]
2
do key[u] ← ∞
3
π[u] ← NIL
4
key[r] ← 0
5
Q ← V [G]
6
while Q ≠ Ø
7
do u ← EXTRACT-MIN(Q)
8
for each v∈Adj[u]
9
do if v ∈ Q and w(u, v) < key[v]
10
then π[v] ← u
11
key[v] ← w(u, v)