






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Worst-case analysis. n^
Analyze running time as function of worst input of a given size. Average case analysis. n^
Analyze average running time over some distribution of inputs. n^
Ex: quicksort. Amortized analysis. n^
Worst-case bound on sequence of operations. n^
Ex: splay trees, union-find. Competitive analysis. n^
Make quantitative statements about online algorithms. n^
Ex: paging, load balancing.
3
Amortized analysis. n^
Worst-case bound on sequence of operations.
-^
no probability involved
n^
Ex: union-find.
-^
sequence of m union and find operations starting with nsingleton sets takes O((m+n)
α
(n)) time.
-^
single union or find operation might be expensive, but only
α
(n)
on average
Dynamic tables. n^
Store items in a table (e.g., for open-address hash table, heap). n^
Items are inserted and deleted.
-^
too many items inserted
copy all items to larger table
-^
too many items deleted
copy all items to smaller table
Amortized analysis. n^
Any sequence of n insert / delete operations take O(n) time. n^
Space used is proportional to space required. n^
Note: actual cost of a single insert / delete can be proportional to nif it triggers a table expansion or contraction. Bottleneck operation. n^
We count insertions (or re-insertions) and deletions. n^
Overhead of memory management is dominated by (orproportional to) cost of transferring items.
5
Aggregate method. n^
Sequence of n insert ops takes O(n) time. n^
Let c
= cost of ii
th
insert.
Initialize table size m = 1.INSERT(x)
IF (number of elements in table = m)
Generate new table of size 2m.Re-insert m old elements into new table.m
2m
Insert x into table.
Dynamic Table Insert
otherwise 1
of
power
exact an is 1
if
i
i
c i^
n j
j
n i
i
2 log
0
1
=
=
Accounting method. n^
Charge each insert operation $3 (amortized cost).
-^
use $1 to perform immediate insert
-^
store $2 in with new item
n^
When table doubles:
-^
$1 re-inserts item
-^
$1 re-inserts another old item
7
Insert and delete. n^
Table overflows
double table size.
n^
Table
½ full
halve table size.
Bad idea: can cause thrashing. 1 2 3 4 5
Initialize
table size m = 1.
DELETE(x)
IF (number of elements in table
m / 4)
Generate new table of size m / 2.m
m / 2
Reinsert old elements into new table.
Delete x from table.
Dynamic Table Delete
Insert and delete. n^
Table overflows
double table size.
n^
Table
¼ full
halve table size.
Docsity.com
13
Binary tree in "sorted" order. n^
Maintain ordering property for ALL sub-trees.
Binary tree in "sorted" order. n^
Maintain ordering property for ALL sub-trees.
15
Insert, delete, find (symbol table). n^
Amount of work proportional to height of tree. n^
O(N) in "unbalanced" search tree. n^
O(log N) in "balanced" search tree. Types of BSTs. n^
AVL trees, 2-3-4 trees, red-black trees. n^
Treaps, skip lists, splay trees. BST vs. hash tables. n^
Guaranteed vs. expected performance. n^
Growing and shrinking. n^
Augmented data structures: order statistic trees, interval trees.
SearchInsert
Splay trees (Sleator-Tarjan, 1983a). Self-adjusting BST. n^
Most frequently accessed items are close to root. n^
Tree automatically reorganizes itself after each operation.
-^
no balance information is explicitly maintained
n^
Tree remains "nicely" balanced, but height can potentially be n - 1. n^
Sequence of m ops involving n inserts takes O(m log n) time. Theorem (Sleator-Tarjan, 1983a). Splay trees are as efficient (inamortized sense) as static optimal BST.Theorem (Sleator-Tarjan, 1983b). Shortest augmenting path algorithmfor max flow can be implemented in O(mn log n) time. n^
Sequence of mn augmentations takes O(mn log n) time! n^
Splay trees used to implement dynamic trees (link-cut trees).
Docsity.com
17
Find(x, S):
Determine whether element x is in splay tree S.
Insert(x, S):
Insert x into S if it is not already there.
Delete(x, S):
Delete x from S if it is there.
Join(S, S’):
Join S and S’ into a single splay tree, assuming thatx < y for all x
S, and y
All operations are implemented in terms of basic operation:Splay(x, S):
Reorganize splay tree S so that element x is at theroot if x
S; otherwise the new root is either
max { k
S : k < x} or min { k
S : k > x}.
Implementing Find(x, S). n^
Call Splay(x, S). n^
If x is root, then return x; otherwise return NO.
Implementing Join(S, S’). n^
Call Splay(+
, S) so that largest element of S is at root and all
other elements are in left subtree. n^
Make S’ the right subtree of the root of S. Implementing Delete(x, S). n^
Call Splay(x, S) to bring x to the root if it is there. n^
Remove x: let S’ and S’’ be the resulting subtrees. n^
Call Join(S’, S’’). Implementing Insert(x, S). n^
Call Splay(x, S) and break tree at root to form S’ and S’’. n^
Call Join(Join(S’, {x}), S’’).
19
Splay(x, S): do following operations until x is root. n^
ZIG: If x has a parent but no grandparent, then rotate(x). n^
ZIG-ZIG: If x has a parent y and a grandparent, and if both x and yare either both left children or both right children. n^
ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, yis a left child and the other is a right child. A
x
y
y
x
ZIG(x)ZAG(y)
root
Splay(x, S): do following operations until x is root. n^
ZIG: If x has a parent but no grandparent. n^
ZIG-ZIG: If x has a parent y and a grandparent, and if both x and yare either both left children or both right children. n^
ZIG-ZAG: If x has a parent y and a grandparent, and if one of x, yis a left child and the other is a right child.
x
y
z
z
y
x
Docsity.com
25
Apply Splay(1, S) to tree S:
10
9
8
1
6
7
2
4 3
5
Apply Splay(1, S) to tree S:
10
1
8
9
6
7
2
4 3
5
27
Apply Splay(1, S) to tree S:
1
10
8
9
6
7
2
4 3
5
Apply Splay(2, S) to tree S:
1
10
8
9
6
7
2
4 3
5
2
8
4
6
3
10
1
9
5
7
Splay(2)
Docsity.com
29
Definitions. n^
Let S(x) denote subtree of S rooted at x. n^
|S| = number of nodes in tree S. n^
μ
(S) = rank =
log |S|
n^
μ
(x) =
μ
(S(x)).
2
8
4
6
3
10
1
9
5
7
|S| = 10 μ
μ
μ
μ
μ
Splay invariant: node x always has at least
μ
(x) credits on deposit.
Splay lemma: each splay(x, S) operation requires
μ
μ
(x)) + 1
credits to perform the splay operation and maintain the invariant.Theorem: A sequence of m operations involving n inserts takesO(m log n) time.Proof: n^
μ
(x)
log n
at most 3
log n
each splay operation. n^
Find, insert, delete, join all take constant number of splays pluslow-level operations (pointer manipulations, comparisons). n^
Inserting x requires
log n
credits to be deposited to maintain
invariant for new node x. n^
Joining two trees requires
log n
credits to be deposited to
maintain invariant for new root.
31
Splay invariant: node x always has at least
μ
(x) credits on deposit.
Splay lemma: each splay(x, S) operation requires
μ
μ
(x)) + 1
credits to perform the splay operation and maintain the invariant.Proof of splay lemma: Let
μ
(x) and
μ
’(x) be rank before and single
ZIG, ZIG-ZIG, or ZIG-ZAG operation on tree S. n^
We show invariant is maintained (after paying for low-leveloperations) using at most:
-^
μ
μ
(x)) + 1 credits for each ZIG operation.
-^
μ
’(x) -
μ
(x)) credits for each ZIG-ZIG operation.
-^
μ
’(x) -
μ
(x)) credits for each ZIG-ZAG operation.
n^
Thus, if a sequence of of these are done to move x up the tree, weget a telescoping sum
total credits
μ
μ
(x)) + 1.
Proof of splay lemma (ZIG): It takes
μ
μ
(x)) + 1 credits to
perform a ZIG operation and maintain the splay invariant. n^
In order to maintain invariant, we must pay: n^
Use extra credit to pay forlow-level operations.
x
x
x
x
x
x y y x y x
A
B x^
C y
C
B
y
A
x
μ (y) =
μ
’(x)
root ZIG
μ ’(x) =
μ
Docsity.com
37
Support following operations.Interval-Insert(i, S):
Insert interval i = (
li
, r
) into tree S.i
Interval-Delete(i, S):
Delete interval i = (
li
, r
) from tree S.i
Interval-Find(i, S):
Key ideas: n^
Tree nodes contain interval. n^
BST keyed on left endpoint.
Key
Interval
39
Key ideas: n^
Tree nodes contain interval. n^
BST keyed on left endpoint. n^
Additional info: store maxendpoint in subtree rootedat node.
max insubtree
x
root(S)
WHILE (x != NULL)
IF (x overlaps i)
RETURN t
IF (left[x] = NULL OR
max[left[x]] <
l i
x
right[x]
x
left[x]
RETURN NOSplay last node on pathtraversed.
Interval-Find (i, S)
Interval-Find(i, S): return an interval x that overlaps i = (
l
, ri
), ori
report that no such interval exists. (4, 8)
Docsity.com
41
Interval-Find(i, S): return an interval x that overlaps i = (
l
, ri
), ori
report that no such interval exists.Case 1 (right). If search goes right,then there exists an overlap in rightsubtree or no overlap in either.Proof. Suppose no overlap in right. n^
left[x]
no overlap in left. n^
max[left[x]] <
l i
no overlap in left.
x
root(S)
WHILE (x != NULL)
IF (x overlaps i)
RETURN t
IF (left[x] = NULL OR
max[left[x]] <
l i
x
right[x]
x
left[x]
RETURN NOSplay last node on pathtraversed.
Interval-Find (i, S)
i = (
l
, ri
i^
left[x]
max
Interval-Find(i, S): return an interval x that overlaps i = (
l
, ri
), ori
report that no such interval exists.Case 2 (left). If search goes left,then there exists an overlap in leftsubtree or no overlap in either.Proof. Suppose no overlap in left. n^
l i
max[left[x]] = r
j^
for
some interval j in left subtree. n^
Since i and j don’t overlap, we have^ l
i^
r i^
l j
r
.j
n^
Tree sorted by
l
for any interval
k in right subtree:
r i^
l j
l k^
no overlap in right subtree.
x
root(S)
WHILE (x != NULL)
IF (x overlaps i)
RETURN x
IF (left[x] = NULL OR
max[left[x]] <
l i
x
right[x]
x
left[x]
RETURN NOSplay last node on pathtraversed.
Interval-Find (i, S)
i = (
l i
, r
i^
j = (
l j
, r
)j^
k = (
l k
, r
)k^
43
Need to maintain augmented data structure during tree-modifying ops. n^
Rotate: can fix sizes in O(1) time by looking at children: A 14
r^ x
x
right
x
left
x
max[
max[
max ]
max[
VLSI database problem. n^
Input: integrated circuit represented as a list of rectangles. n^
Goal: decide whether any two rectangles overlap. Algorithm idea. n^
Move a vertical "sweep line" from left to right. n^
Store set of rectangles that intersect the sweep line in an intervalsearch tree (using y interval of rectangle).
Docsity.com