Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nick Harvey & Kevin Zatloukal

Similar presentations


Presentation on theme: "Nick Harvey & Kevin Zatloukal"— Presentation transcript:

1 Nick Harvey & Kevin Zatloukal
The Post-Order Heap Nick Harvey & Kevin Zatloukal

2 Binary Heap Review … Simple priority queue
keys 1 3 5 12 9 11 7 15 13 18 20 12 23 Simple priority queue “Heap-ordered”: parent key < children keys Insert, DeleteMin require O(log n) time

3 Binary Heap Review … … “Implicit”: stored in array with no pointers
array indices 1 Implicit tree structure: 2 3 4 5 6 7 8 9 10 11 12 13 Array: 1 2 3 4 5 6 7 8 9 10 11 12 13 “Implicit”: stored in array with no pointers Array index given by level-order traversal

4 The Heapify Operation +   Combine 2 trees of height h + new root
fix order + Combine 2 trees of height h + new root Fix up heap-ordering Produces tree of height h+1 Time: O(h)

5 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:

6 The FUN begins… BuildHeap: O(n) batch insert of n elements. Cannot FindMin efficiently until done. Is O(1) Insert possible? Yes: Fibonacci Heaps Implicit Binomial Queues (Carlsson et al. ’88) Is there a simple variant of binary heap with O(1) Insert? Yes: The Post-Order Heap

7 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!

8 A Better Ordering BuildHeap: new node either parent or leaf
O(log n) trees 7 3 6 10 9 8 12 11 1 2 4 5 BuildHeap: new node either parent or leaf Parent is good  reduces # trees by 1 Idea: add parent whenever possible This is a Post-Order insertion order

9 Insert Insert: Run BuildHeap incrementally
Insertion order = post-order

10 FindMin & DeleteMin FindMin DeleteMin: like binary heap
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

11 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

12 No Don, Potential Functions!
Fun with Sums Write BuildHeap sum as How can we evaluate this exactly? Expand and Contract! No Don, Potential Functions!

13 Fun with Sums How can we evaluate exactly?
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) Consider BuildHeap with n = 2k+1 - 1 The 2k leafs pay $0 The 2k - 1 internal nodes pay $2 Total = 2k k

14 Back to Post-Order Heaps
Tree: Array: Problem: Array sparse  not implicit Why? Tree-array map = level-order Insertion order = post-order Solution: use post-order for tree-array map

15 Navigating with Post-Order
where are the children? x ? ? 7 3 6 10 9 8 12 11 1 2 4 5 For node x at height h Right child = x - 1 Left child = x (size of right subtree) = x - 2h Must know height to navigate!

16 Height of New Nodes Where is node x+1?
height h x+1 x Where is node 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 Must know ancestry to update height!

17 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 z h zero bits 1 x = right child y = left child y x height h

18 Updating Ancestry String
One ancestry string, for last node in heap Must update after Insert no ancestors x = left child x’s ancestry string: x+1 y x height h all left children y = right child 1 x+1’s ancestry string:

19 Updating Ancestry String
One ancestry string, for last node in heap Must update after Insert no ancestors x = right child 1 x’s ancestry string: height h x+1 x no ancestors x+1’s ancestry string:

20 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

21 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

22 height & ancestry bookkeeping
Pseudocode enumerate roots find children height & ancestry bookkeeping

23 Experiments 1 million Inserts (300 times): Post-Order Heap
Ordering 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 5.00 Binary Heap Post-Order Heap Post-Order Heap improves worst-case reduces # comparisons larger constant factor due to bookkeeping

24 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!


Download ppt "Nick Harvey & Kevin Zatloukal"

Similar presentations


Ads by Google