self adjusting data structuresgerth/aa11/slides/selfadjusting.pdf · searching and sorting, volume...

7
SelfAdjusting Data Structures 1

Upload: others

Post on 15-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Self Adjusting Data Structuresgerth/aa11/slides/selfadjusting.pdf · Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973] →lfleftist heaps

Self‐Adjusting Data Structures

1

Page 2: Self Adjusting Data Structuresgerth/aa11/slides/selfadjusting.pdf · Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973] →lfleftist heaps

Self‐Adjusting Data Structures

t7 42 3591

Search(2), Search(2), Search(2) , Search(5), Search(5), Search(5)ve‐to‐fron

t

Lists[D.D. Sleator, R.E. Tarjan, Amortized Efficiency of List Update Rules, Proc. 16th Annual ACM Symposium on 

2 75 3914mov

[ , j , ff y f p , y pTheory of Computing, 488‐492, 1984]

Dictionaries[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Binary Search Trees, Journal of the ACM, 32(3): 652‐686, 1985]→ splay trees→ splay trees

Priority Queues[C.A. Crane, Linear lists and priority queues as balanced binary trees, PhD thesis, Stanford University, 1972][D.E. Knuth. Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973]

l f h→ leftist heaps

[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Heaps, SIAM Journal of Computing, 15(1): 52‐69, 1986]→ skew heaps

[C Okasaki Alternatives to Two Classic Data Structures Symposium on Computer Science Education 162‐

2

[C. Okasaki, Alternatives to Two Classic Data Structures, Symposium on Computer Science Education, 162165, 2005]→maxiphobix heaps   

Okasaki:  maxiphobix heaps are an alternative to leftist heaps ... but without the “magic”

Page 3: Self Adjusting Data Structuresgerth/aa11/slides/selfadjusting.pdf · Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973] →lfleftist heaps

MakeHeap FindMin Insert Meld DeleteMinHeaps (via Binary Heap‐Ordered Trees)

5

2

3

22

3

[C.A. Crane, Linear lists and priority queues as balanced binary 

MakeHeap, FindMin, Insert, Meld, DeleteMinMeld                             Cut root + Meld

= =4 6

8

97

1

1 1 1

2

1

Leftist Heaps

trees, PhD thesis, Stanford University, 1972][D.E. Knuth. Searching and Sorting, volume 3 of The Art of Computer Programming,Addison‐Wesley, 1973]

8

1011

12

1

12

12

23

3

Meld (          ,           )   =  Each node distance to empty leafInv Distance right child ≤ left child

13 13 4

7

9

2

2

42

3

4

1321

2

7

2

22

3

M i h bi H

Inv. Distance right child  ≤ left child⇒ rightmost path ≤ ⎡log n+1⎤ nodes

9

131

1

11 94

1

1

Time O(log n)

=  Meld (         ,           )

Meld (               ,             ) 

Maxiphobic Heaps

T

yx x

[C. Okasaki, Alternatives to Two Classic Data Structures,Symposium on Computer Science Education, 

162‐165, 2005]( , )

3

T3

T1 T2 Ti Tj Tklargest size two smallest

Max size n→ ⅔nTime O(log3/2 n)

x < y

Page 4: Self Adjusting Data Structuresgerth/aa11/slides/selfadjusting.pdf · Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973] →lfleftist heaps

[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Heaps, SIAM Journal of Computing, 15(1): 52‐69, 1986]

Skew Heaps2

Meld Cut root + Meld

= =

Heap ordered binary tree with no balance informationMakeHeap, FindMin, Insert, Meld, DeleteMin

5 3

4 6

8

10

97

12

Meld                             Cut root  Meld

2

Meld = merge rightmost paths + swap all siblings on merge path10

Meld (          ,           )   =   4

7

4

13 7

2v heavy if |Tv| > |Tp(v)|/2, otherwise light

⇒ any path ≤ log n light nodes

9

13

9 Potential  Φ = # heavy right children in tree

O(log n) amortized MeldHeavy right child on merge path before meld → replaced by light child

l l d f h d

4

⇒ 1 potential released for heavy node⇒ amortized cost 2∙ # light children on rightmost paths before meld

Page 5: Self Adjusting Data Structuresgerth/aa11/slides/selfadjusting.pdf · Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973] →lfleftist heaps

[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Heaps, SIAM Journal of Computing, 15(1): 52‐69, 1986]

Skew Heaps – O(1) time MeldMeld = Bottom‐up merg of rightmost paths + swap all siblings on merge path

1 1

Meld (          ,                  )   =                           

4

24

13 2

1

5 3 4

2

6

3=

7

9

7

9

8

11

6 11 5

8

13

Φ = # heavy right children in tree + 2 ∙ # light children on minor & major path

O(1) amortized MeldHeavy right child on merge path before meld → replaced by light child⇒ 1 potential releasedLight nodes disappear from major paths (but might→ heavy) ⇒≥ 1 potential released

and become a heavy or light right children on major path⇒ potential increase by ≤ 44 5

5

and       become a heavy  or light right children on major path⇒ potential increase by ≤ 4

O(log n) amortized DeleteMinCutting root⇒ 2 new minor paths, i.e. ≤ 2∙log n new light children on minor & major paths

4 5

Page 6: Self Adjusting Data Structuresgerth/aa11/slides/selfadjusting.pdf · Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973] →lfleftist heaps

[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Binary Search Trees, Journal of the ACM, 32(3): 652‐686, 1985]

Splay TreesBinary search tree with no balance informationsplay(x) = rotate x to root (zig/zag, zig‐zig/zag‐zag, zig‐zag/zag‐zig)

i i zig zagy

xC

x

yA

zigy

x

y

z

zig‐zigz

D

x

Ay

x

x

z

zig‐zagz

D y

root

Search (splay), Insert (splay predecessor+new root), Delete (splay+cut root+join), 

BA CB

BA

C

DC

B

CB

ADCBA

Join (splay max, link), Split (splay+unlink)v

u xzag‐zagi i insert

y

Fz

A

Ez

C

zig‐zigu

vD

yx u

vD

y

insert

6DC

xB

A

BA

C

FE

D

BA

zC

FE

Page 7: Self Adjusting Data Structuresgerth/aa11/slides/selfadjusting.pdf · Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973] →lfleftist heaps

[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Binary Search Trees, Journal of the ACM, 32(3): 652‐686, 1985]

Splay Trees

The access bounds of splay trees are amortizedThe access bounds of splay trees are amortized

(1) O(log n) ( ) l(2) Static optimal(3) Static finger optimal(4) Working set optimal (proof requires dynamic change of weight)( ) g p (p q y g g )

Static optimality:   Φ =       v log |Tv|Σ

7