Slide 1

Slide 1 text

Introduction Operation Time Analysis Applications Conclusion Splay Tree and the Dynamic Tree Problem Self Adjusting Binary Search Tree (D. Sleator, R. Tarjan, 1985) Jongwook Choi and Taihyun Hwang Seoul National University 4541.561 Advanced Computation Theory Term Project June 7, 2011

Slide 2

Slide 2 text

Introduction Operation Time Analysis Applications Conclusion Splay Tree : Structure Splay Tree is a simple BST with Splay operation. The Splay operation is a kind of restructuring heuristic, applying tree rotations to move the last-accessed node to the root. Due to the Splay operation, costs of operations are reduced to O (log n ) . Furthermore, it performs especially well on special access patterns.

Slide 3

Slide 3 text

Introduction Operation Time Analysis Applications Conclusion Supported Operations In the following, T, T0 are splay trees and x are nodes. Splay( T, x ) move the node x of T to the root. Find( T, k ) find the node x with key k. Insert( T, k ) insert a new node with key k into T. Delete( T, x ) delete the node x of T.

Slide 4

Slide 4 text

Introduction Operation Time Analysis Applications Conclusion Supported Operations In the following, T, T0 are splay trees and x are nodes. Splay( T, x ) move the node x of T to the root. Find( T, k ) find the node x with key k. Insert( T, k ) insert a new node with key k into T. Delete( T, x ) delete the node x of T. Split( T, k ) Join( T 1 , T 2) . . . All the cost of above operations are O (log n ) amortized, O ( n ) worst.

Slide 5

Slide 5 text

Introduction Operation Time Analysis Applications Conclusion Notations and Definitions root ( T ) the root node of T Nil the null node key ( x ) the key value of node x Tx the subtree rooted at node x x 2 T x is a node contained in T p ( x ) the parent node of x left ( x ) the left children node of x (if any) right ( x ) the right children node of x (if any) x the predecessor of x x+ the successor of x Min( T ) the node with minimum key in T Max( T ) the node with maximum key in T h ( T ) the height of tree T

Slide 6

Slide 6 text

Introduction Operation Time Analysis Applications Conclusion The Splay Operation Splay( T, x ) : moves the node x to the root of T. If x = root ( T ) , nothing to do. We will denote x’s parent by y and x’s grandparent by z. Figure: Splay (T, a)

Slide 7

Slide 7 text

Introduction Operation Time Analysis Applications Conclusion Case A : Zig Step (Zig) : The basis step, where y = root ( T ) A trivial tree rotation between x and y. 1 Rotate the edge ( x, y ) . Figure: Zig

Slide 8

Slide 8 text

Introduction Operation Time Analysis Applications Conclusion Case B : Zig-zig Step (Zig-zig) : if x and y are either both left or both right childrens 1 Let z be the parent of y. 2 Rotate the edge ( y, z ) , making y a root. 3 Rotate the edge ( x, y ) , making x a root. Figure: Zig-zig

Slide 9

Slide 9 text

Introduction Operation Time Analysis Applications Conclusion Case C : Zig-zag Step (Zig-zag) : if x is a right child and y is a left child, or vice versa 1 Let z be the parent of y. 2 Rotate the edge ( x, y ) , making x a child of z. 3 Rotate the edge ( x, z ) , making x a root. Figure: Zig-zag

Slide 10

Slide 10 text

Introduction Operation Time Analysis Applications Conclusion Find Find( T, k ) : Find the node in T with key value k. 1 Find the node x with key k, as with traditional BST. 2 If x is found, Splay( T, x ) and return x. 3 If no such key, splay at the last non- Nil node, and return Nil . Cost : O ( h ) . Unfortunately, the worst case is O ( n ) . Figure: Find (T, 20 ) Figure: Find (T, 80 )

Slide 11

Slide 11 text

Introduction Operation Time Analysis Applications Conclusion Split Split( T, k ) : Split the tree T and return two trees T 1 , T 2 such that key ( x )  k for all x in T 1 and key ( y ) > k for all y in T 1 . 1 Find( T, k ) . Then the root becomes a successor, a precedessor, or of key k. 2 If root ( T )  k, break the right child and return two trees. 3 If root ( T ) > k, break the left child and return two trees. Cost : O ( h ) . Figure: Split (T, 40 )

Slide 12

Slide 12 text

