L10 661 feb 17 2009 abhi shelat
General-MST-Strategy(G =(V, E)) 1 A 2 repeat V 1 times: 3 Pick a cut (S, V S) that respects A 4 Let e be min-weight edge over cut (S, V S) 5 A A {e}
prim General-MST-Strategy(G =(V, E)) 1 A 2 repeat V 1 times: 3 Pick a cut (S, V S) that respects A 4 Let e be min-weight edge over cut (S, V S) 5 A A {e} A is a subtree edge e is lightest edge that grows the subtree
prim b 8 d 8 g 10 7 2 a 9 e 5 9 i 12 3 3 11 c 1 f 6 h
prim e d f c b a 10 8 1 3 3 9 12 6 5 9 11 2 i g h 8 7 e d f c b a 10 8 1 3 3 9 12 6 5 9 11 2 i g h 8 7 e d f c b a 10 8 1 3 3 9 12 6 5 9 11 2 i g h 8 7 e d f c b a 10 8 1 3 3 9 12 6 5 9 11 2 i g h 8 7 e d f c b a 10 8 1 3 3 9 12 6 5 9 11 2 i g h 8 7 e d f c b a 10 8 1 3 3 9 12 6 5 9 11 2 i g h 8 7 e d f c b a 10 8 1 3 3 9 12 6 5 9 11 2 i g h 8 7 e d f c b a 10 8 1 3 3 9 12 6 5 9 11 2 i g h 8 7 e d f c b a 10 8 1 3 3 9 12 6 5 9 11 2 i g h 8 7
implementation idea: maintain the set A set key of all nodes not adjacent to A to be
implementation idea: use a priority queue to keep track of light edges makequeue: insert: extractmin: decreasekey:
implementation prim(g =(V, E)) 1 Q Q is a Priority Queue 2 Initialize each v V with key k v, π v nil 3 Pick a starting node r and set k r 0 4 Insert all nodes into Q with key k v. 5 while Q 6 do u extract-min(q) 7 for each v Adj (u) 8 do if v Q and w(u, v) <k v 9 then π v u 10 decrease-key(q, v, w(u, v)) Sets k v w(u, v) A = {(v, π v ) : v V {r} Q}
prim b 8 8 d 0 8 g 8 10 7 2 a 9 e 7 5 9 i 11 12 3 3 11 prim(g =(V, E)) c 1 Q Q is a Priority Queue 2 Initialize each v V with key k v, π v nil 3 Pick a starting node r and set k r 0 4 Insert all nodes into Q with key k v. 5 while Q 6 do u extract-min(q) 7 for each v Adj (u) 8 do if v Q and w(u, v) <k v 9 then π v u 10 decrease-key(q, v, w(u, v)) Sets k v w(u, v) 1 f 6 6 h 5
prim b 8 8 d 0 8 g 8 10 7 2 a 9 e 7 5 9 i 11 12 3 3 11 prim(g =(V, E)) c 1 Q Q is a Priority Queue 2 Initialize each v V with key k v, π v nil 3 Pick a starting node r and set k r 0 4 Insert all nodes into Q with key k v. 5 while Q 6 do u extract-min(q) 7 for each v Adj (u) 8 do if v Q and w(u, v) <k v 9 then π v u 10 decrease-key(q, v, w(u, v)) Sets k v w(u, v) 1 f 6 6 h 5
implementation prim(g =(V, E)) 1 Q Q is a Priority Queue 2 Initialize each v V with key k v, π v nil 3 Pick a starting node r and set k r 0 4 Insert all nodes into Q with key k v. 5 while Q 6 do u extract-min(q) 7 for each v Adj (u) 8 do if v Q and w(u, v) <k v 9 then π v u 10 decrease-key(q, v, w(u, v)) Sets k v w(u, v)
implementation prim(g =(V, E)) 1 Q Q is a Priority Queue 2 Initialize each v V with key k v, π v nil 3 Pick a starting node r and set k r 0 4 Insert all nodes into Q with key k v. 5 while Q 6 do u extract-min(q) 7 for each v Adj (u) 8 do if v Q and w(u, v) <k v 9 then π v u 10 decrease-key(q, v, w(u, v)) Sets k v w(u, v) O(V log V + E log V) =O(E log V)
faster implementation prim(g =(V, E)) 1 Q Q is a Priority Queue 2 Initialize each v V with key k v, π v nil 3 Pick a starting node r and set k r 0 4 Insert all nodes into Q with key k v. 5 while Q 6 do u extract-min(q) 7 for each v Adj (u) 8 do if v Q and w(u, v) <k v 9 then π v u 10 decrease-key(q, v, w(u, v)) Sets k v w(u, v) O(E + V log V)
fibonacci heaps insert: findmin: extractmin: decreasekey:
amortized analysis
binary counter
binary counter Increment(A) 1 i 0 2 while i < len(a) and A i =1 3 do A i 0 4 i i +1 5 if i < len(a) 6 then A i 1
binary counter better analysis Increment(A) 1 i 0 2 while i < len(a) and A i =1 3 do A i 0 4 i i +1 5 if i < len(a) 6 then A i 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1
potential method data structure A
potential method data structure A ĉ i = c i + Φ(A i ) Φ(A i 1 )
image: edupics.com
amortized cost of n ops: n i=1 ĉ i =
binary counter 0 0 0 0 0 0 0 0 let let! b i " t i be the... be the... Φ(A i )= ĉ i =
heaps
binomial heaps B 4 B 4 B 5
fibonacci heap min picture courtesy of jeff erickson
each node has 4 pointers 2 fields: degree marked D(n)
fibonacci heap min
fibheap potential min Φ(H) =
key idea: be lazy put off the work until last minute
create new heap
insert node min
findmin min
extractmin min two steps: split consolidate
extractmin example min[h] - -----
min[h] Q Q@ (d)~~~~~ @-t--------t A~
0 1 2 3 A// ),x4 -- I
0 1 2 3 A 30 e,' AN -- ----- -- -
A/ We) X 0-- xw30@ R 3544 5--
x 7 ---- 21*-I 41 46 17 23w 0 1 2 3 A// / : 38 2 :32
48 0 1 2 3 17 A I
0 1 2 3 A-- l 10ltf0l 9 ----- 24 17 2
3 1 2 3 )-.-...-- -- -----lb 38 41
min[h] -- ---- ---- 7 38
analysis min cost 1. extract 2. add children 3. consolidate potential before: potential after: amortized cost:
decrease key operation what changes in the heap: 0 1 2 3 A 30 e,' AN -- ----- -- -
decrease key decreasekey(v) promote node v to the root list update min of heap if necessary if 2 children of a node have been promoted, promote parent as well (recursively)
min[h] W~tgW decreasekey(46, 15)
min[hm - --- decreasekey(35,5)
-- - 4
min[h] Mt @
min[h] V--- 0 -- ----- --------- 7
analysis decreasekey(v) promote node v to the root list update min of heap if necessary if 2 children of a node have been promoted, promote parent as well (recursively) actual cost: potential before: potential after:
finally prove: D(n) = log φ n size(x) =
lemma: for node x, consider children (degrees)
lemma: F k+2 = 1 + k i=0 F i
lemma: size(x) F deg(x)+2 s i :
thm: D(n) log φ n