/
Self Adjusted Data Structures Self Adjusted Data Structures

Self Adjusted Data Structures - PowerPoint Presentation

danika-pritchard
danika-pritchard . @danika-pritchard
Follow
404 views
Uploaded On 2017-06-19

Self Adjusted Data Structures - PPT Presentation

Selfadjusting Structures Consider the following AVL Tree 44 17 78 32 50 88 48 62 Selfadjusting Structures Consider the following AVL Tree 44 17 78 32 50 88 48 62 Suppose we want to search for the following sequence of ID: 560976

splay zig tree rank zig splay rank tree operation analysis time amortized case ranki parent root grandparent log node

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Self Adjusted Data Structures" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Self Adjusted Data StructuresSlide2

Self-adjusting Structures

Consider the following AVL Tree

44

17

78

32

50

88

48

62Slide3

Self-adjusting Structures

Consider the following AVL Tree

44

17

78

32

50

88

48

62

Suppose we want to search for the following sequence of

elements: 48, 48, 48, 48, 50, 50, 50, 50, 50.Slide4

Self-adjusting Structures

Consider the following AVL Tree

44

17

78

32

50

88

48

62

Suppose we want to search for the following sequence of

elements: 48, 48, 48, 48, 50, 50, 50, 50, 50.

In this case,

is this a good structure?Slide5

Self-adjusting Structures

So far we have seen:

BST: binary search trees

Worst-case running time per operation = O(N)Worst case average running time = O(N)Think about inserting a sorted item listAVL tree:Worst-case running time per operation = O(

logN)Worst case average running time = O(logN)Does not adapt to

skew distributionsSlide6

Self-adjusting Structures

T

he structure is

updated after each operationConsider a binary search tree. If a sequence of insertions produces a leaf in the level O(n), a sequence of m searches to this element will represent a time complexity of O(mn) Use an auto-adjusting strucutureSlide7

Self

adjustable

listsMove to FrontWhenever an element is accessed it is moved to the front of the listTranspositionWhenever an element x is accessed, we switch

x with its predecessorFrequency counterThe list is always sorted by decreasing order of frequenciesSlide8

EXAMPLE

Self

adjustable

listsSlide9

Self-adjusting Structures

Algoritmos

AdmissíveisUm método é dito admissível se no i-ésimo acesso ele move o

elemento acessado ki posições para frenteClasse de algoritmos que engloba todos os métodos apresentados.Slide10

Análise do Move

to

Front

Teorema. Seja H um método admissível e seja s uma sequência de m acessos. Então CustoMF

(S) <= 2CustoH(s) –m,Aonde CustoMF

(S) e CustoH(s)

são, respectivamente, os custos de MF e H para processar uma sequência s de requisições Slide11

Análise do Move

to

Front

Prova. Empregamos o método da função potencialDi :o numero de inversões da lista mantida pelo MF em relação a lista do algoritmo H após o i-

ésimo acesso. Lista de H: a b c f e d Lista de MF: b a f d e c Inversões: (

b,a) , (f,c), (d,e), (d,c)  D

i =4Temos quec’i

= ci +D

i –Di-1Slide12

Análise do Move

to

Front

Prova. Elemento x é acessado pelo MF. x: k-ésimo elemento da lista de H

x: j-ésimo elemento da lista de MFp: número de elementos que precedem x na lista de MF e sucedem x na lista de HQuando o MF coloca x na primeira posição,

j-1-p inversões são criadas e p são destruídasQuando H move o elemento x ei operações para frente, ei inversões são destruídas e nenhuma é criadaSlide13

Análise do Move

to

Front

Prova. Da análise anteriorc’i

= j +(j-1-p) – (p + ei) = 2(j-1-p) - ei + 1Temos que j-1-p <=k-1 já que o número de inversões criadas é limitado pela posição de x na lista de H. Logo,

c’i <= 2(k-1) - ei + 1

Somando c’i

Segue que

 Slide14

Bad

Sequences

for Transposition and Frequency CounterTranspositionWe insert n elements a