Introduction Operation Time Analysis Applications Conclusion Join Join( T 1 , T 2) : Merge T 1 and T 2 to obtain a tree T. Assumed key ( x ) < key ( y ) for all x 2 T 1 , y 2 T 2 . 1 Splay(Max( T 1)) . 2 Now the root of T 1 has only one (left) child. Let the root ( T 2) be the right child of root ( T 1) . 3 Or, alternatively, splay Min( T 2) and concatenate. Cost : O (min {h ( T 1) , h ( T 2) } ) . The worst case is linear. Figure: Join (T1 , T2 )

Slide 13

Slide 13 text

Introduction Operation Time Analysis Applications Conclusion Insert (Using Split) Insert( T, k ) : insert a new (single) node with key k into T. 1 Do Split( T, k ) . Let T 1 , T 2 be the returned trees. 2 Replace T by a new node x with key k, letting T 1 and T 2 be its two subtree. (Handle separately the case key ( root ( T 1)) = key ( x ) ) Figure: Insert (T, 45 )

Slide 14

Slide 14 text

Introduction Operation Time Analysis Applications Conclusion Insert (Alternative) Insert( T, x ) : insert a new node x into T. 1 Insert x as with traditional BST. 2 Do Splay( T, x ) . Figure: Insert 0(T, 80 )

Slide 15

Slide 15 text

Introduction Operation Time Analysis Applications Conclusion Delete (Using Join) Delete( T, x ) : delete the node x from T. 1 Do Splay( T, x ) . Now x = root ( T ) . 2 If x has less than two children, we are done. 3 If there are two children, let T 1 , T 2 be the two subtrees. 4 Do Join( T 1 , T 2) . Figure: Delete (T, 40 )

Slide 16

Slide 16 text

Introduction Operation Time Analysis Applications Conclusion Delete (Alternative) Delete( T, x ) : delete the node x from T. 1 If x has less than two children: we can easily delete x. 2 If x has two children: join x’s two child subtrees, and replace x by the resulting joined tree. Figure: Delete 0(T, 30 )

Slide 17

Slide 17 text

Introduction Operation Time Analysis Applications Conclusion A Potential Analysis Model weight w ( x ) : a fixed value assigned for each node assigned by theorem-provers: NOT a property of tree

Slide 18

Slide 18 text

Introduction Operation Time Analysis Applications Conclusion A Potential Analysis Model weight w ( x ) : a fixed value assigned for each node assigned by theorem-provers: NOT a property of tree size s ( x ) : the sum of all weights in the subtree Tx rooted at x that is, s ( x ) = X i 2 Tx w ( i )

Slide 19

Slide 19 text

Introduction Operation Time Analysis Applications Conclusion A Potential Analysis Model weight w ( x ) : a fixed value assigned for each node assigned by theorem-provers: NOT a property of tree size s ( x ) : the sum of all weights in the subtree Tx rooted at x that is, s ( x ) = X i 2 Tx w ( i ) rank r ( x ) := log s ( x )

Slide 20

Slide 20 text

Introduction Operation Time Analysis Applications Conclusion A Potential Analysis Model weight w ( x ) : a fixed value assigned for each node assigned by theorem-provers: NOT a property of tree size s ( x ) : the sum of all weights in the subtree Tx rooted at x that is, s ( x ) = X i 2 Tx w ( i ) rank r ( x ) := log s ( x ) potential ( T ) : the sum of all ranks in T that is, ( T ) = X i 2 T r ( i ) Note that the amortized cost is (actual cost) +

Slide 21

Slide 21 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma Lemma (Access Lemma) Let t = root ( T ) . The amortized cost ↵ of Splay( T, x ) is at most 3( r ( t ) r ( x )) + 1 = O ✓ log s ( t ) s ( x ) ◆ .

Slide 22

Slide 22 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma Lemma (Access Lemma) Let t = root ( T ) . The amortized cost ↵ of Splay( T, x ) is at most 3( r ( t ) r ( x )) + 1 = O ✓ log s ( t ) s ( x ) ◆ . Proof . For each rotation operation, let s and t (s0 and t0) denote the size and rank functions just before (just after) the operation, respectively.

Slide 23

Slide 23 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma Lemma (Access Lemma) Let t = root ( T ) . The amortized cost ↵ of Splay( T, x ) is at most 3( r ( t ) r ( x )) + 1 = O ✓ log s ( t ) s ( x ) ◆ . Proof . For each rotation operation, let s and t (s0 and t0) denote the size and rank functions just before (just after) the operation, respectively. We will show: (Zig Step) ↵  3( r0 ( x ) r ( x )) + 1 (Zig-zig, Zig-zag Step) ↵  3( r0 ( x ) r ( x )) .

