260 likes | 419 Views
21 장 . Fibonacci Heaps. 숭실대학교 인공지능 연구실 석사 2 학기 김완섭. 발표 순서. Introduction Structure of Fibonacci heaps Mergeable-heap operations Insert, Minimum, Extract-Min, Union Decreasing a key and deleting a node Bounding the maximum degree. Introduction (1/2). Introduction (2/2).
E N D
21장. Fibonacci Heaps 숭실대학교 인공지능 연구실 석사 2학기 김완섭
발표 순서 • Introduction • Structure of Fibonacci heaps • Mergeable-heap operations • Insert, Minimum, Extract-Min, Union • Decreasing a key and deleting a node • Bounding the maximum degree
Introduction (2/2) • Run in O(1) amortized time. • Make-Heap, Insert, Union, Decrease-key • Except Extract-Min, Delete • From theoretical standpoint • FH are very desirable. • From a practical standpoint • Less desirable than ordinary binary heap. • The big constant factors. • Programming complexity.
23 3 24 52 38 23 17 46 7 3 41 35 52 23 7 3 17 24 18 52 38 23 26 46 39 41 35 Structure of FH (1/4) • Similarity to BH • A collection of tree. (like a binomial tree.) • Difference with BH • Have a more relaxed structure. • Unlike binomial heaps, tree within FH are rooted but unordered. • Work that maintains the structure can delayed until it is convenient to perform.
Min[H] Root-list 23 7 3 17 24 Child-list 18 52 38 23 26 46 Parent Child 39 41 35 41 Structure of FH (2/4) • Each Node x contains • Parent (x) • Left (x) • Child (x) • Right (x)
Min[H] Root-list 23 7 3 17 24 Child-list 18 52 38 23 26 46 Parent Child 39 41 35 41 Structure of FH (3/4) Min[H] - Pointer to the root of the tree containing minmum key. Root-list – Roots of all tree are linked together in a circular linked-list Child-list – children of node x are linked together in a circular linked-list. Degree[x] – Number of child-list of node x Marked[x] – indicates whether node x has lost a child since the last time x was made the child of another node.
Structure of FH (4/4) Min[H] Root-list 23 7 3 17 24 Child-list 18 52 38 23 26 46 Parent Child 39 41 35 41 n[H] - Number of nodes in H ex) n[H] = 12 Potential function = T(H) + 2m(H) ex) PF(H) = 5 + 2*3 D(n) - Maximum degree ex) D(n) = lg(12) = 4
Op1. MAKE-HEAP • MAKE-FIB-HEAP • Make an empty Fibonacci heap • N[H] = 0 • Min[H] = NIL • Amortized cost • PF[H] = t[H] – 2*m[H] = 0 • Thus, the amortized cost is thus equal to its O(1) actual cost.
23 7 3 17 24 18 52 38 23 26 46 39 41 35 23 7 21 3 17 24 18 52 38 23 26 46 39 41 35 Op1. INSERT Min[H] FIB-HEAP-INSERT (H,x) 1 degree(x) ← 02 p[x] ← NIL3 child[x] ← nil4 left[x] ← x5 right[x] ← x6 mark[x] ← False7 Concat the root list containing x with root list H8if min[H] = NIL or key[x] <key[min[H]]9 then min[H] ← x 10 n[H] ← n[H] + 1 FIB-HEAP-INSERT (H, 21) Min[H]
Op1. INSERT • Amortized cost • PF[H’] – PF[H] = 1 • t(H’) = t(H) + 1 • M(H’) = m(H) • PF[H’] - PF[H] = (t[H’] – 2*m[H’] ) – (t[H] – 2*m[H] ) = (t[H] – 2*m[H] +1) - (t[H] – 2*m[H] ) = 1 • The amortized cost is O(1) +1 = O(1) actual cost.
Op2. FIND-MIN • The minimum node of a FH H is given by the pointer min[H] . • We can find the mimimum node in O(1) Min[H] 23 7 3 17 24 Child-list 18 52 38 23 26 46 Parent Child 39 41 35 41
23 7 3 17 24 18 52 38 23 26 46 39 41 35 23 7 21 3 17 24 18 52 38 23 26 46 39 41 35 Op3. UNION FIB-HEAP-UNION (H1,H2 ) 1 H ← MAKE-FIB-HEAP()2 min[H] ← min[H1]3 Concat the root list of H2 with the root list of H4if (min[H1] = NIL) or (min[H2] != NIL and min[H2] < min[H1]])5 then min[H] ← MIN[H2]6 n[H] ← n[H1] + n[H1] 7 free the objects H1 and H28 return H
Op4. EXTACT-MIN Min[H] 23 7 3 17 24 21 FIB-HEAP-EXTRACT-MIN (H) 1 z ← min[H] 2 if z != NIL then3 for each child x of z4 do add x to the root-list of H5 remove z from the root-list of H 6 if z = right[z]7 then min[H] ← NIL8 else min[H] ← right[z]9CONSOLIDATE(H)10 n[H] ← n[H] – 111 return z 18 52 38 30 26 46 39 41 35 FIB-HEAP-EXTRACT-MIN (H) Min[H] 18 7 38 24 17 41 21 39 23 52 26 30 46 35
Op4-1. CONSOLIDATE CONSOLIDATE (H) 1 for i ← 0 to D (n[H]) 2 do A[I] NIL ← NIL3 for each node w in the root-list of H4 do x ← w5 d ← degree6 while A[d] != NIL7 do y ← A[d]8 if key[x] > key[y]9 then exchange(x, y)10FIB-HEAP-LINK (H,y,x) 11 A[d] ← NIL 12 d ← d + 113 min[H] ← NIL14 for i = 0 to D(n[H]) 15 do if A[i] != NIL16 then add A[i] to the root-list17 if min[H] = NIL or key[A[i]] < key[min[H]]18 then min[H] ← A[i]19 then exchange(x, y)
Op4-1. CONSOLIDATE Min[H] 23 7 21 18 52 38 17 24 CONSOLIDATE (H) 39 41 30 26 46 35 Min[H] 18 7 38 24 17 41 21 39 23 52 26 30 46 35
EX)FIB-HEAP-EXTRACT-MIN (H) (1/4) Min[H] Min[H] 23 7 3 17 24 23 7 21 18 52 38 17 24 21 39 41 30 18 52 38 30 26 46 26 46 39 41 35 35 0 1 2 3 4 0 1 2 3 4 23 7 21 18 52 38 17 24 23 7 21 18 52 38 17 24 39 41 39 41 30 30 26 46 26 46 35 35
EX)FIB-HEAP-EXTRACT-MIN (H) (2/4) 0 1 2 3 4 0 1 2 3 4 23 7 21 18 52 38 17 24 21 7 18 52 38 17 24 39 41 23 30 39 41 26 46 30 26 46 35 35 0 1 2 3 4 0 1 2 3 4 7 18 52 38 21 7 21 18 52 38 24 24 17 23 39 41 23 17 39 41 26 46 26 30 46 30 35 35
0 1 2 3 4 0 1 2 3 4 7 18 52 38 7 18 52 38 21 21 24 24 17 17 23 23 39 41 39 41 26 30 26 30 46 46 35 35 0 1 2 3 4 0 1 2 3 4 18 18 7 38 7 38 24 24 17 41 17 41 21 39 23 21 39 23 52 52 26 30 26 30 46 46 35 35
0 1 2 3 4 18 7 38 24 17 41 21 39 23 52 26 30 46 35 EX)FIB-HEAP-EXTRACT-MIN (H) (4/4) Min[H] 18 7 38 24 17 41 21 39 23 52 26 30 46 35
Delete a node (1/2) • Key idea to delete a node • Decreasing a key • Extract minimum node of H
Decreasing a key 3 18 38 FIB-HEAP-DECREASE-KEY (H , x, k) 1 if k > key[x] 2 then error “new key is greater than currently key” 3 key[x] ← k4 y ← p[x] 5 if y != NIL and key[x] < key[y] 6 then CUT(H,x,y)7CASCADING-CUT(H, y)8 if key[x] < key[min[H]] 9 then min[H] ← x 24 17 23 21 39 41 46 26 30 52 35 FIB-HEAP-DECREASE-KEY (H , 46, 15) 15 3 18 38 24 17 23 21 39 41 26 30 52 35
15 7 18 38 24 17 23 21 39 41 26 30 52 35 Delete a node; DECREASE-KEY(H, 35, 5) Min[H] Min[H] 5 7 18 38 15 24 17 23 21 39 41 26 30 52 Min[H] Min[H] 26 24 7 18 38 5 15 26 7 18 38 5 15 17 23 21 39 41 24 17 23 21 39 41 30 52 30 52
15 7 18 38 24 17 23 21 39 41 26 30 52 35 Delete a node; DELETE(H, 35) Min[H] CALL DELETE(H, 35) 1. DECREASE-KEY(H,35, -@) 2. EXTRACT-MIN(H) DECREASE-KEY (H, -@) Min[H] Min[H] 26 24 7 18 38 -@ 15 26 24 7 18 38 15 17 23 21 39 41 17 23 21 39 41 30 52 30 52 EEXTRACT-MIN (H)
Bounding the maximum degree • D(n) • upper bound degree on the n-node FH • We must prove D(n) = [lg n] • FIB-HEAP-DECREASE-KEY • FIB-HEAP-DELETE