Analysis Review – Amortization Potential Functions Splay Tree Operations

32
Lecture 23 Analysis review – amortization Potential functions Splay tree operations

Transcript of Analysis Review – Amortization Potential Functions Splay Tree Operations

Page 1: Analysis Review – Amortization Potential Functions Splay Tree Operations

Lecture 23

Analysis review – amortization

Potential functions

Splay tree operations

Page 2: Analysis Review – Amortization Potential Functions Splay Tree Operations

Doubling array stacks

Page 3: Analysis Review – Amortization Potential Functions Splay Tree Operations

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;

}

Page 4: Analysis Review – Amortization Potential Functions Splay Tree Operations

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)

Page 5: Analysis Review – Amortization Potential Functions Splay Tree Operations

Alternative analysis

• Idea: Amortization – you buy a new car for $20,000– You expect it to last 10 years

Page 6: Analysis Review – Amortization Potential Functions Splay Tree Operations

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.

Page 7: Analysis Review – Amortization Potential Functions Splay Tree Operations

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

Page 8: Analysis Review – Amortization Potential Functions Splay Tree Operations

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!

Page 9: Analysis Review – Amortization Potential Functions Splay Tree Operations

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

Page 10: Analysis Review – Amortization Potential Functions Splay Tree Operations

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.

Page 11: Analysis Review – Amortization Potential Functions Splay Tree Operations

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.

Page 12: Analysis Review – Amortization Potential Functions Splay Tree Operations

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!

Page 13: Analysis Review – Amortization Potential Functions Splay Tree Operations

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.

Page 14: Analysis Review – Amortization Potential Functions Splay Tree Operations

We’ll return to potential functions for analyzing splay trees

Page 15: Analysis Review – Amortization Potential Functions Splay Tree Operations

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

Page 16: Analysis Review – Amortization Potential Functions Splay Tree Operations

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

Page 17: Analysis Review – Amortization Potential Functions Splay Tree Operations

Splay trees

• Binary search tree

• One main operation: splay node to root

• All other operations defined in terms of this one

Page 18: Analysis Review – Amortization Potential Functions Splay Tree Operations

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]

Page 19: Analysis Review – Amortization Potential Functions Splay Tree Operations

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

Page 20: Analysis Review – Amortization Potential Functions Splay Tree Operations

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

Page 21: Analysis Review – Amortization Potential Functions Splay Tree Operations

Example: split 5

7

63

84

7

63

84

Search process. 5 not found; 6 was last node encountered

Page 22: Analysis Review – Amortization Potential Functions Splay Tree Operations

Example: split 5

7

63

84

Splay 6 to root5 is less than 6, so delete marked edge

6

3

74

8

Page 23: Analysis Review – Amortization Potential Functions Splay Tree Operations

6

3

74

8

Return the two resulting trees

Page 24: Analysis Review – Amortization Potential Functions Splay Tree Operations

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)

Page 25: Analysis Review – Amortization Potential Functions Splay Tree Operations

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”

Page 26: Analysis Review – Amortization Potential Functions Splay Tree Operations

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”

B

CD

E

F

G

H

A

Page 27: Analysis Review – Amortization Potential Functions Splay Tree Operations

ZIG operation

x

y x

y

A B

C A

B C

Page 28: Analysis Review – Amortization Potential Functions Splay Tree Operations

ZIG-ZIG

x

y

x

y

A B

C

A

B

C

D

z

D

z

Page 29: Analysis Review – Amortization Potential Functions Splay Tree Operations

ZIG-ZAG

x

y

x

y

A

B C

A B C

D

z

D

z

Page 30: Analysis Review – Amortization Potential Functions Splay Tree Operations

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

Page 31: Analysis Review – Amortization Potential Functions Splay Tree Operations

Rotation of an edge

x

yx

y

A B

CA

B C