Download presentation
Presentation is loading. Please wait.
Published byHandoko Ade Kartawijaya Modified over 6 years ago
1
21장. Fibonacci Heaps 숭실대학교 인공지능 연구실 석사 2학기 김완섭
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
3
Introduction (1/2) Procedure Binary heap (worst-case) Binomial heap
Fibonacci Heap (amortized) Make-Heap (1) Insert (lg n) Minimum Extract-Min Union (n) Decrease-key Delete
4
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.
5
Structure of FH (1/4) Similarity to BH Difference with BH
23 7 3 17 24 38 52 41 46 35 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. 23 7 3 17 24 18 38 39 52 41 26 46 35
6
Structure of FH (2/4) Each Node x contains Parent (x) Child (x)
23 7 3 17 24 18 38 39 52 41 26 46 35 Min[H] Parent Child Root-list Child-list Each Node x contains Parent (x) Left (x) Child (x) Right (x)
7
Structure of FH (3/4) 23 7 3 17 24 18 38 39 52 41 26 46 35 Min[H] Parent Child Root-list Child-list 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.
8
Structure of FH (4/4) n[H] - Number of nodes in H ex) n[H] = 12
Min[H] Root-list 23 7 3 17 24 Child-list 18 52 38 23 26 46 Parent Child 39 41 41 35 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
9
Op1. MAKE-HEAP MAKE-FIB-HEAP Amortized cost
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.
10
Op1. INSERT FIB-HEAP-INSERT (H,x)
Min[H] FIB-HEAP-INSERT (H,x) 1 degree(x) ← 0 2 p[x] ← NIL 3 child[x] ← nil 4 left[x] ← x 5 right[x] ← x 6 mark[x] ← False 7 Concat the root list containing x with root list H 8 if min[H] = NIL or key[x] <key[min[H]] then min[H] ← x 10 n[H] ← n[H] + 1 23 7 3 17 24 18 38 39 52 41 26 46 35 FIB-HEAP-INSERT (H, 21) Min[H] 23 7 3 17 24 18 38 39 52 41 26 46 35 21
11
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.
12
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 41 35
13
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 H 4 if (min[H1] = NIL) or (min[H2] != NIL and min[H2] < min[H1]]) then min[H] ← MIN[H2] 6 n[H] ← n[H1] + n[H1] 7 free the objects H1 and H2 8 return H 23 7 3 17 24 18 38 39 52 41 26 46 35 23 7 3 17 24 18 38 39 52 41 26 46 35 21
14
Op4. EXTACT-MIN FIB-HEAP-EXTRACT-MIN (H )
23 7 21 3 17 24 FIB-HEAP-EXTRACT-MIN (H ) 1 z ← min[H] 2 if z != NIL then for each child x of z do add x to the root-list of H remove z from the root-list of H if z = right[z] then min[H] ← NIL else min[H] ← right[z] CONSOLIDATE(H) n[H] ← n[H] – 1 11 return z 18 52 38 30 26 46 39 41 35 FIB-HEAP-EXTRACT-MIN (H ) Min[H] 7 18 38 24 17 23 21 39 41 26 46 30 52 35
15
Op4-1. CONSOLIDATE CONSOLIDATE (H )
1 for i ← 0 to D (n[H]) do A[I] NIL ← NIL 3 for each node w in the root-list of H do x ← w d ← degree while A[d] != NIL do y ← A[d] if key[x] > key[y] then exchange(x, y) FIB-HEAP-LINK (H,y,x) A[d] ← NIL d ← d min[H] ← NIL 14 for i = 0 to D(n[H]) do if A[i] != NIL then add A[i] to the root-list if min[H] = NIL or key[A[i]] < key[min[H]] then min[H] ← A[i] then exchange(x, y)
16
Op4-1. CONSOLIDATE CONSOLIDATE (H ) Min[H] 23 7 21 18 52 38 17 24 39
41 30 26 46 35 Min[H] 7 18 38 24 17 23 21 39 41 26 46 30 52 35
17
EX) FIB-HEAP-EXTRACT-MIN (H ) (1/4)
23 7 21 3 17 24 23 7 21 18 52 38 17 24 18 52 38 30 26 46 39 41 30 26 46 39 41 35 35 1 2 3 4 1 2 3 4 23 7 21 18 52 38 17 24 23 7 21 18 52 38 17 24 39 41 30 26 46 39 41 30 26 46 35 35
18
EX) FIB-HEAP-EXTRACT-MIN (H ) (2/4)
1 2 3 4 1 2 3 4 23 7 21 18 52 38 17 24 7 21 18 52 38 17 24 39 41 30 26 46 23 39 41 30 26 46 35 35 1 2 3 4 1 2 3 4 7 21 18 52 38 7 21 18 52 38 24 24 17 23 39 41 17 23 39 41 26 46 26 46 30 30 35 35
19
1 2 3 4 1 2 3 4 7 21 18 52 38 7 21 18 52 38 24 17 23 24 17 39 41 23 39 41 26 46 30 26 46 30 35 35 1 2 3 4 1 2 3 4 7 18 38 7 18 38 24 17 23 21 39 41 24 17 23 21 39 41 26 46 30 52 26 46 30 52 35 35
20
EX) FIB-HEAP-EXTRACT-MIN (H ) (4/4)
23 7 17 24 18 38 39 52 41 30 26 46 35 1 2 3 4 21 Min[H] 7 18 38 24 17 23 21 39 41 26 46 30 52 35
21
Delete a node (1/2) Key idea to delete a node Decreasing a key
Extract minimum node of H
22
Decreasing a key FIB-HEAP-DECREASE-KEY (H , x, k)
3 18 38 FIB-HEAP-DECREASE-KEY (H , x, k) 1 if k > key[x] then error “new key is greater than currently key” 3 key[x] ← k 4 y ← p[x] 5 if y != NIL and key[x] < key[y] then CUT(H,x,y) CASCADING-CUT(H, y) 8 if key[x] < key[min[H]] then min[H] ← x 24 17 23 21 39 41 26 46 30 52 35 FIB-HEAP-DECREASE-KEY (H , 46, 15) 15 3 18 38 24 17 23 21 39 41 26 30 52 35
23
Delete a node; DECREASE-KEY(H, 35, 5)
Min[H] Min[H] 30 7 38 18 24 23 15 17 26 41 21 39 52 35 15 5 7 18 38 24 17 23 21 39 41 26 30 52 Min[H] Min[H] 15 5 26 24 7 18 38 15 5 26 7 18 38 17 23 21 39 41 24 17 23 21 39 41 30 52 30 52
24
Delete a node; DELETE(H, 35)
Min[H] 30 7 38 18 24 23 15 17 26 41 21 39 52 35 CALL DELETE(H, 35) 1. DECREASE-KEY(H,35, 2. EXTRACT-MIN(H) DECREASE-KEY (H, Min[H] Min[H] 15 26 24 7 18 38 15 26 24 7 18 38 17 23 21 39 41 17 23 21 39 41 30 52 30 52 EEXTRACT-MIN (H)
25
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.