0% found this document useful (0 votes)
64 views54 pages

Amortized Analysis and Splay Trees: Inge Li Gørtz

This document discusses amortized analysis and splay trees. It provides examples of using amortized analysis to analyze the runtime of dynamic tables that double in size when full and halve in size when a quarter full. The three methods of amortized analysis discussed are the summation method, accounting method, and potential method. The potential method is used to show that the amortized cost of insertions and deletions in a dynamic table is O(1) by defining a potential function based on the table size and number of elements.

Uploaded by

Simon Syleves
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views54 pages

Amortized Analysis and Splay Trees: Inge Li Gørtz

This document discusses amortized analysis and splay trees. It provides examples of using amortized analysis to analyze the runtime of dynamic tables that double in size when full and halve in size when a quarter full. The three methods of amortized analysis discussed are the summation method, accounting method, and potential method. The potential method is used to show that the amortized cost of insertions and deletions in a dynamic table is O(1) by defining a potential function based on the table size and number of elements.

Uploaded by

Simon Syleves
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Amortized Analysis and Splay Trees

Inge Li Gørtz

CLRS Chapter 17

Jeff Erickson notes


Today

• Amortized analysis

• Multipop-stack

• Dynamic tables

• Splay trees
Dynamic tables

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

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

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

• Can insert and delete elements at the end.

3
Dynamic tables

• First attempt.

• Insert:

• 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.

4
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.

3 5

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:

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

• Space: Θ(n).

5
Amortized Analysis

• Amortized analysis.

• Average running time per operation over a worst-case sequence of


operations.

• Methods.

• Summation (aggregate) method

• Accounting (tax) method

• Potential method
Summation (Aggregate) method

• Summation.

• Determine total cost.

• Amortized cost = total cost/#operations.

• Analysis of doubling strategy (without deletions):

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

• Amortized cost per insert: Θ(1).


Dynamic Tables: Accounting Method

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

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

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

• 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.

• Amortized cost per operation: 3.


💰 💰 💰 💰
💰 💰 💰 💰

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 x x x x x x x x x x x x
Accounting method

• Accounting/taxation.

• Some types of operations are overcharged (taxed).

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

• 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!).

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

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


Example: Stack with MultiPop

• Stack with MultiPop.

• Push(e): push element e onto stack.

• MultiPop(k): pop top k elements from the stack

• Worst case: Implement via linked list or array.

• Push: O(1).

• MultiPop: O(k).

• Can prove amortized cost per operation: 2.


Stack: Aggregate Analysis

• Amortized analysis. Sequence of n Push and MultiPop operations.

• Each object popped at most once for each time it is pushed.

• #pops on non-empty stack ≤ #Push operations ≤ n.

• Total time O(n).

• Amortized cost per operation: 2n/n = 2.


Stack: Accounting Method

• Amortized analysis. Sequence of n Push and MultiPop operations.

• Pay 2 credits for each Push.

• Keep 1 credit on each element on the stack.

• Amortized cost per operation:

• Push: 2

• MultiPop: 1 (to pay for pop on empty stack).


Dynamic tables with deletions

• Halving (first attempt). If the array is half full copy the elements to a new array
of half the size.

3 5 1 7 8 2 3 4 6

3 5 1 7 8 2 3 4

3 5 1 7 8 2 3 4 6

3 5 1 7 8 2 3 4

• Consequence. The array is always between 50% and 100% full. But risk to
use too much time (double or halve every time).

13
Dynamic tables

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

3 5 1 7 8

3 5 1 7

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

14
Potential method

• Potential method. Define a potential function for the data structure that is
initially zero and always non-negative.

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

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

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

• cî = ci + Φ(Di) − Φ(Di−1)

• Thus:

m m m m


cî =
∑ (ci + Φ(Di) − Φ(Di−1)) =

ci + Φ(Dm) − Φ(D0) ≥

ci
i i i i
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.

• 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.


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.

• 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

• 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

• 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 before and half full after:

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

• amortized cost = 1 + - 💰 = 0
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.

• 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 at least half full, but not full:


11, L = 16
n = 12,

💰 💰 💰 💰
x x x x x x x x x x x x 💰 💰 💰 💰

• amortized cost = 1 + 💰💰 = 3
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.

• 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 in full table and doubling


n = 9,
8, L = 16
8
x x x x x x x x
💰 💰 💰 💰
💰 💰 💰 💰
x x x x x x x x x

• amortized cost = 9 + - 💰💰 💰 💰
💰 💰 =3
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.

