Towards Ultimate Binary Heaps Jyrki Katajainen 1 , 2 Amr Elmasry 3 1 - - PowerPoint PPT Presentation

towards ultimate binary heaps
SMART_READER_LITE
LIVE PREVIEW

Towards Ultimate Binary Heaps Jyrki Katajainen 1 , 2 Amr Elmasry 3 1 - - PowerPoint PPT Presentation

Towards Ultimate Binary Heaps Jyrki Katajainen 1 , 2 Amr Elmasry 3 1 University of Copenhagen 2 Jyrki Katajainen and Company 3 Alexandria University c Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (1) Binary heaps 0


slide-1
SLIDE 1

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (1)

Towards Ultimate Binary Heaps

Jyrki Katajainen1,2 Amr Elmasry3

1 University of Copenhagen 2 Jyrki Katajainen and Company 3 Alexandria University

slide-2
SLIDE 2

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (2)

Binary heaps

2 3 4 6 7 5 1 1 2 3 6 5

8 10 80

4

75 12 75 46 8 26 10 75 46 12 75 80

n = 8 a

7

26

left-child(i)

return 2i + 1

right-child(i)

return 2i + 2

parent(i)

return ⌊(i − 1)/2⌋

construct()

for (i = parent(n − 1); i ≥ 0; −−i)

sift-down(i) minimum()

return a0

insert(x)

an = x

sift-up(n)

n += 1

extract-min() min = a0

n −= 1 a0 = an

sift-down(0)

return min

slide-3
SLIDE 3

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (3)

Model of computation

Available

  • An infinite array a suitable for storing elements
  • O(1) other memory locations for storing elements
  • O(1) variables (counters, indices, bit strings of length ⌈lg(1+n)⌉)

2 3 4 6 7 5 1

a

workspace

n = 8

Requirement

  • If the data structure stores n elements, these elements must be

kept in the first n locations of a.

slide-4
SLIDE 4

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (4)

2014: The 50th anniversary of binary heaps

[Williams 1964] Operation Worst-case time # element comparisons

minimum

O(1)

insert

O(lg n) lg n + O(1)

extract-min

O(lg n) 2 lg n + O(1) [Gonnet & Munro 1986] Operation # element comparisons

insert

lg lg n ± O(1) sufficient and necessary

extract-min

lg n + log∗ n ± O(1) sufficient and necessary Assumptions

  • 1. Single heap
  • 2. Perfectly heap ordered before and after each operation
  • 3. Operations are memoryless.
slide-5
SLIDE 5

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (5)

Multi-ary heaps

8 10

2 3

75 75 80

1 2 3 4 6 7 5 1

8 12 10 75 46 26 75 80

n = 8 a

4

46

k = 4

5 6 7

12 26

first-child(i)

return k × i + 1

parent(i)

return ⌊(i − 1)/k⌋

[Johnson 1975] Operation Worst-case time

minimum

O(1)

insert

O(logk n)

extract-min

O(k logk n) n: # elements k: arity

slide-6
SLIDE 6

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (6)

Open—but solved later (see the last slide)

Operation Worst-case time # element comparisons

minimum

O(1)

insert

O(1) O(1)

extract-min

O(lg n) lg n + O(1) in-place as binary heaps Sorting Operation Worst-case time # element comparisons

minimum

O(1)

insert

O(1) O(1)

extract-min

Ω(lg n) lg n − O(1) necessary n: # elements

slide-7
SLIDE 7

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (7)

Related work

[Carlsson, Munro & Poblete 1988] queue of pennants

extract-min: 3 lg n + log∗ n element comparisons

[Elmasry, Jensen & Katajainen 2008] multipartite priority queue O(n) extra words [Edelkamp, Elmasry & Katajainen 2012] engineered weak heaps n + O(w) extra bits n: # elements w: word size in bits

slide-8
SLIDE 8

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (8)

Our main result

The objective can be achieved in the amortized sense

n0 ℓ ≤ lg2(n0)/4 n = n0 + ℓ + r r ≤ n0 ℓ elements: sizes: r components: buffer partial heap backlog a0a1 . . . an−1

What is special?

partial heap

heap-order violations

partial heap buffer

heap-order violations

backlog

some large elements kept outside

slide-9
SLIDE 9

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (9)

Insert

n0 ℓ ≤ lg2(n0)/4 n = n0 + ℓ + r r ≤ n0 ℓ elements: sizes: r components: buffer partial heap backlog a0a1 . . . an−1

insert(x)

an ← an0+ℓ an0+ℓ ← x if (ℓ = 0) k ← ⌈lg(n0)/2⌉ k-ary-heap-insert(a, n0, ℓ, an0+ℓ) ℓ++ n++ if (ℓ = k2)

bulk-insert(a, n, n0, ℓ)

slide-10
SLIDE 10

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (10)

Bulk-insert

I part II part ℓ ≤ lg2(n0)/4

Use Floyd’s heap-construction algorithm in the red area: call

sift-down at each node

I part: O(ℓ) nodes; O(ℓ) total work as for Floyd’s algorithm II part: O(lg(n0)) sift-down calls ⇒ O(lg2(n0)) total work ∴ amortized O(1) work per insert

slide-11
SLIDE 11

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (11)

Extract-min

n0 ℓ ≤ lg2(n0)/4 n = n0 + ℓ + r r ≤ n0 ℓ elements: sizes: r components: buffer partial heap backlog a0a1 . . . an−1

Minimum in the buffer

k-ary-heap-extract-min(a, n0, ℓ) an0+ℓ ← an ℓ-- n--

Minimum in the heap

extract-root(a, n, n0, ℓ)

n--

extract-root(a, n, n0, ℓ)

if (n0 + ℓ = n)

rebuild-structure(a, n, n0, ℓ)

i ← 0 while (i ≤ parent(n0 − 1)) if (aleft-child(i) < aright-child(i)) ai ← aleft-child(i) i ← left-child(i) else ai ← aright-child(i) i ← right-child(i) ai ← an−1

slide-12
SLIDE 12

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (12)

Deamortization of insertions

The objective can almost be achieved in the worst case Operation Worst-case time # element comparisons

minimum

O(1) O(1)

insert

O(1) O(1)

extract-min

O(lg n) lg n + log∗ n + O(1)

n0 ℓ ≤ lg2(n0)/4 n = n0 + ℓ + m m ≤ lg2(n0 + ℓ)/4 ℓ elements: sizes: components: binary heap buffer1 buffer2 m a0a1 . . . an−1

Role of the buffers

  • buffer2 takes care of the in-

serted elements

  • buffer1 is incrementally sub-

merged into the heap by per- forming a constant amount

  • f work per insert
slide-13
SLIDE 13

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (13)

Online sorting

Operation Worst-case time # element comparisons

construct

O(n) O(n)

extract-min

O(lg n) lg n + O(1) → Read the details from the paper [Elmasry 2003] O(n) extra bits n: # elements

slide-14
SLIDE 14

c

Performance Engineering Laboratory

ARCO meeting at SDU on 5 April, 2013 (14)

Note added on 15 April, 2013

Stefan Edelkamp came with the missing link that helped us to solve the problem. We are in the process of writing the paper “Optimal in-place heaps”. The new construction shows that the both lower bounds proved by Gonnet & Munro can be bypassed.