Slide 24

Slide 24 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1

Slide 25

Slide 25 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . x y A B C x y A B C

Slide 26

Slide 26 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . The actual cost is 1 (only one rotation). Therefore ↵ = 1 + 0 ( T ) ( T )

Slide 27

Slide 27 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . The actual cost is 1 (only one rotation). Therefore ↵ = 1 + 0 ( T ) ( T ) = 1 + r0 ( x ) + r0 ( y ) r ( x ) r ( y )

Slide 28

Slide 28 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . The actual cost is 1 (only one rotation). Therefore ↵ = 1 + 0 ( T ) ( T ) = 1 + r0 ( x ) + r0 ( y ) r ( x ) r ( y )  1 + r0 ( x ) r ( x )

Slide 29

Slide 29 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) + 1 Note that only x and y can change their rank. Specifically, rank of y decreased and rank of x increased, that is, r ( y ) r0 ( y ) and r0 ( x ) r ( x ) . The actual cost is 1 (only one rotation). Therefore ↵ = 1 + 0 ( T ) ( T ) = 1 + r0 ( x ) + r0 ( y ) r ( x ) r ( y )  1 + r0 ( x ) r ( x )  1 + 3( r0 ( x ) r ( x )) .

Slide 30

Slide 30 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x ))

Slide 31

Slide 31 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) x y z A B C D x y z A B C D

Slide 32

Slide 32 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) There are 2 tree rotations. ↵ = 2 + 0 ( T ) ( T )

Slide 33

Slide 33 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) There are 2 tree rotations. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z )

Slide 34

Slide 34 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) There are 2 tree rotations. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z ) = 2 + r0 ( y ) + r0 ( z ) r ( x ) r ( y )

Slide 35

Slide 35 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Note that only x, y and z can change their rank. Observation: r0 ( x ) = r ( z ) (x goes to root) r0 ( x ) r0 ( y ) r ( y ) r ( x ) There are 2 tree rotations. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z ) = 2 + r0 ( y ) + r0 ( z ) r ( x ) r ( y )  2 + r0 ( x ) + r0 ( z ) 2 r ( x )

Slide 36

Slide 36 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x ))

Slide 37

Slide 37 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Since log ↵ + log  2 whenever ↵ +  1 , r ( x ) + r0 ( z ) 2 r0 ( x ) = ( r ( x ) r0 ( x )) + ( r0 ( z ) r0 ( x )) = log ✓ s ( x ) s0 ( x ) ◆ + log ✓ s0 ( z ) s0 ( x ) ◆  2 . Note that s ( x ) + s0 ( z )  s0 ( x ) .

Slide 38

Slide 38 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zig Step) Claim : amortized cost  3( r0 ( x ) r ( x )) Since log ↵ + log  2 whenever ↵ +  1 , r ( x ) + r0 ( z ) 2 r0 ( x ) = ( r ( x ) r0 ( x )) + ( r0 ( z ) r0 ( x )) = log ✓ s ( x ) s0 ( x ) ◆ + log ✓ s0 ( z ) s0 ( x ) ◆  2 . Note that s ( x ) + s0 ( z )  s0 ( x ) . Therefore ↵  2 + r0 ( x ) + r0 ( z ) 2 r ( x )  ( r ( x ) r0 ( z ) + 2 r0 ( x )) + ( r0 ( x ) + r0 ( z ) 2 r ( x ))  3( r0 ( x ) r ( x )) .

Slide 39

Slide 39 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zag Step) Claim : amortized cost  3( r0 ( x ) r ( x ))

Slide 40

Slide 40 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zag Step) Claim : amortized cost  3( r0 ( x ) r ( x )) We observe that r0 ( x ) = r ( z ) and r ( x )  r ( y ) . x y z A B C D x y z A B C D

Slide 41

Slide 41 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zag Step) Claim : amortized cost  3( r0 ( x ) r ( x )) We observe that r0 ( x ) = r ( z ) and r ( x )  r ( y ) . Also 2 tree rotation is needed. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z )  2 + r0 ( y ) + r0 ( z ) 2 r ( x )

Slide 42

Slide 42 text