• 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.

• Deleting in a quarter full table and halving


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

• amortized cost = 3 + - 💰 💰 💰 =0
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.

• 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.

• Deleting when more than half full (still half full after):
n = 11,
12, L = 16

💰 💰 💰 💰
x x x x x x x x x x x x 💰 💰 💰 💰

• amortized cost = 1 + - 💰
💰
= -1
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.

• 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.

• Deleting when half full (not half full after):


n = 7,
8, L = 16

💰
x x x x x x x x

• amortized cost = 1 + 💰 =2
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.

• 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.

• Deleting in when less than half full (but still a quarter full after):

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

• amortized cost = 1 + 💰 = 2
Potential Method

• Summary:

1. Pick a potential function, Φ, that will work (art).

2. Use potential function to bound the amortized cost of the operations


you're interested in.

3. Bound Φ(D0) - Φ(Dfinal)

• Techniques to find potential functions: if the actual cost of an operation is


high, then decrease in potential due to this operation must be large, to keep
the amortized cost low.
Splay Trees
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.

• 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

• 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.

y x
right
x y
c a
left

a b b c

right rotation at x (and left rotation at y)


Splaying

• 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.

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

z z x

w x w z
d d
x w
a c a b c d

b c a b

zig-zag at x
Splaying

• 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.

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

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

z y x

y x z y
d a
x z
c a b c d b

a b c d

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


Splaying

zig-zag at x
z z x

w x w z
d d
x w
a c a b c d

b c a b

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


z y x

y x z y
d a
x z
c a b c d b

a b c d
Splay

• Example. Splay(1) 10

right roller-coaster at 1
Splay

• Example. Splay(1) 10

right roller-coaster at 1
Splay

• Example. Splay(1) 10

2 5

right roller-coaster at 1
Splay

• Example. Splay(1) 10

4 7

2 5

right roller-coaster at 1
Splay

• Example. Splay(1) 10

6 9

4 7

2 5

right roller-coaster at 1
Splay

• Example. Splay(1) 10

6 9

4 7

2 5

right rotation at 1
Splay

• Example. Splay(1) 1

10

6 9

4 7

2 5

right rotation at 1
Splay

• Example. Splay(3) 1

10

6 9

4 7

2 5

zig-zag at 3
Splay

• Example. Splay(3) 1

10

6 9

3 7

2 4

zig-zag at 3
Splay

• Example. Splay(3) 1

10

6 9

3 7

2 4

roller-coaster at 3
Splay

• Example. Splay(3) 1

10

3 7 9

2 4

roller-coaster at 3
Splay

• Example. Splay(3) 1

10

2 6

4 8

5 7 9

roller-coaster at 3
Splay

• Example. Splay(3) 1

10

2 6

4 8

5 7 9

zag-zig at 3
Splay

• Example. Splay(3)

1 10

2 6

4 8

5 7 9

zag-zig at 3
Splay Trees

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


search algorithm. Splay found node.

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

• 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 the predecessor v of Make v the parent of the


Find x and splay it Delete x
x and splay it root of the right subtree

x v
v

v
Deletion in Splay Trees

• Delete 6.
3

10
6

1 4 8

2 5 7 9

splay 6: zag-zig at 6
Deletion in Splay Trees

• Delete 6.
6

3 10

1 4 8

2 5 7 9

delete
splay 56
Deletion in Splay Trees
5
• Delete 6.
4

3 10

1 8

2 7 9

connect
Analysis of splay trees

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

• All costs bounded by splay.


Analysis of splay trees

• Rank of a node.

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

• rank(v)

= blg size(v)c

• Potential function.

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

• 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).
Splay Lemma Proof

• 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).

• Proof.

• Assume we have k rotations.

• Only last one can be a single rotation.

k k−1

∑(
cî ≤ ri(v) − ri−1(v)) + (1 + rk(v) − rk−1(v)) = 1 + rk(v) − r0(v)m = O(lg n)


i=0 i=1

where ri(v) is the rank of v after the ith rotation.


Rotation Lemma

• Proof of rotation lemma: Single rotation.

• Actual cost: 1

• Change in potential:

• Only x and y can change rank.

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

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


y x
right
x y
c a
left

a b b c

right rotation at x (and left rotation at y)


Rotation Lemma

• Proof of rotation lemma: zig-zag.

• Actual cost: 2

• Change in potential:

• Only x, w and z can change rank.

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

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


z z x

w x w z
d d
x w
a c a b c d

b c a b

zig-zag at x

You might also like