the post-order heap nick harvey & kevin zatloukal

24
The Post-Order Heap Nick Harvey & Kevin Zatloukal

Post on 21-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

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

The Post-Order Heap

Nick Harvey &

Kevin Zatloukal

Page 2: The Post-Order Heap Nick Harvey & Kevin Zatloukal

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

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

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

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

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

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

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

Insert

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

Page 10: The Post-Order Heap Nick Harvey & Kevin Zatloukal

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

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

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

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

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

? ?

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

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

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

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

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

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

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

Pseudocode

height & ancestry bookkeeping

enumerate roots

find children

Page 23: The Post-Order Heap Nick Harvey & Kevin Zatloukal

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

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!