Four different data structures, each one best in a different setting. Simple Heap Balanced Heap...

51
A balanced binary tree, with items in the leaves in left-right order by decreasing key slope. The tree is a tournament on items by current key. Simple heap (details)

Transcript of Four different data structures, each one best in a different setting. Simple Heap Balanced Heap...

Page 1: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

A balanced binary tree, with items in the leavesin left-right order by decreasing key slope.

The tree is a tournament on items by current key.

Simple heap (details)

Page 2: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c ea

a c

a

Page 3: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Simple kinetic heaps (Cont)

Add swap times at internal nodes

Page 4: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c ea

a c

a

3

6 6.8

4.4

Page 5: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Simple kinetic heaps (Cont)

Add minimum future swap time

Page 6: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c ea

a c

a

3

3 6.8

3

6

3

4.4

6.8

Page 7: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Simple kinetic heaps (Cont)

When we do a find-min if the current time moves forward, we act as follows:

Locate all nodes whose winner changes.

The paths from the root to those nodes form a subtree.

Update this subtree

Page 8: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c ea

a c

a

3

3 6.8

3

6

3

4.4

6.8

Page 9: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c ea

a c

a

3

3 6.8

3

6

3

4.4

6.8

Page 10: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c eb

a c

a

3 6.8

3

6

3

4.4

6.8

Page 11: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c eb

b

2020

3

c

a 34.4

6.8 6.8

Page 12: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c eb

b

2020

3

c

b 55

6.8 6.8

Page 13: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c eb

b c

b

20

5

20

3

5

6.86.8

Page 14: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c eb

b c

b

20

5

20

3

5

6.86.8

Page 15: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c eb

b e

e

2020

3

6.8

205

Page 16: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c eb

b e

e

2020

3

206.3

6.8

Page 17: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Simple kinetic heaps – insert & delete

Use the regular mechanism of (say) red-black trees

Page 18: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

d c eb

b e

e

f

2020

3

5

6.8

20

Page 19: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b d

c eb

b e

e

20 20

3

5

6.8

20

f

f

f -20

Page 20: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b d

c eb

f e

e

-10

3

6.9

6.8

f

f

f -20

Page 21: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Simple kinetic heaps (analysis)

Find-min takes O(1) time if we do not advance the current time.

Advancing the current time can be expensive on the worst-case

Amortization:

Assign O(log n) potential to each node whose swap time is in the future.

This potential pays for advancing the time.

So find-min takes O(1) amortized time.

Insert and delete take O(log2n) amortized time

Page 22: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

How do we make this a parametric heap ?(Overmars-Van Leeuwen)

Page 23: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

c e

d

Page 24: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Looks like we use non linear space ?

Each node keeps the chain of segments that are on its envelope but not on its parent envelope.

This makes the space linear.

Page 25: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

c e

d

Page 26: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Analysis

Using search trees to represent the envelopes sorted by slopes then:

Find-min takes O(log n) time

Insert and delete take O(log2 n) time: You split and concatenate O(log n) such envelopes.

Page 27: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Balanced kinetic heaps

Def: The level of a line is the depth of the highest node where it is stored.

Plan: first we’ll do only deletions faster and then we’ll add insertions.

Our starting point is the previous data structure.

Page 28: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Deletion-only data structure

The level of a line is the depth of the highest node where it is stored.

Page 29: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

c e

d

d

Page 30: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Deletion-only data structure

What happens when we delete a line ?

Page 31: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

c e

d

d

Page 32: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

e

d

d

Page 33: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

e

d

d

Page 34: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

a

b

e

d

c

a b

e

d

d

Page 35: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Balanced kinetic heaps

When we delete a line, lines only decrease their level.

Page 36: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.
Page 37: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.
Page 38: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Analysis

Key idea: Implement the envelopes using finger search trees

How much time it takes to delete an element ?

O(log n) + log (# lines get on the replacement piece)

+ log (# lines get off the replacement piece)

If in all nodes # lines get on/off the replacement piece < log2n we are ok --- the delete takes O(log n loglog n) time

However it could be that # lines get on/off the replacement piece > log2n

Page 39: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Analysis (Cont)

If a line gets on the replacement piece it changes its level

Since the total number of level changes is nlog(n)

The number of chains of length > log2n is at most n/log(n)

So the total work in moving long chain is O(n) and can be charged to the initialization phase.

Page 40: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

A better deletions only data structure

O(n) init. time and then O(log n loglog n) per delete.O(1) find-min if we are kinetic.

Now we add insert.

Summary so far

Page 41: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Balanced kinetic heaps (finalle)

Balanced heaps (step 2):

Use a well known reduction by Bently and Saxe to add insertions

. . . . . . . . . . . . . .

Maintain a binary counter of deletion only data structures

Page 42: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Balanced kinetic heaps

Delete : O(log n loglog n)

. . . . . . . . . . . . . .

Insert : O(log n)

Find-min : O(log n) (kinetic)

Page 43: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Based one Fibonacci heaps with lazy linking.

We link only when it is safe

Fibonacci heap (highlights)

We get

O(log2 n) for delete

O(log n) for find-min and insert

O(log n) for decrease-key

Page 44: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Fibonacci heaps (Fredman & Tarjan 84)

Want to do decrease-key(x,h,) faster than delete+insert.

( in O(1) time.)

Page 45: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

55

6

2

6

1

85

95

10

Heap ordered (Fibonacci) trees

Page 46: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Linking

Operations are defined via a basic operation, called linking, of Fibonacci trees:

Produce a Fk from two Fk-1, keep heap order.

1

65

8

2

95

10

4

116

9

5

96

10

Page 47: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Basic idea: delay the linking until they are safe

S1S0

Sr

Regular heap

Page 48: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Linking

The basic operation is inserting a tree of rank r into Sr,

making the appropriate linking if needed.

Insert(i) : Insert a new node containing i into S0

This may cascade, but we have one tree less every time we do the linking

Decrease-key(i, a’.b’) : Decrease the key of i in the tree containing it making cascading cuts, link the resulting trees back into the structure.

Page 49: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Analysis

Similar to the analysis of F-heaps

Multiply the potential by log n

Page 50: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Incremental heap (highlights)

Assumptions insertion of i: ai = 1, increase key of i: ai := ai + 1

Group items by key slope, each group is an ordinary Fibonacci heap

Move items among groups as key slopes change(“displaced” items because entire subtrees are moved)

We get

find-max, insert : O(1)The kth increase-key : O(log min{k,n})delete : O(log n)

Page 51: Four different data structures, each one best in a different setting. Simple Heap Balanced Heap Fibonacci Heap Incremental Heap Our results.

Open problems

Experiments: What is the best structure in practice?

Better bounds: kinetic heaps in ordinary F-heap bounds?

parametric heaps in O(log n) amortized time per operation?

Simple kinetic heaps via splay trees?