1,…,an and then we access the element an n times. Transposition pays O(n2) while MF pays O(n)Frequency Counter

List with elements (an,…,a1). We access an n times, an-1 (n-1) times and so on. FC does not reorganize the list.FC pays O(n

3) while MF pays O(n2)Slide15

Further Reading

Randomization avoids malicious adversariesSlide16

Self-adjusting Structures

Splay

Trees (Tarjan and Sleator 1985)Binary

search tree.Every accessed node is brought to

the rootAdapt to the access probability distribution

Slide17

Splay tree

s: Basic Idea

Try to make the worst-case situation occur less frequently.

In a Binary search tree, the worst case situation can occur with every operation. (while inserting a sorted item list). In a splay tree, when a worst-case situation occurs for an operation:The tree is re-structured (during or after the operation), so that the subsequent operations do not cause the worst-case situation to occur again. Slide18

Splay tree

s: Basic idea

The basic idea of splay tree is: After a node is accessed, it is pushed to the root by a series of AVL tree-like operations (rotations).For most applications, when a node is accessed, it is likely that it will be accessed again in the near future (principle of

locality). Slide19

Splay tree:

Basic Idea

By pushing the accessed node to the root the tree: If the accessed node is accessed again, the future accesses will be much less costly. During the push to the root operation, the tree might be more balanced than the previous tree. Accesses to other nodes can also be less costly. Slide20

A

first

attemptA simple ideaWhen a node k is accessed, push it towards the root by the following algorithm: On the path from k to root: Do a singe rotation between node k’s parent and node k itself. Slide21

F

k

4

E

D

A

k

1

B

B

k

5

k

3

k

2

access path

Accessing node k

1

A

first attemptSlide22

F

k

4

E

D

B

k

5

k

3

After rotation between k

2

and k

1

A

k

2

C

k

1

A

first attemptSlide23

F

k

4

E

B

k

5

k

1

After rotation between k

3

and k

1

A

k

2

D

C

k

3

A

first attemptSlide24

F

k

1

B

k

5

After rotation between k

4

and k

1

A

k

2

D

C

E

k

4

k

3

A

first attemptSlide25

k

1

B

A

k

2

D

C

F

k

3

k

5

E

k

4

k

1

is now root

But k

3

is nearly as deep as k

1

was.

An access to k

3

will push some other node nearly as deep as k

3

is.

So, this method does not work

...

A

first

attemptSlide26

Splaying

The method will push the accessed node to the root.

With this pushing operation it will also balance the tree somewhat.

So that further operations on the new will be less costly compared to operations that would be done on the original tree.A deep tree will be splayed:Will be less deep, more wide. Slide27

Splaying - algorithm

Assume we access a node.

We will splay along the path from access node to the root.

At every splay step: We will selectively rotate the tree. Selective operation will depend on the structure of the tree around the node in which rotation will be performedSlide28

Implementing Splay(x, S)

Do the following operations until x is root.

ZIG: If x has a parent but no grandparent, then rotate(x).

ZIG-ZIG: If x has a parent y and a grandparent, and if both x and y are either both left children or both right children.ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, y is a left child and the other is a right child.Slide29

Implementing Splay(x, S)

Do the following operations until x is root.

ZIG: If x has a parent but no grandparent, then rotate(x).

ZIG-ZIG: If x has a parent y and a grandparent, and if both x and y are either both left children or both right children.ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, y is a left child and the other is a right child.

A

B

x

C

ySlide30

Implementing Splay(x, S)

Do the following operations until x is root.

ZIG: If x has a parent but no grandparent, then rotate(x).

ZIG-ZIG: If x has a parent y and a grandparent, and if both x and y are either both left children or both right children.ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, y is a left child and the other is a right child.

A

B

x

C

y

C

B

y

A

x

ZIG(x)

rootSlide31

Implementing Splay(x, S)

Do the following operations until x is root.

ZIG: If x has a parent but no grandparent, then rotate(x).

ZIG-ZIG: If x has a parent y and a grandparent, and if both x and y are either both left children or both right children.ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, y is a left child and the other is a right child.