Introduction Operation Time Analysis Applications Conclusion A Key Lemma : Access Lemma (Zig-zag Step) Claim : amortized cost  3( r0 ( x ) r ( x )) We observe that r0 ( x ) = r ( z ) and r ( x )  r ( y ) . Also 2 tree rotation is needed. ↵ = 2 + 0 ( T ) ( T ) = 2 + r0 ( x ) + r0 ( y ) + r0 ( z ) r ( x ) r ( y ) r ( z )  2 + r0 ( y ) + r0 ( z ) 2 r ( x ) Similarily as (Zig-zig step), one can easily show ↵  3 r0 ( x ) r ( x ) . Therefore the lemma is proved.

Slide 43

Slide 43 text

Introduction Operation Time Analysis Applications Conclusion Balance Theorem Theorem (Balance Theorem) The total access time of m accesses on an n-node splay tree is O (( m + n ) log n + m ) .

Slide 44

Slide 44 text

Introduction Operation Time Analysis Applications Conclusion Balance Theorem Theorem (Balance Theorem) The total access time of m accesses on an n-node splay tree is O (( m + n ) log n + m ) . Proof . Assign w ( x ) = 1 /n for each x 2 T. The amortized cost of accessing each node is at most 3 log n + 1 . The net potential drop is at most P x log( W/w ( x )) , where W = P x w ( x ) , which is n log n (W = 1 ). Therefore the total time is O ( m (log n + 1)) + O ( n log n ) .

Slide 45

Slide 45 text

Introduction Operation Time Analysis Applications Conclusion Balance Theorem Theorem (Balance Theorem) The total access time of m accesses on an n-node splay tree is O (( m + n ) log n + m ) . Yet, we didn’t count up for insert/delete operations. Implication : Accessing a splay tree is as e cient as any uniformly balanced binary tree.

Slide 46

Slide 46 text

