Today. Amortized Analysis and Splay Trees. Dynamic tables. Dynamic tables. Amortized analysis. Dynamic tables. Splay trees

Today • Amortized analysis • Dynamic tables Amortized Analysis and Splay Trees • Splay trees Inge Li Gørtz CLRS Chapter 17 Jeff Erickson notes D...
Author: Jocelyn Lane
9 downloads 0 Views 263KB Size
Today • Amortized analysis • Dynamic tables

Amortized Analysis and Splay Trees

• Splay trees

Inge Li Gørtz

CLRS Chapter 17

Jeff Erickson notes

Dynamic tables

Dynamic tables

• Problem. Have to assign size of table at initialization.

• First attempt.

• Goal. Only use space Θ(n) for an array with n elements.

• Insert:

• Applications. Stacks, queues, hash tables,….

• Create a new table of size n+1.

• Move all elements to the new table.

• Delete old table.

• Size of table = number of elements

• Too expensive.

• Have to copy all elements to a new array each time.

• Insertion of N elements takes time proportional to: 1 + 2 + ······ + n = Θ(n2).

• Goal. Ensure size of array does not change to often. 3

4

Dynamic tables

Dynamic tables

• Doubling. If the array is full (number of elements equal to size of array) copy the elements to a new array of double size.

• Halving. If the array is a quarter full copy the elements to a new array of half the size.

3

3 5 1 7 8

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

• Consequence. Insertion of n elements take time:

• Consequence. The array is always between 25% and 100% full.

• n + number of reinsertions = n + 1 + 2 + 4 + 8 + ···· + 2log n < 3n.

• Space: Θ(n). 5

6

Amortized Analysis

Summation (Aggregate) method

• Methods.

• Summation.

• Summation (aggregate) method

• Determine total cost.

• Accounting (tax) method

• Amortized cost = total cost/#operations.

• Potential method • Analysis of doubling strategy:

• Total cost: n + 1 + 2 + 4 + ... + 2log n = Θ(n).

• Amortized cost per insert: Θ(1).

Dynamic Tables: Accounting Method

Accounting method

• Analysis: Allocate 2 credits to each element when inserted.

• Accounting/taxation.

• All elements in the array that is beyond the middle have 2 credits.

• Some types of operations are overcharged (taxed).

• Table not full: insert costs 1, and we have 2 credits to save.

• Amortized cost of an operation is what we charge for an operation.

• table full, i.e., doubling: half of the elements have 2 credits each. Use these to pay for reinsertion of all in the new array.

• Credit allocated with elements in the data structure can be used to pay for later operations (only in analysis, not actually stored in data structure!).

• Amortized cost per operation: 3.

• Total credit must be non-negative at all times.

! ! ! ! ! ! ! !

x x x x x x x x x x x x

• => Total amortized cost an upper bound on the actual cost.

! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x

Potential method

Dynamic tables

• Potential functions.

• Doubling. If the table is full (number of elements equal to size of array) copy the elements to a new array of double size.

• Prepaid credit (potential) associated with the data structure (money in the bank).

• Ensure there is always enough “money in the bank” (positive potential).

• Amortized cost of an operation: actual cost plus change in potential.

• Halving. If the table is a quarter full copy the elements to a new array of half the size

• Potential function.