A

B

y

C

x

C

B

x

A

y

ZAG(x)

rootSlide32

Implementing Splay(x, S)

Do the following operations until x is root.

ZIG: If x has a parent but no grandparent, then rotate(x).

ZIG-ZIG: If x has a parent y and a grandparent, and if both x and y are either both left children or both right children.ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, y is a left child and the other is a right child.

A

B

x

C

y

D

zSlide33

Implementing Splay(x, S)

Do the following operations until x is root.

ZIG: If x has a parent but no grandparent, then rotate(x).

ZIG-ZIG: If x has a parent y and a grandparent, and if both x and y are either both left children or both right children.ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, y is a left child and the other is a right child.

ZIG-ZIG

A

B

x

C

y

D

z

D

C

z

B

y

A

xSlide34

Implementing Splay(x, S)

Do the following operations until x is root.

ZIG: If x has a parent but no grandparent, then rotate(x).

ZIG-ZIG: If x has a parent y and a grandparent, and if both x and y are either both left children or both right children.ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, y is a left child and the other is a right child.

B

C

x

D

y

z

ASlide35

Implementing Splay(x, S)

Do the following operations until x is root.

ZIG: If x has a parent but no grandparent, then rotate(x).

ZIG-ZIG: If x has a parent y and a grandparent, and if both x and y are either both left children or both right children.ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, y is a left child and the other is a right child.

B

C

x

D

y

z

D

C

y

x

A

B

A

z

ZIG-ZAGSlide36

Splay Example

Apply Splay(1, S) to tree S:

10

9

8

7

6

5

4

3

2

1

ZIG-ZIGSlide37

10

9

8

7

6

5

4

ZIG-ZIG

1

2

3

Splay Example

Apply Splay(1, S) to tree S:Slide38

10

9

8

7

6

ZIG-ZIG

1

2

3

4

5

Splay Example

Apply Splay(1, S) to tree S:Slide39

10

9

8

ZIG-ZIG

1

6

2

3

4

5

7

Splay Example

Apply Splay(1, S) to tree S:Slide40

10

ZIG

1

8

9

6

7

2

3

4

5

Splay Example

Apply Splay(1, S) to tree S:Slide41

1

10

8

9

6

7

2

3

4

5

Splay Example

Apply Splay(1, S) to tree S:Slide42

Apply Splay(2, S) to tree S:

1

10

8

9

6

7

2

3

4

5

2

8

4

6

3

10

1

9

5

7

Splay(2)Slide43

Splay Tree Analysis

Definitions.

Let S(x) denote subtree of S rooted at x.

|S| = number of nodes in tree S.

(S) = rank =  log |S| .

(x) =  (S(x)).

2

8

4

6

3

10

1

9

5

7

|S| = 10

(2) = 3

(8) = 3

(4) = 2

(6) = 1

(5) = 0

S(8)Slide44

Splay Tree Analysis

Define

the

potential functionAssociate a positive weight to each node v:

w(v)W(v)=  w(y), y belongs

to a subtree rooted at v

Rank(v) = log W(v)Slide45

Splay Tree Analysis

Define

the

potential functionAssociate a positive weight to each

node v: w(v)W(v)=  w(y), y belongs

to a subtree rooted at

vRank(v) = log W(v)The tree potential

is:

v

 rank(v)Slide46

Upper

bound

for the amortized time ofa complete splay operation

To estimate the time of

a splay operation we are going

to use the number of

rotations Slide47

Upper

bound

for the amortized time ofa complete splay operation

To estimate the time of

a splay operation we use

the number of rotations

Lemma: The amortized time for a complete splay operation

of a node x in a tree of root r is at most 1 + 3[

rank(r) – rank(x)] where rank(x) is the

rank of x before the splay and

rank(r) is the rank of r after

the splay. Slide48

Upper

bound

for the amortized time ofa complete splay operation

Proof: The amortized cost a is given by a=t + 

after – before

t : number of rotations executed in the splayingSlide49

Upper

bound

for the amortized time ofa complete splay operation