Introduction Operation Time Analysis Applications Conclusion Static Optimality Theorem Theorem (Static Optimality Theorem) Let f ( x ) be the access frequency (the # of access) of node x, where f ( x ) 1 for all x. Then the total access time is O m + X x2T f ( x ) log ✓ m f ( x ) ◆! .

Slide 47

Slide 47 text

Introduction Operation Time Analysis Applications Conclusion Static Optimality Theorem Theorem (Static Optimality Theorem) Let f ( x ) be the access frequency (the # of access) of node x, where f ( x ) 1 for all x. Then the total access time is O m + X x2T f ( x ) log ✓ m f ( x ) ◆! . Proof . Assign w ( x ) = f ( x ) /m for each x 2 T. Clearly W = 1 and the amortized cost of accessing each node is O (log( m/f ( x ))) . The net poential drop is at most P x2T log( m/f ( x )) .

Slide 48

Slide 48 text

Introduction Operation Time Analysis Applications Conclusion Static Optimality Theorem Theorem (Static Optimality Theorem) Let f ( x ) be the access frequency (the # of access) of node x, where f ( x ) 1 for all x. Then the total access time is O m + X x2T f ( x ) log ✓ m f ( x ) ◆! . Implication : Accessing a splay tree is statically optimal. It is as e cient as any fixed search tree, given the access frequency. Specially, as e cient as an optimal one (e.z. Hu↵man Tree).

Slide 49

Slide 49 text

Introduction Operation Time Analysis Applications Conclusion Working Set Theorem Theorem (Working Set Theorem) Let tj be the number of distinct elements accessed, between j-th access and the previous time the element was accessed (or the beginning). Then the total access time of m access is O 0 @m + m X j =1 log( tj + 1) 1 A .

Slide 50

Slide 50 text

Introduction Operation Time Analysis Applications Conclusion Working Set Theorem Theorem (Working Set Theorem) Let tj be the number of distinct elements accessed, between j-th access and the previous time the element was accessed (or the beginning). Then the total access time of m access is O 0 @m + m X j =1 log( tj + 1) 1 A . Sketch of Proof . Assign the weights 1 , 1 / 4 , · · · , 1 /n2 to the items, in order by their first access. After j-th access on node x with w ( x ) = 1 /k2, reassign w ( x ) = 1 and w ( y ) = 1 / ( k0 + 1) 2 for all y such that w ( y ) = 1 / ( k0 ) 2 > 1 /k2. Since P n k =1 1 /k2 = ⇡2/ 6 = O (1) , the amortized time for j-th access is O (log( t j + 1)) . The weight reassignment can only decrease the potential, hence non-positive amortized time for weight reassignment. The net potential drop is O ( n log n ) .

Slide 51

Slide 51 text

Introduction Operation Time Analysis Applications Conclusion Working Set Theorem Theorem (Working Set Theorem) Let tj be the number of distinct elements accessed, between j-th access and the previous time the element was accessed (or the beginning). Then the total access time of m access is O 0 @m + m X j =1 log( tj + 1) 1 A . Implication : The most recently accessed items are the easiest to access. The tree work as a “working set”.

Slide 52

Slide 52 text

Introduction Operation Time Analysis Applications Conclusion Another Key Lemma : Update Lemma Lemma (Update Lemma) Let W be the total weight of the nodes in the tree(s) involved in the operation. Then the amortized times of the splay tree operations have the following upper bounds: Find( T, k ) : ( 3 log( W/w ( x )) + 1 if T has a node x with key k 3 log( W/w ( x0 )) + 1 otherwise Join( T1 , T2) : 3 log( W/w ( x )) + O (1) , where x = Max( T1) . Split( T, x ) : ( 3 log( W/w ( x )) + O (1) if T has a node x with key k 3 log( W/w ( x0 )) + O (1) otherwise Here x0 is either x or x+ , such that w ( x0 ) = min {w ( x ) , w ( x+) }.

Slide 53

Slide 53 text

Introduction Operation Time Analysis Applications Conclusion Update Lemma : Proof Proof. Find( T, k ) : ( 3 log( W/w ( x )) + 1 if x 2 T, key ( x ) = k 3 log( W/w ( x0 )) + 1 if x 62 T where x0 2 {x , x+} with w ( x0 ) = min {w ( x ) , w ( x+) }. Let v be the node at which the tree is splayed. Access Lemma says the amortized time is  3 log( s ( t ) /s ( v )) + 1 . In case of successful search, v = x and s ( v ) w ( x ) . In case of failed search, s ( v ) min {w ( x ) , w ( x+ ) } because either of x or x+ was in T v (before splaying). It is obvious s ( t ) = W. We are done.

Slide 54

Slide 54 text

Introduction Operation Time Analysis Applications Conclusion Update Lemma : Proof Split( T, k ) : ( 3 log( W/w ( x )) + O (1) if x 2 T, key ( x ) = k 3 log( W/w ( x0 )) + O (1) otherwise Immediate from the result of Access . Join( T 1 , T 2) : 3 log( W/w ( x )) + O (1) , where x = Max( T 1) . A successful search of Max( T1) in T1 : bounded by 3 log( s ( t1) /w ( x )) + 1 . Potential increase by joining is log ✓ s ( t1) + s ( t2) s ( t1) ◆  3 log ✓ W s ( t1) ◆ . Hence the total cost is at most 3 log ✓ s ( t1) w ( x ) · W s ( t1) ◆ + 1 = 3 log ✓ W w ( x ) ◆ + O (1) .

Slide 55

Slide 55 text

Introduction Operation Time Analysis Applications Conclusion Update Lemma (Insert, Delete) Lemma (Update Lemma (cont’d) ) Let W be the total weight of the nodes in the tree(s) involved in the operation. Then the amortized times of the splay tree operations have the following upper bounds: Insert( T, x ) : 3 log ⇣ W w ( x ) w ( x 0) ⌘ + log ⇣ W w ( x ) ⌘ + O (1) Delete( T, x ) : 3 log ⇣ W w ( x ) ⌘ + 3 log ⇣ W w ( x ) w ( x ) ⌘ + O (1) Insert( T, x ) alternative : 2 log ⇣ W w ( x ) w ( x 0) ⌘ + log ⇣ W w ( x ) ⌘ + O (1) Delete( T, x ) alternative : 2 log ⇣ W w ( x ) w ( x 0) ⌘ + O (1) Here x0 is either x or x+ , such that w ( x0 ) = min {w ( x ) , w ( x+) }. Proof . Straightforward from the bounds shown above.

Slide 56

Slide 56 text

Introduction Operation Time Analysis Applications Conclusion Balance Theorem with Updates Theorem (Balance Theorem with Updates) A sequence of m arbitrary operations on a splay tree T, starting from the empty one, takes O ( m + Pm j =1 log nj ) , where nj is the number of nodes in the tree(s) involved in j-th operation. Proof . Assign w ( x ) = 1 for each x 2 T, then the theorem is immediate from the lemma.

Slide 57

Slide 57 text

Introduction Operation Time Analysis Applications Conclusion Balance Theorem with Updates Theorem (Balance Theorem with Updates) A sequence of m arbitrary operations on a splay tree T, starting from the empty one, takes O ( m + Pm j =1 log nj ) , where nj is the number of nodes in the tree(s) involved in j-th operation. Proof . Assign w ( x ) = 1 for each x 2 T, then the theorem is immediate from the lemma. Implication : Amortized costs for basic BST operations of a splay tree is O (log n ) .

Slide 58

Slide 58 text

Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree.

Slide 59

Slide 59 text

Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. That is, a disjoint-set data structure with cut operation.

Slide 60

Slide 60 text

Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. That is, a disjoint-set data structure with cut operation. Sleator and Tarjan had invented Link-Cut Tree which supports the above operations in amortized O (log n ) time.

Slide 61

Slide 61 text

Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. That is, a disjoint-set data structure with cut operation. Sleator and Tarjan had invented Link-Cut Tree which supports the above operations in amortized O (log n ) time. Using splay tree, we can implement a dynamic tree in rather simple manner, in amortized O (log n ) time.

Slide 62

Slide 62 text

Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Problem : How fast can we do the following dynamic rooted-tree operations? Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. Cost( v ) returns the cost of node v. Min( v ) returns the node of minimum cost on the path from v to Root( v ) . Add( v, c ) adds c to the cost of every node on the path from v to Root( v ) .

Slide 63

Slide 63 text

Introduction Operation Time Analysis Applications Conclusion Dynamic Tree Problem Amortized O (log n ) is possible. Using this, for example, Maximum Flow is solvable in O ( nm log n ) instead of O ( n2 m ) . Root( v ) returns the root of the tree that contains v. Link( u, v ) adds an edge from u to v. Assumed u is a root of one tree and v is in another tree. Cut( v ) deletes the edge from v to its parent. Assumed v is not a root of one tree. Cost( v ) returns the cost of node v. Min( v ) returns the node of minimum cost on the path from v to Root( v ) . Add( v, c ) adds c to the cost of every node on the path from v to Root( v ) .

Slide 64

Slide 64 text

Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree Each node has at most one child (solid child) connected with solid edge. The others are connected with dashed edge. This heuristic exploits an locality of access, hence O (log n ) time. a b c d e f g h i j k l m n o p q r s t u v w access s !

Slide 65

Slide 65 text

Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree Each node has at most one child (solid child) connected with solid edge. The others are connected with dashed edge. This heuristic exploits an locality of access, hence O (log n ) time. a b c d e f g h i j k l m n o p q r s t u v w access s ! a b c d e f g h i j k l m n o p q r s t u v w

Slide 66

Slide 66 text

Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree : Virtual Tree Nodes on each maximal solid path forms a solid subtree (splay tree), where higher node in the original tree has greater key. a b c d e f g h i j k l m n o p q r s t u v w

Slide 67

Slide 67 text

Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree : Virtual Tree Nodes on each maximal solid path forms a solid subtree (splay tree), where higher node in the original tree has greater key. a b c d e f g h i j k l m n o p q r s t u v w Virtual Tree V f l b q p i c a j r g d m v w t u s h k e o n

Slide 68

Slide 68 text

Introduction Operation Time Analysis Applications Conclusion Representation of Dynamic Tree : Virtual Tree Each dashed edge ( x, p ) in T corresponds to an edge ( r, p ) in V , where r is the root of a solid subtree containing x. a b c d e f g h i j k l m n o p q r s t u v w Virtual Tree V f l b q p i c a j r g d m v w t u s h k e o n

Slide 69

Slide 69 text

Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access ) We have to restruct the virtual tree V , as if v was accessed in the original tree T. Note that solid paths and solid subtrees change. Access operation in Dynamic Tree, which encapsulates Splay operation in (a subtree of) Virtual tree

Slide 70

Slide 70 text

Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access ) After accessing x, the subpath (of the solid path containing x) below x is separated. d g j m r s t u v w access s ! d g j m r s t u v w

Slide 71

Slide 71 text

Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access ) After accessing x, the subpath (of the solid path containing x) below x is separated. Motivation : In the virtual tree, if x is a root of a solid subtree, we just cut the left subtree of x. d g j m r s t u v w access s ! d g j m r s t u v w Virtual Tree j r g d m v w t u s

Slide 72

Slide 72 text

Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access ) After accessing x, the subpath (of the solid path containing x) below x is separated. Motivation : In the virtual tree, if x is a root of a solid subtree, we just cut the left subtree of x. Splay and d g j m r s t u v w access s ! d g j m r s t u v w Splayed at s j r g d m s t v w u

Slide 73

Slide 73 text

Introduction Operation Time Analysis Applications Conclusion Operation: Splay ( Access ) After accessing x, the subpath (of the solid path containing x) below x is separated. Motivation : In the virtual tree, if x is a root of a solid subtree, we just cut the left subtree of x. Splay and cut the left child! d g j m r s t u v w access s ! d g j m r s t u v w Splayed at s j r g d m s t v w u

Slide 74

Slide 74 text

Introduction Operation Time Analysis Applications Conclusion Operation: Access Remark : Access in dynamic tree is just doing Splay within the entire virtual tree. Please do not confuse ‘splaying’ in a virtual tree with ‘splaying’ in a solid subtree, which is just a subroutine.

Slide 75

Slide 75 text

Introduction Operation Time Analysis Applications Conclusion Operation: Access Splay( V, x ) 1 Splay at x within its solid subtree, and move up to the parent of x via a dahsed edge, splay again, move up, and so on · · · , until we reach the root. 2 Walk again from x up to the root, making x ’s each of ancestors a left child. Here, solid edges are changed. 3 Finally splay at x within its new solid subtree. Figure: Splay Step1

Slide 76

Slide 76 text

Introduction Operation Time Analysis Applications Conclusion Operation: Access Splay( V, x ) 1 Splay at x within its solid subtree, and move up to the parent of x via a dahsed edge, splay again, move up, and so on · · · , until we reach the root. 2 Walk again from x up to the root, making x ’s each of ancestors a left child. Here, solid edges are changed. 3 Finally splay at x within its new solid subtree. Figure: Splay Step2

Slide 77

Slide 77 text

Introduction Operation Time Analysis Applications Conclusion Operation: Access Splay( V, x ) 1 Splay at x within its solid subtree, and move up to the parent of x via a dahsed edge, splay again, move up, and so on · · · , until we reach the root. 2 Walk again from x up to the root, making x ’s each of ancestors a left child. Here, solid edges are changed. 3 Finally splay at x within its new solid subtree. Figure: Splay Step3

Slide 78

Slide 78 text

Introduction Operation Time Analysis Applications Conclusion Operation: Access in one-pass Splay( V, x ) 1 Splay at x within its solid-subtree. 2 Cut the left-subtree of x . 3 Until x becomes the root of V : 1 p the parent of x’s solid-subtree (via dashed edge) 2 Splay at p within p’s solid-subtree. 3 Switch p’s solid child as x. 4 x p and repeat! 4 Splay at x .

Slide 79

Slide 79 text

Introduction Operation Time Analysis Applications Conclusion Operation: Root Root( v ) returns the root of the tree that contains v. 1 Splay at v. Now v is the root of the entire virtual tree V . 2 Find the maximum-key node r in v’s solid tree, following right-links. 3 Splay at r, in its solid tree, and return r. a b c e f h i k l n o p q Virtual Tree V f l b q p i c a h k e o n

Slide 80

Slide 80 text

Introduction Operation Time Analysis Applications Conclusion Operation: Root Root( v ) returns the root of the tree that contains v. 1 Splay at v. Now v is the root of the entire virtual tree V . 2 Find the maximum-key node r in v’s solid tree, following right-links. 3 Splay at r, in its solid tree, and return r. a b c e f h i k l n o p q Virtual Tree V f l b q p i c a h k e o n

Slide 81

Slide 81 text

Introduction Operation Time Analysis Applications Conclusion Operation: Link Link( u, v ) adds an edge from u to v (u is a root in T). 1 Splay at u. Now u is a root of u’s virtual tree. 2 Connect u to v via a dashed edge. That is, p ( u ) v. a b c e f h i k l n o p q Link ( e, c ) Virtual Tree V f l b q p i c a h k e o n splay at e !

Slide 82

Slide 82 text

Introduction Operation Time Analysis Applications Conclusion Operation: Link Link( u, v ) adds an edge from u to v (u is a root in T). 1 Splay at u. Now u is a root of u’s virtual tree. 2 Connect u to v via a dashed edge. That is, p ( u ) v. a b c e f h i k l n o p q Link ( e, c ) Virtual Tree V f l b q p i c a e h k o n

Slide 83

Slide 83 text

Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V First, let’s splay at e . f l q p i c a b e h k o n

Slide 84

Slide 84 text

Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V After first pass, making c root f l q p i c a b e h k o n

Slide 85

Slide 85 text

Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V After second pass, making e solid child f l q p i c a b e h k o n

Slide 86

Slide 86 text

Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V After third pass, making e root f l q p i c b a e h k o n

Slide 87

Slide 87 text

Introduction Operation Time Analysis Applications Conclusion Operation: Cut Cut( v ) cuts the edge from v to its parent. (v is NOT a root in T) 1 Splay at v, making v the root of the entire virtual tree V . 2 Disconnect v and right ( v ) . a b c e f h i k l n o p q Cut ( e ) Virtual Tree V Finally ( e, c ) is cutted. f l q p i c b a e h k o n

Slide 88

Slide 88 text

Introduction Operation Time Analysis Applications Conclusion Time Complexity All the operations are done in amortized O (log n ) time. Another potential analysis : in the virtual tree V , weight w ( x ) = 1 for each node x 2 V size s ( x ) := the number of its descendants (the sum of weight), including subtrees connected via dashed edges. rank r ( x ) := log s ( x ) (binary logarithm) potential ( V ) := 2 P x in V r ( x ) A single splay operation costs amortized O (log n ) . The first pass costs 6 log n + k, where k is the depth of x after the first pass (the number of zig cases). The second pass costs zero since there is no rotations. For the third pass, each of the k rotations is charged two to take account for k rotations in the first pass, hence 6 log n + 2 . Now the total amortized time is O (log n ) .

Slide 89

Slide 89 text

Introduction Operation Time Analysis Applications Conclusion Summary of Splay Tree Amortized O (log n ) BST operations (O ( n ) worst) Splay operation : the last-accessed node goes to root. Read-only access might change the structure (NOT thread-safe) As well as basic BST operations, there are various time bounds on some access patterns Various applications, such as O (log n ) Dynamic Tree

Slide 90

Slide 90 text

Introduction Operation Time Analysis Applications Conclusion Summary of Splay Tree Amortized O (log n ) BST operations (O ( n ) worst) Splay operation : the last-accessed node goes to root. Read-only access might change the structure (NOT thread-safe) As well as basic BST operations, there are various time bounds on some access patterns Various applications, such as O (log n ) Dynamic Tree Theoretical time bound is as good as other search trees There are a little gap on practical performance within a constant factor, but splay tree is competitive and highly simple to write a code In some access patterns, quite faster than RB-tree due to the access locality

Slide 91

Slide 91 text

Introduction Operation Time Analysis Applications Conclusion Some Conjectures On Splay Tree Dynamic Optimality Conjecture : Splay tree is dynamically optimal, that is, O (1) -competitive to an optimal o✏ine binary search tree. Open as of now. Splay tree (Sleator and Tarjan, 1985) is proved to be O (log n ) -competitive. Currently best-known competitiveness bound is O (log log n ) . Tango tree (Demaine et al., 2004) and Multi-splay tree (Wang et al., 2006) are O (log log n ) -competitive.

Slide 92

Slide 92 text

Introduction Operation Time Analysis Applications Conclusion Some Conjectures On Splay Tree Dynamic Optimality Conjecture : Splay tree is dynamically optimal, that is, O (1) -competitive to an optimal o✏ine binary search tree. Traversal Conjecture : If T 1 , T 2 are two n-node splay trees containing exactly the same items, then accessing items of T 1 in order of pre-order traversal of T 2 costs O ( n ) .

Slide 93

Slide 93 text

Introduction Operation Time Analysis Applications Conclusion Some Conjectures On Splay Tree Dynamic Optimality Conjecture : Splay tree is dynamically optimal, that is, O (1) -competitive to an optimal o✏ine binary search tree. Traversal Conjecture : If T 1 , T 2 are two n-node splay trees containing exactly the same items, then accessing items of T 1 in order of pre-order traversal of T 2 costs O ( n ) . Split Conjecture : The cost of any ordering of deleting all the n elements is O ( n ) .

Slide 94

Slide 94 text

Introduction Operation Time Analysis Applications Conclusion Some Conjectures On Splay Tree Dynamic Optimality Conjecture : Splay tree is dynamically optimal, that is, O (1) -competitive to an optimal o✏ine binary search tree. Traversal Conjecture : If T 1 , T 2 are two n-node splay trees containing exactly the same items, then accessing items of T 1 in order of pre-order traversal of T 2 costs O ( n ) . Split Conjecture : The cost of any ordering of deleting all the n elements is O ( n ) . Deque Conjecture : The cost of performing m double-ended queue operations is O ( m + n ) . ( () amortized O (1) )

Slide 95

Slide 95 text

Introduction Operation Time Analysis Applications Conclusion The End Thank You Very Much Any Questions?