( 2n L if T at least half full • Φ(Di) =

L/2 n if T less than half full • L = current array size, n = number of elements in array.

• Inserting when less than half full and still less than half full after insertion:

n = 7, 6, L = 16 x x x x x x x

• amortized cost = 1 + - ! = 0

! !

Dynamic tables

Dynamic tables

• Doubling. If the table is full (number of elements equal to size of array) copy the elements to a new array of double size.

• Doubling. If the table is full (number of elements equal to size of array) copy the elements to a new array of double size.

• Halving. If the table is a quarter full copy the elements to a new array of half the size

• Halving. If the table is a quarter full copy the elements to a new array of half the size

• Potential function.

( 2n L if T at least half full • Φ(Di) =

L/2 n if T less than half full

• Potential function.

( 2n L if T at least half full • Φ(Di) =

L/2 n if T less than half full

• L = current array size, n = number of elements in array.

• L = current array size, n = number of elements in array.

• Inserting when less than half full before and half full after:

• Inserting when half full, but not full:

11, L = 16 n = 12,

n = 8, 7, L = 16 x x x x x x x x

x x x x x x x x x x x x

!

!

! !

!

!

! !

!

• amortized cost = 1 + - ! = 0

• amortized cost = 1 + !! = 3

Dynamic tables

Dynamic tables

• Doubling. If the table is full (number of elements equal to size of array) copy the elements to a new array of double size.

• Doubling. If the table is full (number of elements equal to size of array) copy the elements to a new array of double size.

• Halving. If the table is a quarter full copy the elements to a new array of half the size

• Halving. If the table is a quarter full copy the elements to a new array of half the size

• Potential function.

( 2n L if T at least half full • Φ(Di) =

L/2 n if T less than half full

• Potential function.

( 2n L if T at least half full • Φ(Di) =

L/2 n if T less than half full

• L = current array size, n = number of elements in array.

• Inserting in full table and doubling

n = 9, 8, L = 16 8

x x x x x x x x

• L = current array size, n = number of elements in array.

• Deleting in a quarter full table and halving

x x x x

! !

! ! ! !

x x x x x x x x x

• amortized cost = 9 + - !!

! ! ! !

=3

! ! ! !

! !

x x x

• amortized cost = 3 + - !

nn==4,3,LL==16 8

! !

=0

Splay Trees • Self-adjusting BST (Sleator-Tarjan 1983).

• Most frequently accessed nodes are close to the root.

• Tree reorganizes itself after each operation.

• After access to a node it is moved to the root by splay operation.

Splay Trees

• Worst case time for insertion, deletion and search is O(n). Amortised time per operation O(log n).

• Operations. Search, predecessor, sucessor, max, min, insert, delete, join.

Splaying

Splaying

• Splay(x): do following rotations until x is the root. Let y be the parent of x.

• Splay(x): do following rotations until x is the root. Let p(x) be the parent of x.

• right (or left): if x has no grandparent.

• right (or left): if x has no grandparent.

• zig-zag (or zag-zig): if one of x,p(x) is a left child and the other is a right child.

right c b

left

z

w

y

x a

z

x

y

x

w

d

x

a

w c

c b

right rotation at x (and left rotation at y)

z

d

a b

x

c

a

b

zig-zag at x

a

b

c

d

Splaying

Splaying zig-zag at x

• Splay(x): do following rotations until x is the root. Let y be the parent of x.

• right (or left): if x has no grandparent.

z w

• zig-zag (or zag-zig): if one of x,y is a left child and the other is a right child.

y

y

z

a

a

b

c

d

b

w c c

b

a

Splay

a

y

10

z

y

x

x

d

y a

a

c a

z

d b

c

d

b

• Example. Splay(1)

10

9

8

8

7

7

6

6

5

5

4

4

1

2

2

1

3

right roller-coaster at 1

z b c

9

3

d

c

x

Splay

• Example. Splay(1)

b

b

z

b c

right roller-coaster at x (and left roller-coaster at z)

z

right roller-coaster at x (and left roller-coaster at z)

a

c

w d

a

y

d

x

x

x

x

x

d

x

• roller-coaster: if x and p(x) are either both left children or both right children.

z

z

right roller-coaster at 1

d

Splay

Splay

• Example. Splay(1)

• Example. Splay(1)

10

10

9

9

8

8

7

1

6

6

1

4

4

2

2

5

7

5

3

3

right roller-coaster at 1

Splay

right roller-coaster at 1

Splay

• Example. Splay(1)

• Example. Splay(1)

10

10

1

1

8

6

4

2

8

9

6

7

5

3

4

2

9

7

5

3

right roller-coaster at 1

right rotation at 1

Splay

Splay

• Example. Splay(1)

• Example. Splay(3)

1

1

10

10

8

6

4

2

8

9

6

7

4

5

2

3

9

7

5

3

right rotation at 1

Splay

zig-zag at 3

Splay

• Example. Splay(3)

• Example. Splay(3)

1

1

10

10

8

6

3

2

8

9

6

7

3

4

2

5

9

7

4

5

zig-zag at 3

roller-coaster at 3

Splay

Splay

• Example. Splay(3)

• Example. Splay(3)

1

1

10

10

6

3

8

3

2

2

7

6

9

4

4

8

5

7

9

5

roller-coaster at 3

Splay

roller-coaster at 3

Splay

• Example. Splay(3)

• Example. Splay(3)

1

10

3

3

1

2

6

2

4

8

5

10

7

6

4

9

zag-zig at 3

8

5

7

9

zag-zig at 3

Splay Trees

Deletion in Splay Trees

• Search(x). Find node containing key x (or predecessor/successor) using usual search algorithm. Splay found node.

• Delete 6.

• Insert(x). Insert node containing key x using algorithm for binary search trees. Splay found node.

3

10 6 1

• Delete(x). Find node x, splay it and delete it. Tree now divided in two subtrees. Find node with largest key in left subtree, splay it and join it to the right subtree by making it the new root.

Find x and splay it

Find the predecessor v of x and splay it

Delete x

4

2

8

5

7

9

Make v the parent of the root of the right subtree

x

v

v

x v

splay 6: zag-zig at 6

Deletion in Splay Trees

Deletion in Splay Trees

• Delete 6.

• Delete 6.

5

6

4

3

1

10

4

2

8

5

delete splay 56

7

3

10

1

9

8

2

7

connect

9

Analysis of splay trees

Analysis of splay trees

• Amortized cost of a search, insert, or delete operation is O(log n).

• Rank of a node.

• size(v) = #nodes in subtree of v

• All costs bounded by splay.

= blg size(v)c • rank(v)

• Potential function.

=

X

rank(v) =

v

X v

blg size(v)c

• Rotation Lemma. The amortized cost of a single rotation at any node v is at most 1 + 3 rank’(v) - 3 rank(v), and the amortized cost of a double rotation at any node v is at most 3 rank’(v) - 3 rank(v).

• Splay Lemma. The amortized cost of a splay(v) is at most 1 + 3rank’(v) - 3 rank(v).

Rotation Lemma

Rotation Lemma

• Proof of rotation lemma: Single rotation.

• Proof of rotation lemma: zig-zag.

• Actual cost: 1

• Actual cost: 2

• Change in potential:

• Change in potential:

• Only x and y can change rank.

• Only x, w and z can change rank.

• Change in potential at most r’(x) - r(x).

• Change in potential at most 2r’(x) - 2r(x) - 2.

• Amortized cost ≤ 1 + r’(x) - r(x) ≤ 1 + 3r’(x) - 3r(x).

• Amortized cost: ≤ 2 + 2r’(x) - 2r(x) - 2 ≤ 2r’(x) - 2r(x) ≤ 3r’(x) - 3r(x). x

y right

x c a

b

z y

left

z

w

x

a

right rotation at x (and left rotation at y)

c

w

d

x b

x

w

a

c b

z

d

c

a

b

zig-zag at x

a

b

c

d