Proof: The amortized cost a is given by a=t + 

after – before

a = o1 + o2 + o3 + ... + o

k oi : amortized cost of the i-th operation during the splay ( zig or zig-zig or zig-zag)Slide50

Upper

bound

for the amortized time ofa complete splay operation

Proof:

i : potential function

after i-th operation

ranki : rank

after i-th operation

oi = ti

+ i –

i-1 Slide51

Splay Tree Analysis

Operations

Case 1: zig( zag)

Case 2: zig-zig (zag-zag) Case 3: zig-zag (zag-zig)Slide52

Splay Tree Analysis

Case 1: Only one rotation (zig)

r

x

rootSlide53

Splay Tree Analysis

Case 1: Only one rotation (zig)

A

B

x

C

r

C

B

r

A

x

ZIG(x)

w.l.o.g.

r

x

rootSlide54

Splay Tree Analysis

Case 1: Only one rotation (zig)

A

B

x

C

r

C

B

r

A

x

ZIG(x)

w.l.o.g.

After the operation only rank(x) and rank(r) change

r

x

rootSlide55

Splay Tree Analysis

Since

potential is the sum of every rank:

i - i-1 = ranki(r) +

ranki(x) – ranki-1(r) – rank

i-1(x) ti

= 1 (time of one rotation)

Amort. Complexity: o

i = 1 + ranki(r) + ranki(x) – rank

i-1(r) – ranki-1(x) Slide56

Splay Tree Analysis

Amort. Complexity:

o

i = 1 + ranki(r) + rank

i(x) – ranki-1(r) – ranki-1(x)

A

B

x

C

r

C

B

r

A

x

ZIG(x)Slide57

Splay Tree Analysis

Amort

.

Complexity: oi

= 1 + ranki(r) + ranki(x) – rank

i-1(r) – ranki-1(x)

A

B

x

C

r

C

B

r

A

x

ZIG(x)

rank

i-1

(r)

rank

i

(r

)

rank

i

(

x)

rank

i-1

(x

)Slide58

Splay Tree Analysis

Amort

.

Complexity: oi

<= 1 + ranki(x) – ranki-1

(x)

A

B

x

C

r

C

B

r

A

x

ZIG(x)

rank

i-1

(r)

rank

i

(r

)

rank

i

(

x)

rank

i-1

(x

)Slide59

Splay Tree Analysis

Amort

.

Complexity: oi

<= 1 + 3[ ranki(x) – ranki-1

(x) ]

A

B

q

C

r

C

B

r

A

q

ZIG(x)

rank

i-1

(r)

rank

i

(r

)

rank

i

(

x)

rank

i-1

(x

)Slide60

Splay Tree Analysis

Case 2: Zig-Zig

ZIG-ZIG

A

B

x

C

y

D

z

D

C

z

B

y

A

xSlide61

Splay Tree Analysis

Case 2: Zig-Zig

ZIG-ZIG

A

B

x

C

y

D

z

D

C

z

B

y

A

x

o

i

= 2 +

rank

i

(

x) +

rank

i

(

y)+

rank

i

(

z) –

rank

i-1

(x

) –

rank

i-1

(y

) –

rank

i-1

(z

)

Slide62

Splay Tree Analysis

Case 2:

Zig-Zig

ZIG-ZIG

A

B

x

C

y

D

z

D

C

z

B

y

A

x

o

i

= 2 +

rank

i

(x) +

rank

i

(

y)+

rank

i

(z

) –

rank

i-1

(x

) –

rank

i-1

(y

) –

rank

i-1

(z

)

rank

i-1

(z

) =

rank

i

(

x)Slide63

Splay Tree Analysis

Case 2: Zig-Zig

ZIG-ZIG

A

B

x

C

y

D

z

D

C

z

B

y

A

x

o

i

= 2 +

rank

i

(

y)+

rank

i

(

z) –

rank

i-1

(x

) –

rank

i-1

(y

) Slide64

Splay Tree Analysis

Case 2:

Zig-Zig

ZIG-ZIG

