Professional Documents
Culture Documents
(MST)
3
Minimum Spanning Trees
• A connected, undirected graph: G = (V,E)
4
Properties of Minimum Spanning Trees
5
Growing a MST – Generic Approach
6
Generic MST algorithm
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)} b
8
c
7
d
4 9
2
5. return A 11 14
a i 4 e
7 6
8 10
h g 2
f
1
• How do we find safe edges?
7
Finding Safe Edges
8 7 V-S
Let’s look at edge (h, g)
b c d
• 4 9
2
11
– Is it safe for A initially? a i 4 14 e
7 6
8 10
• Later on: S h
1
g 2
f
8
Definitions
8 7
b c d
4 9
2
• A cut (S, V - S) S a 11 i 4 14 e S
7 6
is a partition of vertices V- S 8 10
V- S
h g 2
f
1
into disjoint sets S and V - S
• An edge crosses the cut
(S, V - S) if one endpoint is in S
and the other in V – S
9
Definitions (cont’d)
• A cut respects a set A
8 7
b c d
4 9
2
of edges no edge S a 11 i 4 14 e S
in A crosses the cut
7 6
V- S 8 10
V- S
h g 2
f
1
• An edge is a light edge
crossing a cut its weight is minimum over all
edges crossing the cut
– Note that for a given cut, there can be > 1 light
edges crossing it
10
Theorem
• Let A be a subset of some MST (i.e., T), (S, V - S) be a
cut that respects A, and (u, v) be a light edge crossing
(S, V-S). Then (u, v) is safe for A .
v
V-S
11
MST
Two greedy algorithms that implement generic
approach:
– Kruskal’s algorithm
• Running time = O(E lg V)
– Prim’s algorithm
• Running time = O(E lg V)
12
Prim’s Algorithm
• The edges in set A always form a single tree
• Starts from an arbitrary “root”: VA = {a}
• At each step:
8 7
b c d
– Find a light edge crossing (VA, V - VA) 4 9
2
– Add this edge to A a 11 i 4 14 e
7 6
– Repeat until the tree spans all vertices 8 10
h g 2
f
1
13
How to Find Light Edges Quickly?
Use a priority queue Q: 8 7
b c d
• Contains vertices not yet 4 9
2
included in the tree, i.e., (V – VA) a 11 i 4 14 e
7 6
– VA = {a}, Q = {b, c, d, e, f, g, h, i} 8 10
h g f
• We associate a key with each vertex v: 1 2
Key[a]=min(w 1,w2) w1
w2
a
14
How to Find Light Edges Quickly?
(cont.)
• After adding a new node to VA we update the weights of all
the nodes adjacent to it
e.g., after adding a to the tree, key[b]=4 and key[h]=8
• Key of v is if v is not adjacent to any vertices in VA
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g 2
f
1
15
How to Find Light Edges Quickly?
(cont.)
16
Example
b
8
c
7
d 0
4 9
2 Q = {a, b, c, d, e, f, g, h, i}
a 11 i 4 14 e VA =
7 6
8 10 Extract-MIN(Q) a
h g 2
f
1
4
8 7 key [b] = 4 [b] = a
b c d
4 9 key [h] = 8 [h] = a
2
a 11 i 4 14 e
8
7 6
10 4 8
h g 2
f Q = {b, c, d, e, f, g, h, i} VA = {a}
1
8
Extract-MIN(Q) b
17
Example
4
8 key [c] = 8 [c] = b
8 7
b c d
9
key [h] = 8 [h] = a - unchanged
4
2
8 8
a 11 i 4 14 e
7 6 Q = {c, d, e, f, g, h, i} VA = {a, b}
8 10
h g f Extract-MIN(Q) c
1 2
8
key [d] = 7 [d] = c
4 8
7
8 7 key [f] = 4 [f] = c
b c d
4 2 2
9
key [i] = 2 [i] = c
a 11 i 4 14 e
8
7 6
10 7 4 8 2
h g 2
f Q = {d, e, f, g, h, i} VA = {a, b, c}
1
8
4
Extract-MIN(Q) i 18
Example
4 8 7 key [h] = 7 [h] = i
8
b c
7
d key [g] = 6 [g] = i
4 9
2 2 7 46 8
a 11 i 4 14 e
Q = {d, e, f, g, h} VA = {a, b, c, i}
7 6
Extract-MIN(Q) f
8 10
h g 2
f
1
87
6 4
4 8 7 key [g] = 2 [g] = f
b
8
c
7
d key [d] = 7 [d] = c unchanged
9
4
2 2 10 key [e] = 10 [e] = f
a 11 i 14 e
4 7 10 2 8
7 6
8 10 Q = {d, e, g, h} VA = {a, b, c, i, f}
h g f
1
6
2
4
Extract-MIN(Q) g
7 2 19
Example
4 8 7 key [h] = 1 [h] = g
8 7
b c d 7 10 1
4 9
2 2 10 Q = {d, e, h} VA = {a, b, c, i, f, g}
a 11 i 14 e
7 6
4
Extract-MIN(Q) h
8 10
h g 2
f
1
7
1 2 4 7 10
4 8 7
8 7
Q = {d, e} VA = {a, b, c, i, f, g, h}
b c d
4 9 Extract-MIN(Q) d
2 2 10
a 11 i 4 14 e
7 6
8 10
h g 2
f
1
1 2 4
20
Example
4 8 7
8 7
b c d
9 9
key [e] = 9 [e] = f
4
2 2 10
9
a 11 i 4 14 e
7 6 Q = {e} VA = {a, b, c, i, f, g, h, d}
8 10
h g f Extract-MIN(Q) e
1 2
1 2 4 Q = VA = {a, b, c, i, f, g, h, d, e}
21
PRIM(V, E, w, r)
1. Q←
Total time: O(VlgV + ElgV) = O(ElgV)
2. for each u V
3. do key[u] ← ∞ O(V) if Q is implemented
as a min-heap
4. π[u] ← NIL
5. INSERT(Q, u)
6. DECREASE-KEY(Q, r, 0) ► key[r] ← 0 O(lgV)
22
Prim’s Algorithm
• Prim’s algorithm is a “greedy” algorithm
– Greedy algorithms find solutions based on a sequence
of choices which are “locally” optimal at each step.
23
A different instance of the
generic approach
S
(instance 1)
(instance 2)
u
tree1
v
V-S
26
Example
1. Add (h, g) {g, h}, {a}, {b}, {c}, {d}, {e}, {f}, {i}
8 7
b c d 2. Add (c, i) {g, h}, {c, i}, {a}, {b}, {d}, {e}, {f}
4 9
2 3. Add (g, f) {g, h, f}, {c, i}, {a}, {b}, {d}, {e}
a 11 i 4 14 e 4. Add (a, b) {g, h, f}, {c, i}, {a, b}, {d}, {e}
7 6 5. Add (c, f) {g, h, f, c, i}, {a, b}, {d}, {e}
8 10
h
1
g 2
f 6. Ignore (i, g) {g, h, f, c, i}, {a, b}, {d}, {e}
7. Add (c, d) {g, h, f, c, i, d}, {a, b}, {e}
1: (h, g) 8: (a, h), (b, c) 8. Ignore (i, h) {g, h, f, c, i, d}, {a, b}, {e}
2: (c, i), (g, f) 9: (d, e) 9. Add (a, h) {g, h, f, c, i, d, a, b}, {e}
4: (a, b), (c, f) 10: (e, f) 10. Ignore (b, c) {g, h, f, c, i, d, a, b}, {e}
6: (i, g) 11: (b, h) 11. Add (d, e) {g, h, f, c, i, d, a, b, e}
7: (c, d), (i, h) 14: (d, f) 12. Ignore (e, f) {g, h, f, c, i, d, a, b, e}
{a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i} 13. Ignore (b, h) {g, h, f, c, i, d, a, b, e}
14. Ignore (d, f) {g, h, f, c, i, d, a, b, e}
27
Implementation of Kruskal’s Algorithm
8
• Uses a disjoint-set data
7
b c d
4 9
2
structure to determine a 11 i 4 14 e
7 6
whether an edge 8 10
h g 2
f
connects vertices in 1
We would add
different components edge (c, f)
28
Operations on Disjoint Data Sets
• MAKE-SET(u) – creates a new set whose only
member is u
• FIND-SET(u) – returns a representative element
from the set that contains u
– Any of the elements of the set that has a particular
property
– E.g.: Su = {r, s, t, u}, the property is that the element be
the first one alphabetically
FIND-SET(u) = r FIND-SET(s) = r
– FIND-SET has to return the same value for a given set
29
Operations on Disjoint Data Sets
• UNION(u, v) – unites the dynamic sets that
contain u and v, say Su and Sv
– E.g.: Su = {r, s, t, u}, Sv = {v, x, y}
v
V-S
33