23 - Minimum Spanning Trees
23 - Minimum Spanning Trees
Sun-Yuan Hsieh
謝孫源 教授
成功大學資訊工程學系
Overview
Problem
A town has a set of houses and a set of roads.
A road connects 2 and only 2 houses.
A road connecting houses u and v has a repair cost w(u, v).
Goal: Repair enough (and no more) roads such that
1. everyone stays connected: can reach every house from all
other houses, and
2. total repair cost is minimum.
2
Overview
Model as a graph:
Undirected graph G = (V, E)
Weight w(u, v) on each edge (u, v) E
Find T E such that
1. T connects all vertices (T is a spanning tree), and
2. w(T ) w(u , v) is minimized.
( u ,v )T
3
Overview
In this example, there is more than one MST. Replace edge (e, f)
by (c, e).
4
Growing a minimum spanning tree
5
Growing a minimum spanning tree
7
Finding a safe edge
How do we find safe edges?
Let’s look at the example. Edge (c, f ) has the lowest weight of
any edge in the Graph. Is it safe for A = Ø?
8
Some definitions: Let S V and A E.
A cut (S, VS) is a partition of vertices into disjoint sets S and
VS.
Edge (u, v) E crosses cut (S, VS) if one endpoint is in S and
the other is in VS.
A cut respects A if and only if no edge in A crosses the cut.
An edge is a light edge crossing a cut if and only if its weight
is minimum over all edges crossing the cut. For a given cut,
there can be > 1 light edge crossing it.
9
Theorem
Let A be a subset of some MST, (S, VS) be a cut that respects A,
and (u, v) be a light edge crossing (S, VS). Then (u, v) is safe for A.
Recall: a tree has unique path between each pair of vertices. Since T is an
MST, it contains a unique path p between u and v. Path p must cross the
cut (S, VS) at least once. Let (x, y) be an edge of p that crosses the cut.
From how We chose (u, v) , must have w(u, v) w(x, y)
10
[Except for the dashed edge (u, v), all edges shown are in T. A is some
subset of the edges of T, but A cannot contain any edges that cross the cut
(S,
VS), since this cut respects A. Shaded edges are the path p.]
S
u
x
v y
VS
11
Since the cut respects A, edge (x, y) is not in A.
To from T from T:
Remove (x, y) . Breaks T into two components.
Add (u, v) . Reconnects.
So T = T {(x, y)} {(u, v)}.
T is a spanning tree.
w(T) = w(T) w(x, y) + w(u, v)
w(T)
since w(u, v) w(x, y) .
Since T is a spanning tree, w(T) w(T), and T is an MST, then
T must be an MST.
12
Need to show that (u, v) is safe for A:
A T and (x, y) A A T
A {(u, v)} T
Since T is an MST, (u, v) is safe for A.
13
GENERIC-MST:
14
Corollary
If C = (VC, EC) is a connected component in the forest GA = (V, A)
and (u, v) is a light edge connecting C to some other component in
GA (i.e., (u, v) is a Light edge crossing the cut (VC, VVC)), then (u,
v) is safe for A.
15
Kruskal’s algorithm
16
KRUSKAL(V, E, w)
1. AØ
2. for each vertex v V[G]
3. do MAKE-SET(v)
4. sort E into nondecreasing order by weight w
5. for each (u, v) taken from the sorted list
6. do if FIND-SET(u) FIND-SET(v)
7. then A A {(u, v)}
8. UNION(u,v)
9. return A
17
Run through the above example to see how Kruskal’s algorithm
works on it:
(c, f) : chosen
(g, i) : chosen
(e, f) : chosen 8 8
b d g
(c, e) : reject 10 2
(d, h) : chosen 7
5
(f, h) : chosen a 9 e 9 i
3 3
(e, d) : reject
(b, d) : chosen 12 11
c f h
(d, g) : chosen 1 6
(b, c) : reject
(g, h) : reject
(a, b) : chosen
18
At this point, we have only one component, so all other
edges will be rejected.
[We could add a test to the main loop of KRUSKAL to stop
once |V|1 edges have been added to A.]
19
Analysis
Initialize A: O(1)
First for loop: |V|MAKE-SETS
Sort E: O(E lgE)
Second for loop: O(E) FIND-SETS and UNIONS
20
Assuming the implementation of disjoint-set data structure,
already seen in chapter21, that uses union by rank and path
compression: O((V + E)(V)) + O(E lg E)
Since G is connected, |E| |V| 1 O(E (V)) + O(E lg E).
(|V|) = O(lg V) = O(lg E)
Therefore, total time is O(E lg E).
|E| |V|2 lg |E| = O(2 lg V) = O(lg V)
Therefore, O(E lg V) time. (If edges are already sorted,
O(E(V)), which is almost linear.)
21
Prim’s algorithm
V VA
Light edge
[Edges of A are shaded.]
22
How to find the light edge quickly?
Use a priority queue Q:
Each object is a vertex in VVA.
Key of v is minimum weight of any edge (u, v) , where u VA.
Then the vertex returned by EXTRACT-MIN is v such that
there exists u VA and (u, v) is light edge crossing (VA, VVA).
Key of v is if v is not adjacent to any vertices in VA.
23
The edges of A will form a rooted tree with root r:
r is given as an input to the algorithm, but it can be any vertex.
Each vertex knows its parent in the tree by the attribute [v]=
parent of v. [v] = NIL if v = r or v has no parent.
As algorithm progresses, A = {(v, [v]) : v V{r}Q}.
At termination,
V A V Q , so MST is A {(v, [v]) : v V {r}}.
24
PRIM(V, E, w, r)
1. QØ
2. for each u V[G]
3. do key[u]
4. [u] NIL
5. INSERT(Q, u)
6. DECREASE-KEY(Q, r, 0)
7. while Q Ø
8. do u EXTRACT-MIN(Q)
9. for each v Adj[u]
10. do if v Q and w(u, v) < key[v]
11. then [v] u
12. DECREASE-KEY(Q, v, w(u, v) )
25
Run through the above example to see how Prim’s algorithm works on it:
Vertex c has been chosen as a starting point.
8 8
b d g
10 2
7
5 aifceghdb
a 9 e 9 i
3 3 11
12
10
82∞016358
12 11
bgehda 12
8∞
58
7
6
3
9 12
∞
0 ca
c f h
root 1 6
idg 11
∞
9
7 ∞
9
3 ∞
1 ∞ g
(c, f) : chosen (d, g) : chosen
eb fi
(e, f) : chosen (g, i) : chosen ∞ ∞
(f, h) : chosen (b, d) : chosen
h i
(d, h) : chosen (a, b) : chosen
26
Analysis
Depends on how the priority queue is implemented:
Suppose Q is a binary heap.
Initialize Q and first for loop: O(V lg V)
Decrease key of r: O(lg V)
while loop: |V| EXTRACT-MIN calls
O(V lg V)
|E| DECREASE-KEY calls
O(E lg E)
Total: O(E lgV)
27
Suppose we could do DECREASE-KEY in O(1) amortized time.
Then |E| DECREASE-KEY calls take O(E) time altogether
total time becomes O(V lg V + E)
In fact, there is a way to do DECREASE-KEY in O(1)
amortized time: Fibonacci heaps, in chapter20.
28