Amortized Analysis and Splay Trees: Inge Li Gørtz
Amortized Analysis and Splay Trees: Inge Li Gørtz
Inge Li Gørtz
CLRS Chapter 17
• Amortized analysis
• Multipop-stack
• Dynamic tables
• Splay trees
Dynamic tables
3
Dynamic tables
• First attempt.
• Insert:
• Too expensive.
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
• Space: Θ(n).
5
Amortized Analysis
• Amortized analysis.
• Methods.
• Potential method
Summation (Aggregate) method
• Summation.
• All elements in the array that is beyond the middle have 2 credits.
• 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.
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.
• 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!).
• Push: O(1).
• MultiPop: O(k).
• Push: 2
• 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
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).
• 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) =
• 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) =
• 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) =
• 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) =
💰 💰 💰 💰
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) =
• 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) =
• 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) =
• 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) =
💰
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) =
• 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:
• Worst case time for insertion, deletion and search is O(n). Amortised time
per operation O(log n).
y x
right
x y
c a
left
a b b c
• Splay(x): do following rotations until x is the root. Let p(x) be the parent of x.
• 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
• 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
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
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
• 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.
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
• Rank of a node.
• rank(v)
= blg size(v)c
• Potential function.
X X
= rank(v) = blg size(v)c
v v
• Proof.
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
• Actual cost: 1
• Change in potential:
a b b c
• Actual cost: 2
• Change in potential:
w x w z
d d
x w
a c a b c d
b c a b
zig-zag at x