Analysis review amortization - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Analysis review amortization

Description:

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 ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 33
Provided by: JFH
Category:

less

Transcript and Presenter's Notes

Title: Analysis review amortization


1
Lecture 23
  • Analysis review amortization
  • Potential functions
  • Splay tree operations

2
Doubling array stacks
3
Array Based Stacks Operation Counting
(Array-Doubling Implementation)?
public void push(E obj) if (t S.length() -
1) A ? (E) new
ObjectS.length 2 for i ? 0 to
S.length - 1 Ai ? Si S ?
A t St ? obj
4
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)

5
Alternative analysis
  • Idea Amortization
  • you buy a new car for 20,000
  • You expect it to last 10 years

6
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 youve
    accumulated.

7
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 youve
    accumulated.
  • Worst case cost 2000

8
Amortized analysis
  • We can analyze the doubling array stack by saying
    for each push operation, were 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!

9
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
  • Weve therefore accumulated 2(n/2) extra
    operations
  • Use these to pay for the copy operations
  • Amortized cost for exceptional push also O(1)!

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

11
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 wasnt too great.

12
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!

13
Amortized cost for sequence (contd)
  • 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.

14
Well return to potential functions for analyzing
splay trees
15
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

16
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

17
Splay trees
  • Binary search tree
  • One main operation splay node to root
  • All other operations defined in terms of this one

18
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

19
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

20
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 lt 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

21
Example split 5
7
7
8
4
8
4
6
3
6
3
Search process. 5 not found 6 was last node
encountered
22
Example split 5
7
6
8
4
7
4
6
3
3
8
Splay 6 to root 5 is less than 6, so delete
marked edge
23
6
7
4
3
8
Return the two resulting trees
24
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)

25
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

26
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
G
B
D
H
C
E
F
27
ZIG operation
y
x
y
x
C
A
B
C
B
A
28
ZIG-ZIG
x
z
y
y
A
D
x
z
B
C
B
A
C
D
29
ZIG-ZAG
x
z
y
y
z
D
x
A
A
B
C
D
C
B
30
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).

31
Rotation of an edge
y
x
y
x
C
A
B
A
C
B
32
DEMO
  • Demo
Write a Comment
User Comments (0)
About PowerShow.com