Professional Documents
Culture Documents
Amortized analysis.
■ Worst-case bound on sequence of operations.
■ Ex: splay trees, union-find.
Competitive analysis.
■ Make quantitative statements about online algorithms.
■ Ex: paging, load balancing.
Princeton University • COS 423 • Theory of Algorithms • Spring 2001 • Kevin Wayne 2
Bottleneck operation.
■ We count insertions (or re-insertions) and deletions.
■ Overhead of memory management is dominated by (or
proportional to) cost of transferring items.
3 4
Dynamic Table: Insert Dynamic Table: Insert
Dynamic Table Insert Accounting method.
Initialize table size m = 1. ■ Charge each insert operation $3 (amortized cost).
– use $1 to perform immediate insert
INSERT(x) – store $2 in with new item
IF (number of elements in table = m) ■ When table doubles:
Generate new table of size 2m.
– $1 re-inserts item
Re-insert m old elements into new table.
m ← 2m – $1 re-inserts another old item
Aggregate method.
■ Sequence of n insert ops takes O(n) time. n log 2 n
■ Let ci = cost of ith insert. ∑ ci ≤ n+ ∑ 2j
i =1 j =0
= n + ( 2n − 1)
i if i - 1 is an exact power of 2
ci = < 3n
1 otherwise
5 6
Dynamic Table: Insert and Delete Dynamic Table: Insert and Delete
Insert and delete. Insert and delete.
■ Table overflows ⇒ double table size. ■ Table overflows ⇒ double table size.
■ Table ≤ ½ full ⇒ halve table size. ■ Table ≤ ¼ full ⇒ halve table size.
1 1 1 1 DELETE(x)
2 2 2 2 IF (number of elements in table ≤ m / 4)
Generate new table of size m / 2.
3 3 3 3
m ← m / 2
4 4 4 4 Reinsert old elements into new table.
5 5
Delete x from table.
7 8
Dynamic Table: Insert and Delete Dynamic Table: Delete
Accounting analysis.
■ Charge each insert operation $3 (amortized cost). 1 2 3 4 5 6 7 8
– use $1 to perform immediate insert
– store $2 with new item
1 2 3 4 5 6 7
■ When table doubles:
– $1 re-inserts item
– $1 re-inserts another old item 1 2 3 4 5 6
9 10
11 12
Binary Search Tree Binary Search Tree
Binary tree in "sorted" order. Binary tree in "sorted" order.
■ Maintain ordering property for ALL sub-trees. ■ Maintain ordering property for ALL sub-trees.
51 51
14 72 14 72
06 33 53 97 06 33 53 97
13 25 43 64 84 99 13 25 43 64 84 99
13 14
15 16
Splay Splay
Find(x, S): Determine whether element x is in splay tree S. Implementing Join(S, S’).
Insert(x, S): Insert x into S if it is not already there. ■ Call Splay(+∞, S) so that largest element of S is at root and all
Delete(x, S): Delete x from S if it is there. other elements are in left subtree.
Join(S, S’): Join S and S’ into a single splay tree, assuming that ■ Make S’ the right subtree of the root of S.
x < y for all x ∈ S, and y ∈ S’.
Implementing Delete(x, S).
All operations are implemented in terms of basic operation:
■ Call Splay(x, S) to bring x to the root if it is there.
■ Remove x: let S’ and S’’ be the resulting subtrees.
Splay(x, S): Reorganize splay tree S so that element x is at the
root if x ∈ S; otherwise the new root is either ■ Call Join(S’, S’’).
max { k ∈ S : k < x} or min { k ∈ S : k > x} .
Implementing Insert(x, S).
Implementing Find(x, S). ■ Call Splay(x, S) and break tree at root to form S’ and S’’.
■ Call Splay(x, S). ■ Call Join(Join(S’, {x}), S’’).
■ If x is root, then return x; otherwise return NO.
17 18
root
z x
x
y
y y
y D A
x
C ZIG(x) A
x ZIG-ZIG z
C B
A B ZAG(y) B C
A B C D
19 20
Implementing Splay(x, S) Splay Example
Splay(x, S): do following operations until x is root. Apply Splay(1, S) to tree S:
10
■ ZIG: If x has a parent but no grandparent.
■ ZIG-ZIG: If x has a parent y and a grandparent, and if both x and y 9
are either both left children or both right children.
8
■ 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. 7
6 ZIG-ZIG
z x
5
y ZIG-ZAG z y 4
A
3
x
D A B C D 2
1
B C
21 22
9 9
8 8
7 7
6 ZIG-ZIG 6 ZIG-ZIG
5 1
4 4
1 2 5
2
3
23 24
Splay Example Splay Example
Apply Splay(1, S) to tree S: Apply Splay(1, S) to tree S:
10 10
9 1
8 8
1 6 9
6 ZIG-ZIG 4 7 ZIG
4 7 2 5
2 5 3
25 26
10
1 2
8
10 1 8
6 9
8 4
10
4 7
6 9 3 6 9
Splay(2)
2 5 7
4 5 7
3
2 5
27 28
Splay Tree Analysis Splay Tree Analysis
Definitions. Splay invariant: node x always has at least µ(x) credits on deposit.
■ Let S(x) denote subtree of S rooted at x.
■ |S| = number of nodes in tree S. Splay lemma: each splay(x, S) operation requires ≤ 3(µ(S) - µ(x)) + 1
credits to perform the splay operation and maintain the invariant.
■ µ(S) = rank = log |S| .
2
■ µ(x) = µ (S(x)).
Theorem: A sequence of m operations involving n inserts takes
S(8) O(m log n) time.
1 8
Proof:
■ µ(x) ≤ log n ⇒ at most 3 log n + 1 credits are needed for
|S| = 10 4
10 each splay operation.
µ(2) = 3
µ(8) = 3 ■ Find, insert, delete, join all take constant number of splays plus
µ(4) = 2 3 6 9 low-level operations (pointer manipulations, comparisons).
µ(6) = 1 ■ Inserting x requires ≤ log n credits to be deposited to maintain
µ(5) = 0 invariant for new node x.
5 7
■ Joining two trees requires ≤ log n credits to be deposited to
maintain invariant for new root.
29 30
x y
Proof of splay lemma: Let µ(x) and µ’(x) be rank before and single C A
ZIG, ZIG-ZIG, or ZIG-ZAG operation on tree S. ZIG
A B B C
■ We show invariant is maintained (after paying for low-level
operations) using at most:
– 3(µ(S) - µ(x)) + 1 credits for each ZIG operation.
■ In order to maintain invariant, we must pay:
– 3(µ’(x) - µ(x)) credits for each ZIG-ZIG operation.
µ ′ ( x ) + µ ′ (y ) − µ ( x ) − µ (y ) = µ ′ (y ) − µ ( x ) µ(y) = µ’(x)
– 3(µ’(x) - µ(x)) credits for each ZIG-ZAG operation.
≤ µ′ (x ) − µ (x )
≤ 3( µ ′ ( x ) − µ ( x ) )
Thus, if a sequence of of these are done to move x up the tree, we Use extra credit to pay for
= 3( µ (S ) − µ ( x ) )
■
µ’(x) = µ(S)
■
31 32
Splay Tree Analysis Splay Tree Analysis
Proof of splay lemma (ZIG-ZIG): It takes ≤ 3(µ’(x) - µ(x)) credits to Proof of splay lemma (ZIG-ZIG): It takes ≤ 3(µ’(x) - µ(x)) credits to
perform a ZIG-ZIG operation and maintain the splay invariant. perform a ZIG-ZIG operation and maintain the splay invariant.
■ Nasty case: µ(x) = µ’(x).
S z x S’
■ We show in this case µ’(x) + µ’(y) + µ’(z) < µ(x) + µ(y) + µ(z).
y y – don’t need any credit to pay for invariant
D A
– 1 credit left to pay for low-level operations
x z
C B so, for contradiction, suppose µ’(x) + µ’(y) + µ’(z) ≥ µ(x) + µ(y) + µ(z).
ZIG-ZIG ■ Since µ(x) = µ’(x) = µ(z), by monotonicity µ(x) = µ(y) = µ(z).
A B C D
■ After some algebra, it follows that µ(x) = µ’(z) = µ(z).
■ Let a = 1 + |A| + |B|, b = 1 + |C| + |D|, then
µ ′ ( x ) + µ ′ ( y ) + µ ′ (z ) − µ ( x ) − µ ( y ) − µ (z ) = µ ′ ( y ) + µ ′ (z ) − µ ( x ) − µ ( y ) log a = log b = log (a+b+1)
= ( µ ′ ( y ) − µ ( x )) + ( µ ′ ( z ) − µ ( y )) z S S’ x
■ WLOG assume b ≥ a.
≤ ( µ ′ ( x ) − µ ( x )) + ( µ ′ ( x ) − µ ( x )) y
y
= 2( µ ′ ( x ) − µ ( x ) ) log(a + b + 1) ≥ log( 2a ) D A
If µ’(x) > µ(x), then can afford to
■
= 1 + log a x z
pay for constant number of low-level C B
operations and maintain invariant using ≤ 3(µ’(x) - µ(x)) credits. > log a
ZIG-ZIG
A B C D
33 34
z x
y ZIG-ZAG
z y
A
x
D A B C D
B C
35
Princeton University • COS 423 • Theory of Algorithms • Spring 2001 • Kevin Wayne
Interval Trees Interval Trees
(7, 10) 10
39 40
Finding an Overlapping Interval Finding an Overlapping Interval
Interval-Find(i, S): return an interval x that overlaps i = (li, ri ), or Interval-Find(i, S): return an interval x that overlaps i = (li, ri ), or
report that no such interval exists. report that no such interval exists.
A B B C
14 19 ZAG 19 30
43 44
VLSI Database Problem Order Statistic Trees
Add following two operations to BST.
Select(i, S): Return ith smallest key in tree S.
VLSI (r1, r2 ,..., rN) Rank(i, S): Return rank of x in linear order of tree S.
Sort rectangle by x coordinate (keep two copies of
rectangle, one for left endpoint and one for right). Key idea: store size of subtrees in nodes.
FOR i = 1 to 2N
IF (ri is "left" copy of rectangle)
IF (Interval-Find(ri, S)) m 8
RETURN YES
ELSE
Interval-Insert(ri, S) c 5 P 2
ELSE (ri is "right" copy of rectangle)
Interval-Delete(ri, S)
b 1 f 2 q 1
y 29 29 w
ZIG
w 11 22 y
Z V
17 6
V X ZAG X Z
6 4 4 17
47