A

B

x

C

y

D

z

D

C

z

B

y

A

x

o

i

= 2 +

rank

i

(

y)+

rank

i

(

z) –

rank

i-1

(x

) –

rank

i-1

(

y)

rank

i

(

x)

rank

i

(

y)

rank

i

-1

(y

)

rank

i-1

(

x)Slide65

Splay Tree Analysis

Case 2: Zig-Zig

ZIG-ZIG

A

B

x

C

y

D

z

D

C

z

B

y

A

x

o

i

2 +

rank

i

(

x)+

rank

i

(

z) –

2rank

i-1

(

x) Slide66

Splay Tree Analysis

Devemos mostrar que

2 +

ranki (x)+rank

i (z) – 2ranki-1 (x) <=3(

ranki(x)-ranki-1(x))

Ou seja, -2

 ranki-1 (x)+rank

i (z) – 2ranki

(x)Devemos estudar o comportamento da função ranki-1

(x)+ranki (z) – 2ranki

(x)Slide67

Splay Tree Analysis

Temos que

rank

i-1 (x)+

ranki (z) – 2ranki (x)

= log( wi-1(x)/

wi (x))+log(

wi(z)/ wi

(x) )Examinando a árvore percebemos que

wi-1(x)/ w

i (x)+ wi

(z)/ wi(x)<=1

Portanto, log( wi-1(x)/

wi (x))+log( wi

(z)/ wi(x) ) é maior ou igual a

min log a + log b sujeito a a+b<=1. Segue da convexidade da função log que o mínimo é atingido em a=b=1/2. Portanto, o mínimo é maior ou igual a -2.

Slide68

Splay Tree Analysis

Case 2: Zig-Zig

ZIG-ZIG

A

B

x

C

y

D

z

D

C

z

B

y

A

x

o

i

3[

rank

i

(

x) –

rank

i-1

(

x) ] Slide69

Splay Tree Analysis

Case

3:

Zig-Zag ( analysis similar to the case 2)

o

i

 3[ ranki

(x) – ranki-1

(x) ] Slide70

Splay Tree Analysis

Putting

the three cases together and telescoping

a =

o1

+ o2

+ ... + ok 

3[rank(r)-rank

(x)]+1Slide71

Splay Tree Analysis

For proving different types of results we must set the weights accordinglySlide72

Theorem

.

The cost of m accesses is

O(m log n +n log n), where n is the

number of items in

the tree

Splay Tree AnalysisSlide73

Theorem

.

The cost of m accesses is

O(m log n+n logn), where n

is the number of

items in the tree

Splay Tree Analysis

Proof

:

Define every weight as 1/n.

Then, the amortized

cost is at

most 3 log n + 1. The potential

variation is at most

n log n Thus, by

summing over all accesses we

conclude that the cost

is

at

most

3m

log n + n log n

+mSlide74

Static

Optimality

Theorem

Theorem: Let q(i) be the number

of accesses to item i. If every item is

accessed at least once, then total

cost is at most Slide75

Static Optimality Theorem

Proof

.

Assign a weight of q(i)/m to item i. Then,

rank(r)=0 and rank(i)  log(q(i)/m)

Thus, 3[rank(r) – rank(i)] +1

 3log(m/q(i)) + 1 In addition, || 

Thus, the overall cost isSlide76

Static Optimality Theorem

Theorem

: The

cost

of an optimal static

binary search tree is Slide77

Static Finger Theorem

Theorem

:

Let

i,...,n be the items

in the splay tree. Let the

sequence of accesses be 1,...,m. If f is

a fixed item, the total access time is Slide78

Static Finger Theorem

Proof

.

Assign a weight 1/(|i –f|+1)2

to item i. Then, rank(r)= O(1). rank

