the post-order heap nick harvey & kevin zatloukal
Post on 21-Dec-2015
220 views
TRANSCRIPT
![Page 1: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/1.jpg)
The Post-Order Heap
Nick Harvey &
Kevin Zatloukal
![Page 2: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/2.jpg)
Binary Heap Review
Simple priority queue “Heap-ordered”: parent key < children keys Insert, DeleteMin require O(log n) time
3
12 9
15 13 18 20
5
711
2312
1
…
keys
![Page 3: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/3.jpg)
Binary Heap Review
“Implicit”: stored in array with no pointers Array index given by level-order traversal
2
4 5
8 9 10 11
3
76
1312
1
…
array indices
2 31 4 5 76 8 9 1011 1312 …
Implicittree structure:
Array:
![Page 4: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/4.jpg)
The Heapify Operation
Combine 2 trees of height h + new root Fix up heap-ordering Produces tree of height h+1 Time: O(h)
+
fix order
![Page 5: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/5.jpg)
The BuildHeap Operation
Batch insert of n elements Initially n/2 trees of height 0 Repeatedly Heapify to get:
– n/4 trees of height 1– n/8 trees of height 2 …
Total time: )(2log
1
nOnin
i
i
![Page 6: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/6.jpg)
The FUN begins…
BuildHeap: O(n) batch insert of n elements. Cannot FindMin efficiently until done.
Is O(1) Insert possible?
Yes:–The Post-Order Heap
Is there a simple variant of binary heap with O(1) Insert?
Yes:–Fibonacci Heaps–Implicit Binomial Queues (Carlsson et al. ’88)
![Page 7: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/7.jpg)
FindMin during BuildHeap
During BuildHeap, a FindMin is slow– Many small trees must search for min
But BuildHeap can heapify in other orders– “Children before parents” sufficient
Idea: Change order to reduce # trees!
min?
![Page 8: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/8.jpg)
A Better Ordering
BuildHeap: new node either parent or leaf– Parent is good reduces # trees by 1
Idea: add parent whenever possible
7
63
1 2 4 5
10
98 1211
– This is a Post-Order insertion order
O(log n) trees
![Page 9: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/9.jpg)
Insert
Insert:– Run BuildHeap incrementally– Insertion order = post-order
![Page 10: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/10.jpg)
FindMin & DeleteMin
FindMin– Enumerate the log n roots– O(log n) time (assuming enumeration is easy)
DeleteMin: like binary heap– Find min, swap it with last element– Heapify to fix up heap order– O(log n) time
![Page 11: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/11.jpg)
Insert Analysis
Potential function = Sum of tree heights Insert leaf
– 0 comparisons, unchanged Insert parent at height h
– h comparisons for heapify– Decrease in = 2(h-1) - h = h - 2 Amortized cost = 2
Total time: O(1) amortized
![Page 12: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/12.jpg)
Fun with Sums
Write BuildHeap sum as
How can we evaluate this exactly?
k
i
iki1
2
Expand and Contract! No Don,
Potential Functions!
![Page 13: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/13.jpg)
Fun with Sums
How can we evaluate exactly?
k
i
iki1
2
Consider BuildHeap with n = 2k+1 - 1– The 2k leafs pay $0– The 2k - 1 internal nodes pay $2
Consider BuildHeap with n = 2k+1 - 1– The 2k leafs pay €0– The 2k - 1 internal nodes pay €2– Potential at end is k (height of final heap)
Total = 2k+1 - 2 - k
![Page 14: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/14.jpg)
Back to Post-Order Heaps
Problem: Array sparse not implicit Why? Tree-array map = level-order Insertion order = post-order Solution: use post-order for tree-array map
Tree:
Array:
![Page 15: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/15.jpg)
? ?
x
Navigating with Post-Order
For node x at height h– Right child = x - 1– Left child = x - 1 - (size of right subtree)
= x - 2h
Must know height to navigate!
7
63
1 2 4 5
10
98 1211
where are the children?
![Page 16: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/16.jpg)
Height of New Nodes
Where is node x+1?
x+1
1) x is left child x+1 is leaf height 0
2) x is right child x+1 is parent height h+1
x+1 height h
x
Must know ancestry to update height!
![Page 17: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/17.jpg)
z
y
Ancestry String
For node x at height h:– Bits below h are 0– ith bit describes x’s ancestor at height i
0 = left child, 1 = right child
height hx
0 0 0
h zero bits
1
x = right child
0
y = left child
……
…
![Page 18: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/18.jpg)
Updating Ancestry String
One ancestry string, for last node in heap Must update after Insert
x+1
yx
height h
no ancestors x = left child
0 0 0 0 ……x’s ancestry string:
all left children y = right child
0 0 0 1 ……x+1’s ancestry string:
![Page 19: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/19.jpg)
x+1
Updating Ancestry String
One ancestry string, for last node in heap Must update after Insert
x
height h
no ancestors x = right child
0 0 0 1 ……x’s ancestry string:
no ancestors
0 0 0 0 ……x+1’s ancestry string:
![Page 20: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/20.jpg)
Updating Ancestry String
One ancestry string, for last node in heap Must update after Insert
– O(1) time
Must update after DeleteMin– Easy to do in O(log n) time
![Page 21: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/21.jpg)
Recap: Problems & Solutions
Main idea: Do Insert by incremental BuildHeap
Problem
Too many trees
Solution
Post-order insertion order
Not implicit Post-order tree-array map
Need height to navigate Maintain height for last node
Updating height Maintain ancestry string
![Page 22: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/22.jpg)
Pseudocode
height & ancestry bookkeeping
enumerate roots
find children
![Page 23: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/23.jpg)
Experiments
Ordering
Comparisons Time Comparisons Time
Increasing 1.00 2.31 2.00 7.59
Random 2.38 5.66 1.87 7.53
Decreasing 17.32 27.09 1.00 5.00
Binary Heap Post-Order Heap
Post-Order Heap– improves worst-case– reduces # comparisons– larger constant factor due to bookkeeping
1 million Inserts (300 times):
![Page 24: The Post-Order Heap Nick Harvey & Kevin Zatloukal](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d565503460f94a33fd9/html5/thumbnails/24.jpg)
Summary
Potential function analysis of BuildHeap
Post-Order Heaps:– Implicit: O(1) extra space– Insert: O(1) amortized time– DeleteMin: O(log n) time
Simple, practical and FUN!