1 / 72

Splay trees (Sleator, Tarjan 1983)

Splay trees (Sleator, Tarjan 1983). Main idea. Try to arrange so frequently used items are near the root. We shall assume that there is an item in every node including internal nodes. We can change this assumption so that items are at the leaves. First attempt.

Download Presentation

Splay trees (Sleator, Tarjan 1983)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Splay trees(Sleator, Tarjan 1983)

  2. Main idea • Try to arrange so frequently used items are near the root • We shall assume that there is an item in every node including internal nodes. We can change this assumption so that items are at the leaves.

  3. First attempt Move the accessed item to the root by doing rotations y x <===> x C A y B C A B

  4. e e e d F d F d F E E c a E c A c b a A b A B C D B b a D C D B C Move to root (example)

  5. Move to root (analysis) There are arbitrary long access sequences such that the time per access is Ω(n) ! Homework ?

  6. z y D C x A B Splaying Does rotations bottom up on the access path, but rotations are done in pairs in a way that depends on the structure of the path. A splay step: (1) zig - zig x ==> A y B z C D

  7. Splaying (cont) (2) zig - zag z x ==> y D y z A B C D x A B C (3) zig y x ==> x C A y B C A B

  8. i i ==> ==> h h J J g I g I f H f H A e A a d G d e a b F G B B b c F E c E C D D C Splaying (example) i h J g I f H A e d G c B b C a D E F

  9. i ==> ==> a h J h a I f g i A d e f H I J g b F G e B A d H c F G E b B C D c E C D Splaying (example cont) i h J g I f H A a d e b F G B c E C D

  10. Splaying (analysis) Assume each item i has a positive weight w(i) which is arbitrary but fixed. Define the sizes(x) of a node x in the tree as the sum of the weights of the items in its subtree. The rank of x: r(x) = log2(s(x)) Measure the splay time by the number of rotations

  11. Access lemma The amortized time to splay a node x in a tree with root t is at most 3(r(t) - r(x)) + 1 = O(log(s(t)/s(x))) Potential used: The sum of the ranks of the nodes. This has many consequences:

  12. Balance theorem Balance Theorem: Accessing m items in an n node splay tree takes O((m+n) log n) Proof:

  13. Static optimality theorem: If every item is accessed at least once then the total access time is O(m +  q(i) log (m/q(i)) ) n i=1 Static optimality theorem For any item i let q(i) be the total number of time i is accessed Optimal average access time up to a constant factor.

  14. Proof of the access lemma The amortized time to splay a node x in a tree with root t is at most 3(r(t) - r(x)) + 1 = O(log(s(t)/s(x))) proof. Consider a splay step. Let s and s’, r and r’ denote the size and the rank function just before and just after the step, respectively. We show that the amortized time of a zig step is at most 3(r’(x) - r(x)) + 1, and that the amortized time of a zig-zig or azig-zag step is at most 3(r’(x)-r(x)) The lemma then follows by summing up the cost of all splay steps

  15. Proof of the access lemma (cont) (3) zig y x ==> x C A y B C A B amortized time(zig) = 1 +  = 1 + r’(x) + r’(y) - r(x) - r(y)  1 + r’(x) - r(x)  1 + 3(r’(x) - r(x))

  16. z y D C x A B Proof of the access lemma (cont) (1) zig - zig x ==> A y B z C D amortized time(zig-zig) = 2 +  = 2 + r’(x) + r’(y) + r’(z) - r(x) - r(y) - r(z) = 2 + r’(y) + r’(z) - r(x) - r(y)  2 + r’(x) + r’(z) - 2r(x)  2r’(x) - r(x) - r’(z) + r’(x) + r’(z) - 2r(x) = 3(r’(x) - r(x)) 2  -(log(p) + log(q)) = log(1/p) + log(1/q) = log(s’(x)/s(x)) + log(s’(x)/s(z))= r’(x)-r(x) + r’(x)-r(z)

  17. Proof of the access lemma (cont) (2) zig - zag z x ==> y D y z A B C D x A B C Similar. (do at home)

  18. Intuition 9 8 7 6 5 4 3 2 1

  19. Intuition (Cont)

  20. Intuition 9 9 8 8 7 7 6 6 5 5  = 0 4 4 3 1 2 2 3 1

  21. 9 9 8 8 7 7 6 6 5 1 4 4 5  = log(5) – log(3) + log(1) – log(5) = -log(3) 2 1 3 2 3

  22. 9 9 8 8 1 7 6 6 7 4 5 1 2 4 3 5  = log(7) – log(5) + log(1) – log(7) = -log(5) 2 3

  23. 1 9 8 9 8 6 7 1 4 6 5 2 7 4 3 5 2 3  = log(9) – log(7) + log(1) – log(9) = -log(7)

  24. Static finger theorem: Let f be an arbitrary fixed item, the total access time is O(nlog(n) + m +  log(|ij-f| + 1)) m j=1 Static finger theorem Suppose all items are numbered from 1 to n in symmetric order. Let the sequence of accessed items be i1,i2,....,im Splay trees support access within the vicinity of any fixed finger as good as finger search trees.

  25. Working set theorem Let t(j), j=1,…,m, denote the # of different items accessed since the last access to item j or since the beginning of the sequence. Working set theorem: The total access time is Proof:

  26. Application: Data Compression via Splay Trees Suppose we want to compress text over some alphabet  Prepare a binary tree containing the items of  at its leaves. • To encode a symbol x: • Traverse the path from the root to x spitting 0 when you go left and 1 when you go right. • Splay at the parent of x and use the new tree to encode the next symbol

  27. a b a b c d e f g h c d e f g h Compression via splay trees (example) aabg... 000

  28. a b a b c d e f g h c d e f g h Compression via splay trees (example) aabg... 000 0

  29. a b c d e f g h Compression via splay trees (example) a b c d e f g h aabg... 0000 10

  30. a b c d e f g h Compression via splay trees (example) a b c d e f g h aabg... 0000 10 1110

  31. Decoding Symmetric. The decoder and the encoder must agree on the initial tree.

  32. Compression via splay trees (analysis) How compact is this compression ? Suppose m is the # of characters in the original string The length of the string we produce is m + (cost of splays) by the static optimality theorem m + O(m +  q(i) log (m/q(i)) ) = O(m +  q(i) log (m/q(i)) ) Recall that the entropy of the sequence  q(i) log (m/q(i)) is a lower bound.

  33. Compression via splay trees (analysis) In particular the Huffman code of the sequence is at least  q(i) log (m/q(i)) But to construct it you need to know the frequencies in advance

  34. Compression via splay trees (variations) D. Jones (88) showed that this technique could be competitive with dynamic Huffman coding (Vitter 87) Used a variant of splaying called semi-splaying.

  35. * * z Regular zig - zig x ==> y D A y C x B z A B C D Semi - splaying Semi-splay zig - zig z * y ==> y D x z * C x A B C D A B Continue splay at y rather than at x.

  36. i i T2 s(T1) + s(T2) log( ) Amortize time: 3(log(s(T1)/s(i)) + 1 + T1 T1 T2 s(T1) ≤ 3log(W/w(i)) + O(1) Update operations on splay trees Catenate(T1,T2): Splay T1 at its largest item, say i. Attach T2 as the right child of the root. T1 T2

  37. Update operations on splay trees (cont) split(i,T): Assume i  T Splay at i. Return the two trees formed by cutting off the right son of i i i T T2 T1 Amortized time = 3log(W/w(i)) + O(1)

  38. Update operations on splay trees (cont) split(i,T): What if i  T ? Splay at the successor or predecessor of i (i- or i+). Return the two trees formed by cutting off the right son of i or the left son of i i- i- T T2 T1 Amortized time = 3log(W/min{w(i-),w(i+)}) + O(1)

  39. Update operations on splay trees (cont) insert(i,T): Perform split(i,T) ==> T1,T2 Return the tree i T1 T2 W-w(i) ) 3log( Amortize time: + log(W/w(i)) + O(1) min{w(i-),w(i+)}

  40. Update operations on splay trees (cont) delete(i,T): Splay at i and then return the catenation of the left and right subtrees i T1 + T2 T1 T2 W-w(i) ) 3log( + O(1) Amortize time: 3log(W/w(i)) + w(i-)

  41. Open problems Dynamic optimality conjecture: Consider any sequence of successful accesses on an n-node search tree. Let A be any algorithm that carries out each access by traversing the path from the root to the node containing the accessed item, at the cost of one plus the depth of the node containing the item, and that between accesses perform rotations anywhere in the tree, at a cost of one per rotation. Then the total time to perform all these accesses by splaying is no more than O(n) plus a constant times the cost of algorithm A.

  42. Open problems Dynamic finger conjecture (now theorem) The total time to perform m successful accesses on an arbitrary n-node splay tree is O(m + n +  (log |ij+1 - ij| + 1)) where the jth access is to item ij m j=1 Very complicated proof showed up in SICOMP recently (Cole et al)

  43. Tango trees(Demaine, Harmon, Iacono, Patrascu 2004)

  44. Lower bound A reference tree

  45. Lower bound A reference tree y

  46. Left region of y y

  47. Right region of y y

  48. IB(σ,y) = #of alternations between accesses to the left region of y and accesses to the right region of y y

  49. y IB(σ) = yIB(σ,y)

  50. The lower bound (Wilber 89) OPT(σ) ≥ ½ IB(σ) - n

More Related