Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bottom-Up Splay Trees–Analysis

Similar presentations


Presentation on theme: "Bottom-Up Splay Trees–Analysis"— Presentation transcript:

1 Bottom-Up Splay Trees–Analysis
Actual complexity of search, insert, delete, and split is O(n). Actual complexity of join is O(1). Amortized complexity of search, insert, delete, join, and split is O(log n).

2 Potential Function size(x) = #nodes in subtree whose root is x.
rank(x) = floor(log2 size(x)). P(i) = Sx is a tree node rank(x). P(i) is potential after i’th operation. size(x) and rank(x) are computed after i’th operation. P(0) = 0. When join and split operations are done, number of splay trees > 1 at times. P(i) is obtained by summing over all nodes in all trees.

3 Example size(x) is in red. rank(x) is in blue. Potential = 5. 6 2 3 1
20 10 6 8 40 30 3 1 2 1 2 1 1 1 size(x) is in red. rank(x) is in blue. Potential = 5.

4 Root Rank rank(root) <= floor(log2 n)
20 10 6 8 40 30 1 2 3 n is total number of operations. On any root to leaf path at most rank(root) + 1 different rank values. When you insert, only the first of each different value sequence (or last if you go from insert node to root) may increase/change. rank(root) <= floor(log2 n) n is total number of operations

5 Join Actual complexity = 1 Rank of root is <= floor(log2 n).
B Actual complexity = 1 Rank of root is <= floor(log2 n). Other ranks unchanged. Potential increases by <= floor(log2 n). amortized complexity = actual complexity + DP <= 1 + floor(log2 n) n is total number of operations. So, no tree has more than n items ever.

6 Other Operations actual complexity = # of splay steps
amortized complexity = actual complexity + DP = # of splay steps + DP Or cost is 1 if you count cost of checking you are in this case.

7 Splay Step Potential Change
If q = null or q is the root, do nothing. DP = 0. Or cost is 1 if you count cost of checking you are in this case.

8 1-Level Move Do a one-level move. r(x) = rank of x before splay step.
c a q p p q a b c Note, r(x) may be different from node rank just before the operation as the operation (exclusive of the splay) may change size(x). E.g., an insert may increase size(x) by 1. r(x) = rank of x before splay step. r’(x) = rank of x after splay step.

9 1-Level Move DP = r’(p) + r’(q) – r(p) – r(q) <= r’(q) – r(q). p q
a b c DP = r’(p) + r’(q) – r(p) – r(q) <= r’(q) – r(q).

10 2-Level Move (case 1) p q a b c gp d DP = r’(gp) + r’(p) + r’(q) – r(gp) – r(p) – r(q)

11 2-Level Move (case 1) r’(gp) <= r’(q) r’(q) = r(gp)
b c gp d r’(gp) <= r’(q) r (q) <= r(p) r’(q) = r(gp) r’(p) <= r’(q)

12 2-Level Move (case 1) DP = r’(gp) + r’(p) + r’(q) – r(gp) – r(p) – r(q) r’(q) = r(gp) r’(gp) <= r’(q) r’(p) <= r’(q) r (q) <= r(p) DP <= r’(q) + r’(q) – r(q) – r(q) = 2(r’(q) – r(q))

13 2-Level Move (case 1) A more careful analysis reveals that
DP <= 3(r’(q) – r(q)) –1 When r’(q) > r(q), this follows from just proved bound of 2(r’(q) – r(q)). Re-analysis needed for case when r’(q) = r(q). In this case, the previous bound is shown to be 2(r’(q)-r(q))-1. When r’(q) = r(q), we can show that r’(gp) < r’(q) and so amortized cost of splay step is at most -1. Note that now, r(q) = r(p) = r(gp). If r’(gp) = r’(q) then s’(q) > 2^r(q) + 2^r’(gp) = 2^r(q) + 2^r’(q) = 2^(r(q)+1), which violates def of rank. Alternatively, s’(q) > 2s’(gp) by using s(a)+s(b)+1 > s©+s(d)+2 (otherwise r’(q)>r(q)). Note, 2s(q) > s(gp) for r(q) = r(gp) = r’(q).

14 2-Level Move (case 2) Similar to Case 1.

15 Only 1-Level Moves Suppose that the splay node q is initially at level L > 1. Let r(q,j) be the rank of q when it is at level j following/preceding a splay step. When only 1-level moves are made, DP <= S2<=j<=L(r(q,j-1)-r(q,j)) = r(q,1)-r(q,L)

16 Search With Only 1-Level Moves
amortized complexity = # of splay steps + DP <= L-1 + r(q,1)-r(q,L) <= n-1 + floor(log2n) = O(n)

17 Correct Splay Make as many 2-level moves as possible. A 1-level move is made at the end if necessary. When L is odd, floor(L/2) 2-level moves are made; there is no 1-level move. Single 2-level move DP <= 3(r’(q) – r(q)) –1 For complete splay DP <= 3(r(q,1)-r(q,L))-floor(L/2) = 3(r(q,1)-r(q,L))- # of splay steps

18 Correct Splay When L is even, L/2-1 2-level moves are made; there is also one 1-level move. Single 2-level move DP <= 3(r’(q) – r(q)) –1 Single 1-level move DP <= r’(q) – r(q) <= 3(r’(q) – r(q)) For complete splay DP <= 3(r(q,1)-r(q,L))- # of splay steps+1 DP <= S2<=j<=L(r(j-1)-r(j)) = r(1)-r(L)

19 Correct Splay So, regardless of the value of L
DP <= 3(r(q,1)-r(q,L))- # of splay steps+1 <= 3floor(log2n)-# of splay steps+1

20 Search With Correct Splay
amortized complexity = # of splay steps + DP <= # of splay steps + 3floor(log2n)-# of splay steps+1 = 3floor(log2n)+1 = O(log n)

21 Insert 7 On every leaf to root path ranks are non-decreasing.
1 4 7 On every leaf to root path ranks are non-decreasing. At most floor(log2 n)+1 different ranks on such a path. Insert may increase the rank of only the last node in each sequence of equal rank nodes (view sequence from bottom to top). When you insert, potential increases by up to floor(log2 n)+1.

22 Insert With Correct Splay
total potential change caused by an insert <= floor(log2n)+1 + 3floor(log2n)-# of splay steps+1 amortized complexity = # of splay steps + DP <= 4floor(log2n)+2 = O(log n)

23 Delete With Correct Splay
total potential change caused by a delete <= 3floor(log2n)-# of splay steps+1 amortized complexity = # of splay steps + DP <= 3floor(log2n)+1 = O(log n)

24 Split With Correct Splay
total potential change caused by a split <= 3floor(log2n)-# of splay steps+2 amortized complexity = # of splay steps + DP <= 3floor(log2n)+2 = O(log n) Split does a dummy insert that increases potential (exclusive of the potential change caused by the following splay). Final removal of root reduces potential by floor log (n+1). So, net increase is at most 1.


Download ppt "Bottom-Up Splay Trees–Analysis"

Similar presentations


Ads by Google