1.15k likes | 1.32k Views
Fundamental Algorithms Chapter 2: Advanced Heaps. Christian Scheideler WS 2012. Contents. A heap implements a priority queue . We will consider the following heaps : Binomial heap Fibonacci heap Radix heap. Priority Queue. 8. 5. 15. 12. 7. 3. Priority Queue. insert(10).
E N D
Fundamental AlgorithmsChapter 2: Advanced Heaps Christian Scheideler WS 2012 Chapter 2
Contents A heapimplements a priorityqueue. We will considerthefollowingheaps: • Binomial heap • Fibonacci heap • Radix heap Chapter 2
Priority Queue 8 5 15 12 7 3 Chapter 2
Priority Queue insert(10) 8 5 15 12 7 3 10 Chapter 2
Priority Queue min() outputs 3 (minimal element) 8 5 15 12 7 3 10 Chapter 2
Priority Queue deleteMin() 8 5 15 12 7 3 10 Chapter 2
Priority Queue decreaseKey(12,3) 8 5 15 12 9 7 10 Chapter 2
Priority Queue delete(8) 8 5 15 9 7 10 Chapter 2
Priority Queue merge(Q,Q’) 8 10 & 7 5 15 9 9 8 5 10 7 Chapter 2
Priority Queue M:set ofelements in priorityqueue Every element eidentified by key(e). Operations: • M.build({e1,…,en}): M:={e1,…,en} • M.insert(e: Element): M:=M∪{e} • M.min: outputs e∈M with minimal key(e) • M.deleteMin: likeM.min, but additionallyM:=M∖{e}, for that ewith minimal key(e) Chapter 2
Extended Priority Queue Additional operations: • M.delete(e: Element): M:=M∖{e} • M.decreaseKey(e:Element, ): key(e):=key(e)- • M.merge(M´): M:=M∪M´ Note: in deleteanddecreaseKeywehavedirectaccesstothecorrespondingelementandtherefore do not havetosearchfor it. Chapter 2
Priority Queue • Priority Queue based on unsorted list: • build({e1,…,en}): time O(n) • insert(e): O(1) • min, deleteMin: O(n) • Priority Queue based on sorted array: • build({e1,…,en}): time O(n log n) (needed for sorting) • insert(e): O(n) (rearrange elements in array) • min, deleteMin: O(1) Better structure needed than list or array! Chapter 2
Binary Heap Idee:use binary tree instead of list Preserve two invariants: • Form invariant:completebinary tree up to lowest level • Heap invariant: e1 key(e1)≤min{key(e2),key(e3)} e2 e3 Chapter 2
Binary Heap Example: 4 Heap invariant 8 5 11 9 12 18 15 17 Form invariant Chapter 2
Binary Heap Representation of binary tree via array: e1 e2 e3 e4 e5 e6 e7 e8 e9 e1 e2 e3 e3 e4 e5 e6 e7 e8 e9 Chapter 2
Binary Heap Representation of binary tree via array: • H: Array [1..N]of Element (N>n) • Children of e in H[i]: in H[2i], H[2i+1] • Form invariant:H[1],…,H[n] occupied • Heap invariant: key(H[i])≤min{key(H[2i]),key(H[2i+1])} e1 e2 e3 e3 e4 e5 e6 e7 e8 e9 Chapter 2
Binary Heap Representation of binary tree via array: insert(e): • Form invariant:n:=n+1; H[n]:=e • Heap invariant:switch ewith parent until key(H[⌊k/2⌋])≤key(e)for e in H[k]ore in H[1] e1 e2 e3 e3 e4 e5 e6 e7 e8 e9 e10 Chapter 2
Insert Operation Procedure insert(e: Element)n:=n+1; H[n]:=esiftUp(n) Procedure siftUp(i: Integer)while i>1 and key(H[⌊i/2⌋])>key(H[i]) doH[i] ↔H[⌊i/2⌋]i:=⌊i/2⌋ Runtime:O(log n) Chapter 2
Insert Operation - Correctness 3 3 5 8 5 8 10 9 12 15 10 9 12 15 11 18 11 18 4 Invariant: H[k]is minimal w.r.t. subtree of H[k] : nodes that may violate invariant Chapter 2
Insert Operation - Correctness 3 3 5 8 5 8 10 9 12 15 10 4 12 15 11 18 4 11 18 9 Invariant: H[k]is minimal w.r.t. subtree of H[k] : nodes that may violate invariant Chapter 2
Insert Operation - Correctness 3 3 5 8 4 8 10 4 12 15 10 5 12 15 11 18 9 11 18 9 Invariant: H[k]is minimal w.r.t. subtree of H[k] : nodes that may violate invariant Chapter 2
Insert Operation - Correctness 3 3 4 8 4 8 10 5 12 15 10 5 12 15 11 18 9 11 18 9 Invariant: H[k]is minimal w.r.t. subtree of H[k] : nodes that may violate invariant Chapter 2
Binary Heap deleteMin: • Form invariant:H[1]:=H[n]; n:=n-1 • Heap invariant: start with e in H[1].Switch e with the child with minimum key until H[k]≤min{H[2k],H[2k+1]}for the current position kof eor eis in a leaf e1 e2 e3 e3 e4 e5 e6 e7 e8 e9 Chapter 2
Binary Heap Runtime: O(log n) Procedure deleteMin(): Elemente:=H[1]; H[1]:=H[n]; n:=n-1siftDown(1)return e Procedure siftDown(i: Integer)while 2i<=n do if 2i+1>n then m:=2i // m: pos. of the minimum child else if key(H[2i])<key(H[2i+1]) then m:=2i else m:=2i+1 if key(H[i])<=key(H[m]) then return // heap inv. holdsH[i] ↔H[m]; i:=m Chapter 2
deleteMin Operation - Correctness 3 18 5 8 5 8 10 9 12 15 10 9 12 15 11 18 11 Invariant: H[k]is minimal w.r.t. subtree of H[k] : nodes that may violate invariant Chapter 2
deleteMin Operation - Correctness 18 5 5 8 18 8 10 9 12 15 10 9 12 15 11 11 Invariant: H[k]is minimal w.r.t. subtree of H[k] : nodes that may violate invariant Chapter 2
deleteMin Operation - Correctness 5 5 18 8 9 8 10 9 12 15 10 18 12 15 11 11 Invariant: H[k]is minimal w.r.t. subtree of H[k] : nodes that may violate invariant Chapter 2
Binary Heap Build({e1,…,en}): • Naive implementation: via ninsert(e) operations. Runtime O(n log n) • Better implementation:Set H[i]:=eifor all i.Call siftDown(i)for i=⌊n/2⌋ down to 1.Runtime (with k=⌈log n⌉):O(1≤l<k 2l(k-l)) = O(2kj≥1j/2j) = O(n) Chapter 2
Binary Heap Set H[i]:=eifor all i.Call siftDown(i)for i=⌊n/2⌋ down to 1. invariant violated Invariant: ∀j>i:H[j] min w.r.t. subtree of H[j] Chapter 2
Binary Heap Runtime: • Build({e1,…,en}): O(n) • Insert(e): O(log n) • Min: O(1) • deleteMin: O(log n) Chapter 2
Extended Priority Queue Additional Operations: • M.delete(e: Element): M:=M∖{e} • M.decreaseKey(e:Element, ): key(e):=key(e)- • M.merge(M´): M:=M∪M´ • delete and decreaseKey can be implemented with runtime O(log n)in binary heap (if position of eis known) • merge is expensive ((n)time)! Chapter 2
Binomial Heap Binomial heap is based on binomial trees Binomial tree has to satisfy: • Form invariant(r: rank): • Heap invariant(key(Parent)≤key(Children)) r=0 r=1 r →r+1 r r Chapter 2
Binomial Heap Examples of correct Binomial trees: r=1 r=2 r=3 r=0 4 4 4 4 10 10 10 7 6 6 11 8 8 20 24 Chapter 2
Binomial Heap Properties of Binomial trees: • 2rnodes • maximum degreer(at root) • root deleted: Binomial tree decomposes into Binomial trees of rank 0tor-1 r=0 r=1 r →r+1 r r number of neighbors Chapter 2
Binomial Heap Example for decomposition into Binomial trees of rank 0tor-1 rank 3 4 ranks 2 1 0 7 6 10 11 8 20 24 Chapter 2
Binomial Heap Binomial Heap: • linked list of Binomial trees • for each rank at most 1 Binomial tree • pointer to root with minimal key 2 4 5 7 9 numbers: ranks Chapter 2
Binomial Heap Example of a correct Binomial heap: min-pointer 9 3 4 15 10 7 6 11 8 20 Binomial tree ofrank r=1 24 Chapter 2
Binomial Heap Merge of Binomial heaps H1and H2: like binary addition 2 H1 5 7 ranks 10100100 2 3 H2 5 + 101100 4 6 11010000 7 Chapter 2
Example of Merge Operation 2 5 6 7 2 3 3 5 4 H1 numbers denotethe ranks Make sure that the heap invariant is preservedby the merging! H2 outcome Chapter 2
Binomial Heap Runtime of merge operation: O(log n) because • thelargest rank in a Binomialheapwithnelementsatmostlog n, and • atmostoneBinomialtreeisallowedforeach rank value Bi:Binomial tree of rank i • insert(e): mergeexistingheapwithB0 (containingonlyelemente) • min: use min-pointer, time O(1) • deleteMin: let the min-pointer pointtotherootofBi.Deleting theroot in Biresults in BinomialtreesB0,…,Bi-1.These have to be merged back into Binomial heap. Thus, theinsertanddeleteMinoperationscanbereducedtothemergeoperation, whichimplies a runtimeofO(log n). Chapter 2
Exampleof Insert Operation Insert(8): 9 3 4 8 & 15 10 6 11 Chapter 2
Exampleof Insert Operation Outcomeof Insert(8): 3 15 4 8 10 9 6 11 Chapter 2
Binomial Heap • decreaseKey(e,): perform siftUp operation in Binomial tree starting with e, update min-pointer. Time O(log n) • delete(e): (min-pointer does not point to e) set key(e):= -and perform siftUpoperation starting with e until eis in a root; then continue like in deleteMinwhenremovinge (but withoutupdatingthe min-pointer!).Time O(log n) Chapter 2
ExampleofdecreaseKey decreaseKey(24,19): 9 3 4 15 10 7 6 11 8 20 24 Chapter 2
ExampleofdecreaseKey OutcomeofdecreaseKey(24,19): 9 3 4 15 10 5 6 11 8 7 20 Chapter 2
Fibonacci Heap • Based on Binomial trees, but it allows lazy merge and lazy delete. • Lazy merge: no merging of Binomial trees of the same rank during merge, only concatenation of the two lists • Lazy delete: creates incomplete Binomial trees Chapter 2
Fibonacci Heap Tree in a Binomial heap: 4 7 6 10 8 20 11 24 Chapter 2
Fibonacci Heap Tree in a Fibonacci heap: 4 Every parent only knows first and last child of list 7 6 10 Every childknows itsparent 8 20 11 List of siblings 24 Chapter 2
Fibonacci Heap Every node remembers: • Element stored in the node • Parent node • Left and right sibling in sibling list • First and last child in its list of children • Its rank (number of children) Thus, every node stores ≤5 pointers. Chapter 2
2 2 5 5 7 7 2 2 3 3 5 5 Fibonacci Heap Lazy merge of results in min & min Chapter 2