today
play

Today Amortized analysis Dynamic tables Amortized Analysis and - PowerPoint PPT Presentation

Today Amortized analysis Dynamic tables Amortized Analysis and Splay Trees Splay trees Inge Li Grtz CLRS Chapter 17 Je ff Erickson notes Dynamic tables Dynamic tables Problem. Have to assign size of table at initialization.


  1. Today • Amortized analysis • Dynamic tables Amortized Analysis and Splay Trees • Splay trees Inge Li Gørtz CLRS Chapter 17 Je ff Erickson notes Dynamic tables Dynamic tables • Problem. Have to assign size of table at initialization. • First attempt. • Goal. Only use space Θ (n) for an array with n elements. • Insert: • Applications. Stacks, queues, hash tables,…. • Create a new table of size n+1. • Move all elements to the new table. • Delete old table. • Size of table = number of elements • Too expensive. • Have to copy all elements to a new array each time. • Insertion of N elements takes time proportional to: 1 + 2 + ······ + n = Θ (n 2 ). • Goal. Ensure size of array does not change to often. 3 4

  2. Dynamic tables Dynamic tables • Doubling. If the array is full (number of elements equal to size of array) copy • Halving. If the array is a quarter full copy the elements to a new array of half the elements to a new array of double size. the size. 3 3 5 1 7 8 3 5 3 5 1 7 3 5 1 3 5 1 7 3 5 1 7 8 2 3 4 3 5 1 7 8 3 5 1 7 8 2 3 3 5 1 7 8 2 3 5 1 7 8 2 3 4 6 • Consequence. Insertion of n elements take time: • Consequence. The array is always between 25% and 100% full. • n + number of reinsertions = n + 1 + 2 + 4 + 8 + ···· + 2 log n < 3n. • Space: Θ (n). 5 6 Amortized Analysis Summation (Aggregate) method • Methods. • Summation. • Summation (aggregate) method • Determine total cost. • Accounting (tax) method • Amortized cost = total cost/#operations. • Potential method • Analysis of doubling strategy: • Total cost: n + 1 + 2 + 4 + ... + 2 log n = Θ (n). • Amortized cost per insert: Θ (1).

  3. Dynamic Tables: Accounting Method Accounting method • Analysis: Allocate 2 credits to each element when inserted. • Accounting/taxation. • All elements in the array that is beyond the middle have 2 credits. • Some types of operations are overcharged (taxed). • Table not full: insert costs 1, and we have 2 credits to save. • Amortized cost of an operation is what we charge for an operation. • table full, i.e., doubling: half of the elements have 2 credits each. Use these • Credit allocated with elements in the data structure can be used to pay for to pay for reinsertion of all in the new array. later operations (only in analysis, not actually stored in data structure!). • Amortized cost per operation: 3. • Total credit must be non-negative at all times. ! ! ! ! ! ! ! ! • => Total amortized cost an upper bound on the actual cost. x x x x x x x x x x x x x x x x x x x x x x x ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x Potential method Dynamic tables • Potential functions. • Doubling. If the table is full (number of elements equal to size of array) copy the elements to a new array of double size. • Prepaid credit (potential) associated with the data structure (money in the • Halving. If the table is a quarter full copy the elements to a new array of half bank). the size • Ensure there is always enough “money in the bank” (positive potential). • Potential function. ( • Amortized cost of an operation: actual cost plus change in potential. 2 n − L if T at least half full • Φ (D i ) = L/ 2 − n if T less than half full • L = current array size, n = number of elements in array. • Inserting when less than half full and still less than half full after insertion: n = 6, L = 16 n = 7, L = 16 ! x x x x x x x x x x x x x ! • amortized cost = 1 + - ! = 0

  4. Dynamic tables Dynamic tables • Doubling. If the table is full (number of elements equal to size of array) copy • Doubling. If the table is full (number of elements equal to size of array) copy the elements to a new array of double size. the elements to a new array of double size. • Halving. If the table is a quarter full copy the elements to a new array of half • Halving. If the table is a quarter full copy the elements to a new array of half the size the size • Potential function. • Potential function. ( ( 2 n − L if T at least half full 2 n − L if T at least half full • Φ (D i ) = • Φ (D i ) = L/ 2 − n if T less than half full L/ 2 − n if T less than half full • L = current array size, n = number of elements in array. • L = current array size, n = number of elements in array. • Inserting when less than half full before and half full after: • Inserting when half full, but not full: n = 11, L = 16 n = 12, L = 16 n = 8, L = 16 n = 7, L = 16 ! ! ! ! x x x x x x x x x x x x x x x ! x x x x x x x x x x x x x x x x x x x x x x x ! ! ! ! ! = 3 • amortized cost = 1 + - ! = 0 • amortized cost = 1 + ! Dynamic tables Dynamic tables • Doubling. If the table is full (number of elements equal to size of array) copy • Doubling. If the table is full (number of elements equal to size of array) copy the elements to a new array of double size. the elements to a new array of double size. • Halving. If the table is a quarter full copy the elements to a new array of half • Halving. If the table is a quarter full copy the elements to a new array of half the size the size • Potential function. • Potential function. ( ( 2 n − L if T at least half full 2 n − L if T at least half full • Φ (D i ) = • Φ (D i ) = L/ 2 − n L/ 2 − n if T less than half full if T less than half full • L = current array size, n = number of elements in array. • L = current array size, n = number of elements in array. • Inserting in full table and doubling • Deleting in a quarter full table and halving n = 9, L = 16 n = 8, L = 8 n = 4, L = 16 n = 3, L = 8 x x x x x x x x x x x x ! ! ! ! ! ! ! ! x x x x x x x x x ! ! ! ! x x x • amortized cost = 9 + • amortized cost = 3 + - ! ! ! = 3 - = 0 ! ! ! ! ! !

  5. Splay Trees • Self-adjusting BST (Sleator-Tarjan 1983). • Most frequently accessed nodes are close to the root. • Tree reorganizes itself after each operation. • After access to a node it is moved to the root by splay operation. Splay Trees • Worst case time for insertion, deletion and search is O(n). Amortised time per operation O(log n). • Operations. Search, predecessor, sucessor, max, min, insert, delete, join. Splaying Splaying • Splay(x): do following rotations until x is the root. Let y be the parent of x. • Splay(x): do following rotations until x is the root. Let p(x) be the parent of x. • right (or left): if x has no grandparent. • right (or left): if x has no grandparent. • zig-zag (or zag-zig): if one of x,p(x) is a left child and the other is a right child. z z x x y w x w z right y x d d x w a c left a c a b c d a b b c b c a b right rotation at x (and left rotation at y) zig-zag at x

  6. Splaying Splaying zig-zag at x • Splay(x): do following rotations until x is the root. Let y be the parent of x. • right (or left): if x has no grandparent. z z x w x w z • zig-zag (or zag-zig): if one of x,y is a left child and the other is a right child. d d x w • roller-coaster: if x and p(x) are either both left children or both right a c a b c d children. c a b b z y x right roller-coaster at x (and left roller-coaster at z) y x z y y x z d a z x y x z y c d a b c b d a z x a b c d a b c d c b right roller-coaster at x (and left roller-coaster at z) a b c d Splay Splay • Example. Splay(1) • Example. Splay(1) 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 1 2 2 1 3 right roller-coaster at 1 right roller-coaster at 1

  7. Splay Splay • Example. Splay(1) • Example. Splay(1) 10 10 9 9 8 8 7 1 6 6 1 4 7 4 2 5 2 5 3 3 right roller-coaster at 1 right roller-coaster at 1 Splay Splay • Example. Splay(1) • Example. Splay(1) 10 10 1 1 8 8 6 9 6 9 4 7 4 7 2 5 2 5 3 3 right roller-coaster at 1 right rotation at 1

  8. Splay Splay • Example. Splay(1) • Example. Splay(3) 1 1 10 10 8 8 6 9 6 9 4 7 4 7 2 5 2 5 3 3 right rotation at 1 zig-zag at 3 Splay Splay • Example. Splay(3) • Example. Splay(3) 1 1 10 10 8 8 6 9 6 9 3 7 3 7 2 4 2 4 5 5 zig-zag at 3 roller-coaster at 3

  9. Splay Splay • Example. Splay(3) • Example. Splay(3) 1 1 10 10 6 3 8 2 6 3 7 9 4 8 2 4 5 7 9 5 roller-coaster at 3 roller-coaster at 3 Splay Splay • Example. Splay(3) • Example. Splay(3) 1 10 3 3 1 10 2 6 2 6 4 8 4 8 5 7 9 5 7 9 zag-zig at 3 zag-zig at 3

  10. Splay Trees Deletion in Splay Trees • Search(x). Find node containing key x (or predecessor/successor) using usual • Delete 6. 3 search algorithm. Splay found node. • Insert(x). Insert node containing key x using algorithm for binary search trees. 10 6 Splay found node. 1 4 8 • Delete(x). Find node x, splay it and delete it. Tree now divided in two subtrees. 2 5 7 9 Find node with largest key in left subtree, splay it and join it to the right subtree by making it the new root. Find the predecessor v of Make v the parent of the Find x and splay it Delete x x and splay it root of the right subtree v x v x v splay 6: zag-zig at 6 Deletion in Splay Trees Deletion in Splay Trees 5 • Delete 6. • Delete 6. 6 4 3 10 3 10 1 4 8 1 8 2 5 7 9 2 7 9 delete 6 splay 5 connect

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend