Professional Documents
Culture Documents
• Spanning Tree
– A tree (i.e., connected, acyclic graph) which contains
Design and Analysis of all the vertices of the graph
• Minimum Spanning Tree
Algorithms – Spanning tree with the minimum sum of weights
8 7
b c d
4 9
2
Minimum Spanning Trees (MST) 11
a i 4 14 e
7 6
8 10
Chapter 23
• Spanning forest g
1
g 2
f
1 2
3 4
3 4
5 6
5 6
1
Growing a MST – Generic Approach Generic MST algorithm
• Grow a set A of edges (initially 1. A ←
empty) 2. while A is not a spanning tree
• Incrementally add edges to A
do find an edge (u, v) that is safe for A
8 7
b c d 3.
such that they would belong 4 9
2
to a MST a 11 i 4 14 e 4. A ← A {(u, v)} b
8
c
7
d
7 6 4 9
8 10 2
h g f 5. return A
Idea: add only “safe” edges 1 2 a 11 i 4 14 e
7 6
8 10
– An edge (u, v) is safe for A if and h
1
g 2
f
7 8
– In any MST, there has to be one edge (at least) that (S, V - S) if one endpoint is in S
connects S with V - S and the other in V – S
– Why not choose the edge with minimum weight (h,g)?
9 10
9 10
crossing a cut its weight is minimum over all • Case1: If T includes (u,v), then
edges crossing the cut it would be safe for A
u
• Case2: Suppose T does not include
– Note that for a given cut, there can be > 1 light
the edge (u, v)
edges crossing it • Idea: construct another MST T’ v
V-S
that includes A {(u, v)}
11 12
11 12
2
Theorem - Proof Theorem – Proof (cont.)
• T contains a unique path p between u and v T’ = T - {(x, y)} {(u, v)}
• Path p must cross the S
Have to show that T’ is an MST: S
x
cut (S, V - S) at least x • (u, v) is a light edge
once: let (x, y) be that edge w(u, v) ≤ w(x, y) u y
u y
p
• Let’s remove (x,y) breaks p • w(T ’) = w(T) - w(x, y) + w(u, v)
v
T into two components. v ≤ w(T) V-S
V-S
• Adding (u, v) reconnects the components • Since T is a spanning tree
T’ = T - {(x, y)} {(u, v)} w(T) ≤ w(T ’) T’ must be an MST as well
13 14
13 14
p
• Since T’ is an MST
v
(u, v) is safe for A V-S
15 16
15 16
17 18
17 18
3
PRIM(V, E, w, r)
1. Q←
2. for each u V
3. do key[u] ← ∞
4. π[u] ← NIL
5. INSERT(Q, u)
6. DECREASE-KEY(Q, r, 0) ► key[r] ← 0
Prim’s Algorithm
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))
19 20
19 20
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
[b] = a
Example/Dry Run 4
b
8
2
c
7
d
9
key [b] = 4
key [h] = 8 [h] = a
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
21 22
21 22
Example Example
4
8 key [c] = 8 [c] = b 4 8 7 key [h] = 7 [h] = i
8 7 8
b c d key [h] = 8 [h] = a - unchanged b c
7
d key [g] = 6 [g] = i
4 9 9
2 4
8 8 2 2 7 46 7
a 11 i 4 14 e 11
a i 4 14 e
7 6 Q = {c, d, e, f, g, h, i} VA = {a, b} 6
Q = {d, e, f, g, h} VA = {a, b, c, i}
8 10 7
Extract-MIN(Q) f
8
h g f Extract-MIN(Q) c h g f
10
1 2
8 1 2
key [d] = 7 [d] = c 87
6 4
4 8
7 key [g] = 2 [g] = f
8 7 key [f] = 4 [f] = c 4 8 7
4
b c d
9 key [i] = 2 [i] = c b
8
c
7
d key [d] = 7 [d] = c unchanged
2 2
9
11 14
4
2 2
10 key [e] = 10 [e] = f
a i 4 e
a 11 i 14 e
8
7 6
7 4 8 2
4 7 10 2 7
10 7 6
8 10
h g 2
f Q = {d, e, f, g, h, i} VA = {a, b, c} Q = {d, e, g, h} VA = {a, b, c, i, f}
1 h g f
8 4
Extract-MIN(Q) i
1
6
2
4
Extract-MIN(Q) g
23
7 2 24
23 24
4
Example 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} 4 8 7
a 11 i 14 e
4
Extract-MIN(Q) h 8 7
8
7 6
10
b c d
9 9
key [e] = 9 [e] = f
4
2 2 10
h
1
g 2
f 9
a 11 i 4 14 e
7 2 4 7 10
1
7 6 Q = {e} VA = {a, b, c, i, f, g, h, d}
4 8 7 8 10
8 7
Q = {d, e} VA = {a, b, c, i, f, g, h} h g f Extract-MIN(Q) e
b c d 1 2
4 9 Extract-MIN(Q) d 1 2 4 Q = VA = {a, b, c, i, f, g, h, d, e}
2 2 10
a 11 i 4 14 e
7 6
8 10
h g 2
f
1
1 2 4
25 26
25 26
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)
7. while Q Min-heap
Analysis 8. do u ← EXTRACT-MIN(Q)
Executed |V| times
Takes O(lgV)
operations:
O(VlgV)
9. for each v Adj[u] Executed O(E) times total
10. do if v Q and w(u, v) < key[v] Constant O(ElgV)
27 28
27 28
29 30
29 30
5
A different instance of the
generic approach
S
(instance 1)
(instance 2)
u
tree1
v
u
components
– Initially, each component is a single
vertex v
tree2
• Any safe edge merges two of
these components into one
31 – Each component is a tree 32
31 32
33 34
KRUSKAL(V, E, w) Example
1. A← 1. Add (h, g) {g, h}, {a}, {b}, {c}, {d}, {e}, {f}, {i}
for each vertex v V
8 7
2. b c d
9
2. Add (c, i) {g, h}, {c, i}, {a}, {b}, {d}, {e}, {f}
4
2 3. Add (g, f) {g, h, f}, {c, i}, {a}, {b}, {d}, {e}
3. do MAKE-SET(v) a 11 14 e 4.
i 4 Add (a, b) {g, h, f}, {c, i}, {a, b}, {d}, {e}
4. sort E into non-decreasing order by w 8
7 6
10 5. Add (c, f) {g, h, f, c, i}, {a, b}, {d}, {e}
35 36
6
Implementation of Kruskal’s Algorithm Operations on Disjoint Data Sets
8 • MAKE-SET(u) – creates a new set whose only
• Uses a disjoint-set data
7
b c d
4
2
9 member is u
structure (see Chapter a 11 i 4 14 e • FIND-SET(u) – returns a representative element
7 6
21) to determine whether 8 10
from the set that contains u
h g 2
f
1
an edge connects – Any of the elements of the set that has a particular
We would add
vertices in different edge (c, f) property
components – 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
37 38
37 38
39 40
6. do if FIND-SET(u) FIND-SET(v) x
applies to Kruskal’s
7. then A ← A {(u, v)} O(lgV) u y
8. UNION(u, v) algorithm too
9. return A
v
- Running time: O(V+ElgE+ElgV)=O(ElgE) O(ElgV) V-S
- Since E=O(V2), we have lgE=O(2lgV)=O(lgV) 41 42
41 42
7
Problem 1 Problem 1 (cont.)
• (Exercise 23.2-3, page 573) Compare Prim’s algorithm
with and Kruskal’s algorithm assuming:
(b) dense graphs
In this case, E=O(V2)
(a) sparse graphs:
In this case, E=O(V)
Kruskal:
Kruskal:
O(ElgE)=O(V2lgV2)=O(2V2lgV)=O(V2lgV)
O(ElgE)=O(VlgV)
Prim:
Prim: - binary heap: O(ElgV)=O(V2lgV)
- binary heap: O(ElgV)=O(VlgV) - Fibonacci heap: O(VlgV+E)=O(VlgV+V2)=O(V2)
- Fibonacci heap: O(VlgV+E)=O(VlgV)
43 44
43 44
45 46
Problem 3 Problem 4
• Suppose that some of the weights in a connected • (Exercise 23.2-2, page 573) Analyze Prim’s
graph G are negative. Will Prim’s algorithm still algorithm assuming:
work? What about Kruskal’s algorithm? Justify
your answers. (a) an adjacency-list representation of G
– Yes, both algorithms will work with negative weights.
Review the proof of the generic approach; there is no O(ElgV)
assumption in the proof about the weights being
positive.
(b) an adjacency-matrix representation of G
O(ElgV+V2)
47 48
47 48
8
PRIM(V, E, w, r) PRIM(V, E, w, r)
1. Q← 1. Q←
Total time: O(VlgV + ElgV) = O(ElgV) Total time: O(VlgV + ElgV+V2) = O(ElgV+V2)
2. for each u V 2. for each u V
3. do key[u] ← ∞ O(V) if Q is implemented 3. do key[u] ← ∞ O(V) if Q is implemented
as a min-heap as a min-heap
4. π[u] ← NIL 4. π[u] ← NIL
5. INSERT(Q, u) 5. INSERT(Q, u)
6. DECREASE-KEY(Q, r, 0) ► key[r] ← 0 O(lgV) 6. DECREASE-KEY(Q, r, 0) ► key[r] ← 0 O(lgV)
49 50
Problem 5 Problem 6
• Find an algorithm for the “maximum” spanning • (Exercise 23.1-8, page 567) Let T be a MST of
tree. That is, given an undirected weighted a graph G, and let L be the sorted list of the
graph G, find a spanning tree of G of maximum edge weights of T. Show that for any other MST
cost. Prove the correctness of your algorithm. T’ of G, the list L is also the sorted list of the
– Consider choosing the “heaviest” edge (i.e., the edge edge weights of T’
associated with the largest weight) in a cut. The
generic proof can be modified easily to show that this
approach will work.
– Alternatively, multiply the weights by -1 and apply
either Prim’s or Kruskal’s algorithms without any
modification at all! T, L={1,2} T’, L={1,2}
51 52
51 52