(ij)=O( log( |ij – f +1|)

Since the weight of

every item is at

least 1/n 2, then ||

 n log nSlide79

Working

Set

Theorem

Theorem

: Let i,...,n be the

items in the splay tree. Let

the sequence of accesses be 1,...,m. Let

i(j) be the item accessed at the

j-th access and let let

t(j) be the number of distinct

itens accessed since the previous access to

i(j). Then,Slide80

Dynamic Optimality Conjecture

Conjecture

Consider

any sequence of successful

accesses on an n-node search tree.

Let A be any algorithm that carries

out each access by traversing the path from

the root to the node containing the

accessed item, at a cost of one plus

the depth of the node containing the

item, and that between accesses performs

an arbitrary number of rotations anywhere in

the tree, at a cost of one per

rotation. Then the total time to perform all

the accesses by splaying is no more than

O(n) plus a constant times the time required by

the algorithm.Slide81

Dynamic

Optimality

Conjecture: best attempt

Tango Trees: O(log log n) competitive ratioDynamic

optimality - almost.E. Demaine, D. Harmon, J. Iacono, and M. Patrascu. In Foundations of Computer Science (FOCS), 2004 Slide82

Insertion and Deletion

Most of the theorems hold !Slide83

Paris Kanellakis

Theory and Practice Award

Award 1999

Splay Tree Data Structure

Daniel D.K. Sleator and Robert E. Tarjan

Citation For their invention of the widely-used "Splay Tree" data structure.Slide84

Amortized running time

Definition

: For a series of

M consecutive operations: If the total running time is O(M*f(N)), we say that the amortized running time (per operation) is O(f(N)). Using this definition:

A splay tree has O(logN) amortized cost (running time) per operation.Slide85

Amortized running time

Ordinary Complexity

: determination of worst case complexity. Examines each operation individuallySlide86

Amortized running time

Ordinary Complexity

: determination of worst case complexity. Examines each operation individually

Amortized Complexity: analyses the average complexity of each operation.Slide87

Amortized

Analysis

:

Physics ApproachIt can be seen as an analogy to the concept of potential energySlide88

It can be seen as an analogy to the concept of potential energy

Potential function

 which maps any configuration E of the structure into a real number (E), called potential of E.

Amortized Analysis: Physics ApproachSlide89

It can be seen as an analogy to the concept of potential energy

Potential function

 which maps any configuration E of the structure into a real number (E), called potential of E.

It can be used to to limit the costs of the operations to be done in the future

Amortized Analysis:

Physics ApproachSlide90

Amortized

cost

of an operation

a = t + (E’) - (E)Slide91

Amortized

cost

of an operation

a = t + (E’) - (E)

Real time

of the operation

Structure configuration

after the operation

Structure configuration

before the operationSlide92

Amortized

cost of

a sequence of operations

 t i =  (a

i - i + i-1

)

i=1

i=1

m

m

a = t + (E’) - (E)Slide93

Amortized

cost of

a sequence of operations

 t i =  (a

i - i + i-1

)

i=1

i=1

m

m

=

0 - m +  a

i

i=1

m

By telescopic

a = t + (E’) - (E)Slide94

Amortized

cost of a

sequence of M operations

 t i =  (a

i - i + i-1

)

i=1

i=1

m

m

=

0 - m +  a

i

i=1

m

By telescopic

The total real time does not depend on the intermediary potential

a = t + (E’) - (E)Slide95

Amortized

cost of

a sequence of operations

 Ti =  (a

i - i + i-1

)

i=1

i=1

If the final potential is greater or equal than the initial, then the amortized complexity can be used as an upper bound to estimate the total real time.Slide96

Amortized running time

Definition

: For a series of

M consecutive operations: If the total running time is O(M*f(N)), we say that the amortized running time (per operation) is O(f(N)). Using this definition:

A splay tree has O(logN) amortized cost (running time) per operation.Slide97

Implementing Splay(x, S)

A

B

x

C

y

D

zSlide98

Implementing Splay(x, S)

A

B

x

C

y

D

z

D

C

z

y

A

B

xSlide99

Implementing Splay(x, S)

A

B

x

C

y

D

z

D

C

z

y

A

B

xSlide100

Implementing Splay(x, S)

A

B

x

C

y

D

z

D

C

z

y

A

B

x

D

C

z

B

y

A

x