Minimum Spanning Trees (Ch. 23) ! Minimum Spanning Trees!
Minimum Spanning Trees (Ch. 23) ! Minimum Spanning Trees!
d e
1! 6!
f g h a 3! b c
5! 3!
6! 2!
d e 8!
5! 2!
f g h
4! 7!
1! 6!
a 3! b c
5! 3!
1! 6! 6! 2!
a d e
3! b 5! 3!
c
5! 2! 8!
6! 2! f g h
d e 8! 4! 7!
5! 2!
f g h
4! 7!
If there are 2 spanning trees T1 and T2 on G that both have the Algorithm MST-Kruskal (G)
MST property, then T1 and T2 have same total weight. R = E // R is initially set of all edges
F = " // F is set of edges in a spanning tree of a subgraph of G
0. sort all edges of R in increasing order
1. while (R is not empty)
2. do remove the lightest-weight edge, (v,w), from R
3. if (v,w) does not make a cycle in F
4. then add (v,w) to F
5. return F
Disjoint Sets (Ch. 21)! Data Structures for Disjoint Sets!
A disjoint-set data structure Comment 1: The Make-Set operation is only used during the
o! maintains a collection of disjoint subsets C = s1,s2,
initialization of a particular algorithm.
…,sm, where each si is identified by a representative
element (set id).
o! set of underlying elements make up the sets Comment 2: We assume there is an array of pointers to each
U = {1,2,…,n} x # U (so we never have to search for a particular
Operations on C: element, just which id the set has). Thus the problems
•! Make-Set(x): x # U, creates singleton set {x} we’re trying to solve are how to join the sets (Union)
•! Union(x,y): x, y # U and are id’s of their resp. sets,
sx and sy; replaces sets sx and sy with a set that is and how to find the id of the set containing a particular
sx $ sy and returns the id of the new set. element (Find-Set) efficiently.
•! Find-Set(x): x # U, returns the id of the set
containing x.
nodes! nodes!
m!
nodes!
nodes! d!
So Find-Set(x) still has worst-case time of O(lg n), but subsequent
Show h = max(hx + 1, hy) % lg k Find-Sets for nodes that used to be ancestors of x (or subsequent
•! hx + 1 % lg m + 1 % lg(k/2) + 1 = lg k – 1 + 1 = lg k finds for x itself) will now be very fast -- O(1).
•! hy % lg(k – m) % lg k (for positive m)
Path Compression Analysis! Kruskal’s MST Algorithm!
Idea:
- Sorts edges, then add the minimum-weight edge (u,v) to the
Analysis in book (section 21.4). They prove that the running time
MST if u and v aren’t in same subgraph. Uses union-
for m disjoint-set operations on n elements is
find operations to determine when nodes are in same subgraph
iteration 2: Q = Q – {b}!
Prim’s Improved MST Algorithm! Q! xa xb c d e f g h! MST-Prim (G, r)!
MST-Prim (G, r)! 1.! insert each v #V into Q with key[v] & ',"
1.! insert each v #V into Q with key[v] & ',"
$! " a "! b a a " " "! #[v] & "!
Start at node a:! #[v] & "! key! 0 1 %! 6 6 3 ' ' '! 2.! key[r] & 0 // root of MST!
2.! key[r] & 0 // root of MST! 3.! while Q # "!
Q! a b c d e f g h! 3.! while Q # "! T = {(b,a)}! 4.! u & extract-min(Q)!
$! " " " " " " " "! 4.! u & extract-min(Q)! iteration 3: Q = Q – {e}! 5.! for each neighbor v of u!
5.! for each neighbor v of u!
key! 0 ' ' ' ' ' ' '! 6.! if v # Q and wt(u,v) < key[v] then!
Q! xa xb c d xe f g h! 6.! if v # Q and wt(u,v) < key[v] then!
7.! # [v] & u!
7.! # [v] & u! $! " a e e a " e e! 8.! key[v] & wt(u,v)!
iteration 1: Q = Q – {a}! 8.! key[v] & wt(u,v)! 9.! end while!
9.! end while!
key! 0 1 3 2 3 ' 2 8!
Q! ax b c d e f g h!
$! " a " a a " " "! T = {(b,a), (e,a)}!
key! 0 1 ' 6 3 ' ' '! 1! 6! iteration 4: Q = Q – {d}! 1! 6!
a 3! b c a 3! b 5! c
5! 3! Q! ax xb c dx xe f g h! 3!
6! 6!
T = {"}! d 2! e $! " a e e a " e e! d 2! e 8!
2! 8! 2!
f 5! g h
key! 0 1 3 2 3 ' 2 8! f 5! g h
4! 4! 7!
7! T = {(b,a), (e,a), (d,e)}!
iteration 5: Q = Q – {g}!
Q! xa xb c dx xe f gx h! MST-Prim (G, r)! MST-Prim (G, r)!
1.! insert each v #V into Q with key[v] & '," 1.! insert each v #V into Q with key[v] & ',"
$! " a e e a g e g! #[v] & "! #[v] & "!
key! 0 1 3 2 3 4 2 7! 2.! key[r] & 0 // root of MST! 2.! key[r] & 0 // root of MST!
3.! while Q # "! 3.! while Q # "!
T = {(b,a), (e,a), (d,e), (g,e)}! 4.! u & extract-min(Q)! iteration 7: Q = Q – {h} = "! 4.! u & extract-min(Q)!
iteration 6: Q = Q – {c}! 5.! for each neighbor v of u! 5.! for each neighbor v of u!
6.! if v # Q and wt(u,v) < key[v] then!
Q! xa xb xc dx xe xf gx h! 6.! if v # Q and wt(u,v) < key[v] then!
Q! xa xb xc dx xe f gx h! 7.! # [v] & u! $! " a e e a g e g! 7.! # [v] & u!
8.! key[v] & wt(u,v)! 8.! key[v] & wt(u,v)!
$! " a e e a g e g! 9.! end while!
key! 0 1 3 2 3 4 2 7! 9.! end while!
key! 0 1 3 2 3 4 2 7!
T = {(b,a), (e,a), (d,e), (g,e), (c,e), (f,g), (h,g)}!
T = {(b,a), (e,a), (d,e), (g,e), (c,e)}!
iteration 6: Q = Q – {f}! 1! 6!
a 3! b 5! c DONE!
a
1! 6!
Q! ax xb xc dx xe xf gx h! 3! 3! b 5! 3!
c
6!
$! " a e e a g e g! d 2! e 8! 6!
d 2! e
2! 2! 8!
key! 0 1 3 2 3 4 2 7! f 5! g h f 5! g h
4! 7! 4! 7!
T = {(b,a), (e,a), (d,e), (g,e), (c,e), (f,g)}!
Running Time of Prim’s MST Algorithm! Correctness of Prim’s Algorithm!
•! Assume Q is implemented with a binary min-heap (heapsort)
•! How can we tell if v # Q without searching heap? (line 6) Let Ti be the tree after the ith iteration of the while loop
- keep an array for nodes with boolean flag indicating if in heap
Lemma: For all i, Ti is a subtree of some MST of G.
Running time: Proof: by induction on i
•! initialize Q: O(V) time
•! decrease v0’s key = O(lg V) time Basis: when i = 0, T0 = ", ok because trivial MST subtree
•! while loop... IHOP: Assume Ti is a subtree of some MST M
in each of V iterations of while loop:
Induction Step: Now show that Ti +1 is a subtree of some MST (possibly
extract min = O(lg V) time
different from M)
update T = O(1) time ==> O(V lg V) total
over all iterations (combined):
Let (u,v) be the edge added in iteration i + 1
check neighbors of u (line 9): O(E) executions
when condition test and update ( = O(1) time
when decreasing v’s key = O(lg V) time = O(E lg V)
So, the grand total is:
O(V lg V + E lg V) = O(E lg V) ( asymptotically the
same as Kruskal’s)
Let (x, y) be the first edge in p with x in Ti and y not in Ti. We know
this edge exists because the algorithm will not add edge (u,v) to a cycle.
How do we know this?