Sei sulla pagina 1di 32

Lecture 23

Analysis review – amortization


Potential functions
Splay tree operations
Doubling array stacks
Array Based Stacks
Operation Counting (Array-Doubling Implementation)

public void push(E obj) {


if (t == S.length() - 1) {
A ← (E[]) new Object[S.length * 2];
for i ← 0 to S.length - 1
A[i] ← S[i];
S ← A;
}
++t;
S[t] ← obj;
}
Analysis
• Worst case: a “push” into a stack with 2n items in
it
– Costs about 2n to copy all items to newly allocated
array
• But worst case happens rarely
• In a sequence of k push operations, starting
from empty stack, at most log k doublings
• Total cost of doublings = 1 + 2 + ..+ ~ 2k
• So average cost was O(1)
Alternative analysis
• Idea: Amortization
– you buy a new car for $20,000
– You expect it to last 10 years
Amortization
• Approach 1: use the car, and in year 11,
you have a $20,000 cost to replace it
• Approach 2: use the car, and in each year
put $2000 in the bank
– In the 11th year, spend the $20,000 you’ve
accumulated.
Analysis
• Approach 1: use the car, and in year 11,
you have a $20,000 cost to replace it
– Worst case cost: 20,000
• Approach 2: use the car, and in each year
put $2000 in the bank
– In the 11th year, spend the $20,000 you’ve
accumulated.
– Worst case cost: $2000
Amortized analysis
• We can analyze the doubling array stack
by saying “for each push operation, we’re
going to pay whatever time it may take,
plus enough time to do two more
elementary operations.”
• Time taken for ordinary push: O(1) still!
Amortized analysis, continued
• Time taken for exceptional push onto a stack of
size n:
– n copies
– plus an ordinary push.
• There must have been at least n/2 ordinary
pushes since the last exceptional “push”
• We’ve therefore accumulated 2(n/2) extra
operations
• Use these to pay for the “copy” operations
• Amortized cost for exceptional push: also O(1)!
Potential functions
• Potential functions are a richer version of
amortization.
• Idea: to each “state” s of the system (k items in
a stack, some particular “shape” for a binary
tree, …), we assign a number f(s).
– Choosing f to make it useful is a dark art!
– Typically f(s) represents “how bad is state s”?
• Example: in doubling-array stack, having n items in a stack of
size n is pretty bad, because the next push will be expensive
• having ~n/2 items in such a stack is fine, because we just
finished doubling.
Potential functions (cont.)
• We let c(s1  s2) denote the ordinary cost
of changing from state s1 to state s2
• Define the “amortized cost” to be
h(s1  s2) = c(s1  s2) + f(s2) – f(s1)

• Do analysis in terms of amortized cost


– …with a check at the end that the potential-
change wasn’t too great.
Amortized cost for a sequence of
operations
h(s1  s2) = c(s1  s2) + f(s2) – f(s1)
• What happens if we go from s1 to s4?
h(s1  s4) = h(s1  s2) + h(s2  s3) + h(s3  s4)
= h(s1  s2) + h(s2  s3) + c(s3  s4) + f(s4) – f(s3)
= h(s1  s2) + c(s2  s3) + f(s3) – f(s2) + c(s3  s4) + f(s4) – f(s3)
= h(s1  s2) + c(s2  s3) + c(s3  s4) + f(s4) – f(s3) + f(s3) – f(s2)
= h(s1  s2) + c(s2  s3) + c(s3  s4) + f(s4)– f(s2)
= c(s1  s2) + f(s2) – f(s1) + c(s2  s3) + c(s3  s4) + f(s4)– f(s2)
= c(s1  s2) + c(s2  s3) + c(s3  s4) + f(s4)– f(s2) + f(s2) – f(s1)
= c(s1  s2) + c(s2  s3) + c(s3  s4) + f(s4) – f(s1)
= ordinary cost + change in potential from start to finish!
Amortized cost for sequence
(cont’d)
h(s1  s4) =
c(s1  s2) + c(s2  s3) + c(s3  s4) +f(s4)– f(s1)
• This is just the ordinary cost plus the change in
potential from the initial to final state!
• If this change in potential is small enough (again,
choosing f requires cleverness!) then everything
works out nicely.
We’ll return to potential functions
for analyzing splay trees
Binary Search Tree Application
• Storing items that naturally occur in sequences
– Stored in a way that makes access to kth item take
time log n, when there are n items total
– Get fast access time only if tree is balanced
– Want to do insertion/deletion operations, but maintain
balance
• Sample application: video editing
– Tree node with key k stores location of the kth video-
frame on disk
– Operations like “delete this section” or “move this clip
to later” supported by BSTs, and are very fast
– access/insertion/deletion are fast using hashtable, too,
but the bulk-move ops are not
Balanced BSTs
• Tons of kinds.
– Red/black trees
– 2-3-4 trees
– skip lists (sort of)
– treaps (trees with heap order on a random
value)
– SPLAY TREES
Splay trees
• Binary search tree
• One main operation: splay node to root
• All other operations defined in terms of
this one
Operations
• t is a splay tree, i is an item
• access(i, t):
– do binary search for item i; if you find it, splay
to root. If not, splay the last non-null node
encountered to the root. [in lecture, we
changed this to “last node less than i”, but this
was the wrong “fix” -- jfh]
Operations (cont.)
• join(t1, t2): combines two trees t1 and t2
into a single tree, assuming all items in t1
are less than all items in t2.
– access largest item in t1; this makes it the
root, and its right subtree empty
– insert t2 as the right subtree
Operations (cont.)
• split(i, t): split tree t into t1 and t2, with t1
containing all items less than or equal to i,
and t2 containing all items greater than i.
– splay(i, t); this moves i to the root (or perhaps
some node near to i, if i is not in the tree)
– if i <= root, delete the edge from the root to its
right child; else delete the edge from root to
left child; return the two resulting trees
Example: split 5

7 7

4 8 4 8

3 6 6
3

Search process. 5 not found; 6


was last node encountered
Example: split 5

7
6
4 8
4 7

3 6
3 8

Splay 6 to root
5 is less than 6, so delete marked edge
6

4 7

3 8

Return the two resulting trees


Operations (cont.)
• insert(i, t): insert item i in tree t
– first split(i, t) to get t1 and t2
– build new tree with i at root, t1 and t2 as left
and right subtrees
• delete(i, t): delete item i from tree t
– first access(i, t) to get i to root
– let t1 and t2 be left and right subtrees of root
– return join(t1, t2)
Splay operations
• while (x not root)
– do appropriate operation to x
• Three cases
– parent p(x) of x is the root of the tree (“zig”)
– else:
• x and p(x) are both left or both right children (“zig zig”)
• x is left and p(x) is right child, or vice versa (“zig zag”
parent p(x) of x is the root of the tree
(“zig”)
else:
x and p(x) are both left or both
right children (“zig zig”)
x is left and p(x) is right child, or
vice versa (“zig zag”
A

B G

D H
C
E

F
ZIG operation

y x
y
x

C A

A B B C
ZIG-ZIG

z x
y

D A
x
z
C B

A B C D
ZIG-ZAG

z x

y y z

x D

A A B C D

B C
Operations in words
• zig: rotate edge joining x to p(x)
• zig-zig: rotate edge from p(x) to p(p(x));
then rotate edge from x to p(x)
• zig-zag: rotate edge from x to p(x); rotate
edge from x to new p(x).
Rotation of an edge

y
x
y
x

C
A

A B
B C
DEMO
• Demo

Potrebbero piacerti anche