1 / 32

CSE 326: Data Structures Lecture #18 Fistful of Data Structures

CSE 326: Data Structures Lecture #18 Fistful of Data Structures. Steve Wolfman Winter Quarter 2000. Today’s Outline. What Steve Didn’t Get To On Monday Warm-up: augmenting leftist heaps Binomial Queues Treaps Randomized Skip Lists What Steve Won’t Get To (Ever?).

rudolf
Télécharger la présentation

CSE 326: Data Structures Lecture #18 Fistful of Data Structures

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. CSE 326: Data StructuresLecture #18Fistful of Data Structures Steve Wolfman Winter Quarter 2000

  2. Today’s Outline • What Steve Didn’t Get To On Monday • Warm-up: augmenting leftist heaps • Binomial Queues • Treaps • Randomized Skip Lists • What Steve Won’t Get To (Ever?)

  3. Thinking about DecreaseKey in Leftist Heaps Why not just percolate up? decreaseKey( , 3) node 7 3 12 8 7 8 3 15 12 17 9 30 17 9 30 20 22 18 20 22 18

  4. DecreaseKey in Leftist Heaps decreaseKey(15, 3) 7 7 12 8 12 8 3 15 3 17 9 30 17 9 30 20 22 18 20 22 18 Now just merge the two?

  5. Fixing DecreaseKey in Leftist Heaps decreaseKey(15, 3) This may not be leftist 7 7 So, fix it! 12 8 8 12 3 17 9 30 9 30 17 20 22 18 18 This is still leftist Now, merge!

  6. DecreaseKey runtime How many nodes could possibly have the wrong Null Path Length up the line? runtime:

  7. Delete in Leftist Heaps decreaseKey(15, -) deleteMin() runtime:

  8. Binomial Trees A binomial tree of rank 0 is a one node tree. A binomial tree of rank k is a binomial tree of rank k-1 with another binomial tree of rank k-1 hanging from its root. rank 0 rank 1

  9. First Five Binomial Trees rank 0 rank 1 rank 2 rank 3 rank 4 How many nodes does a binomial tree of rank k have?

  10. Binomial Queue Heap Data Structure rank 3 rank 2 rank 1 rank 0 • Composed of a forest of binomial trees, no two of the same rank • Heap-order enforced within each tree • Ranks present can be computed using the binary representation of the tree’s size size = 10 = 10102 rank 1 rank 3 5 3 4 9 7 13 6 10 15 21

  11. Insertion in Binomial Queues insert(10) rank 1 rank 2 rank 0 rank 1 rank 2 10 10 5 3 5 3 9 7 13 9 7 13 15 15 If there’s no rank 0 tree, just put the new node in as a rank 0 tree.

  12. Insertion in Binomial Queues insert(10) rank 0 rank 1 rank 0 rank 1 rank 2 10 5 3 5 3 3 7 10 7 7 5 10 It’s like addition of binary numbers! 1+1 = 0 plus a carry tree 1+1+1 = 1 plus a carry tree runtime:

  13. Merge in Binomial Queues rank 1 rank 2 rank 0 rank 1 5 3 11 4 9 7 13 16 15 rank 0 rank 3 11 3 0110 + 0011 = 1001 4 7 13 15 16 5 runtime: 9

  14. DeleteMin in Binomial Queues These are one Binomial Queue 11 10 1 3 4 14 8 25 7 13 27 15 16 5 9 These are another 8 10 3 Just merge the two: 4 11 14 25 7 13 27 15 16 5 runtime: 9

  15. Binomial Queue Summary • Implements priority queue ADT • Insert in amortized O(1) time • FindMin (with some tricks) in O(1) time • DeleteMin in O(log n) time • Merge in O(log n) time • Memory use • O(1) per node • about the cost of skew heaps • Complexity?

  16. Treap Dictionary Data Structure heap in yellow; search tree in blue • Treaps have the binary search tree • binary tree property • search tree property • Treaps also have the heap-order property! • randomly assigned priorities 2 9 6 7 4 18 7 8 9 15 10 30 Legend: priority key 15 12

  17. insert(7) insert(8) insert(9) insert(12) 6 7 6 7 2 9 2 9 7 8 6 7 6 7 15 12 7 8 7 8 Tree + Heap… Why Bother? Insert data in sorted order into a treap; what shape tree comes out? Legend: priority key

  18. Treap Insert • Choose a random priority • Insert as in normal BST • Rotate up until heap order is restored insert(15) 2 9 2 9 2 9 6 7 15 12 6 7 15 12 6 7 9 15 7 8 7 8 9 15 7 8 15 12

  19. Treap Delete delete(9) 2 9 6 7 • Find the key • Increase its value to  • Rotate it to the fringe • Snip it off 7 8  9 6 7 9 15 9 15 7 8 15 12 6 7 6 7 15 12 6 7 7 8 7 8 9 15 9 15 7 8 9 15  9 15 12 15 12 15 12  9

  20. Treap Summary • Implements Dictionary ADT • insert in expected O(log n) time • delete in expected O(log n) time • find in expected O(log n) time • Memory use • O(1) per node • about the cost of AVL trees • Complexity?

  21. Perfect Binary Skip List • Sorted linked list • # of links of a node is its height • The height i link of each node (that has one) links to the next node of height i or greater 22 11 8 19 29 2 10 13 20 23

  22. Find in a Perfect Binary Skip List • Start i at the maximum height • Until the node is found or i is one and the next node is too large: • If the next node along the i link is less than the target, traverse to the next node • Otherwise, decrease i by one runtime:

  23. Randomized Skip List Intuition It’s far too hard to insert into a perfect skip list, but is perfection necessary? What matters in a skip list?

  24. 13 8 22 10 20 29 2 11 19 23 Randomized Skip List • Sorted linked list • # of links of a node is its height • The height i link of each node (that has one) links to the next node of height i or greater • There should be about 1/2 as many nodes of height i+1 as there are of height i

  25. Find in a RSL • Start i at the maximum height • Until the node is found or i is one and the next node is too large: • If the next node along the i link is less than the target, traverse to the next node • Otherwise, decrease i by one Same as for a perfect skip list! runtime:

  26. Insertion in a RSL • Flip a coin until it comes up heads; that takes i flips. Make the new node’s height i. • Do a find, remembering nodes where we go down • Put the node at the spot where the find ends • Point all the nodes where we went down (up to the new node’s height) at the new node • Point the new node’s links where those redirected pointers were pointing

  27. insert(22) with 3 flips 13 20 23 13 8 8 10 10 20 29 29 2 2 11 11 19 19 23 Insertion Example in RSL 22 runtime:

  28. Range Queries and Iteration • Range query: search for everything that falls between two values • Iteration: successively return (in order) each element in the structure How do we do them? How fast are they?

  29. Randomized Skip List Summary • Implements Dictionary ADT • insert in expected O(log n) • find in expected O(log n) • delete? • Memory use • expected constant memory per node • about double a linked list • Complexity?

  30. What We Won’t Discuss Pairing heaps - practically, the fastest and best implementation of heaps for decreaseKey and merge; they use the leftist cut and merge technique Red-Black Trees - a balanced tree that uses just a one-bit color flag and some invariants to maintain balance: see www/homes/sds/rb.html AA-Trees - a cross between Red-Black trees and B-Trees that is relatively simple to code and gives worst case O(log n) running time Deterministic skip lists - a version of skip lists that gives worst case O(log n) running time

  31. To Do • Finish Project III • Browse chapters 10 & 12 in the book • Form Project IV teams! • groups of 4-6 • 2 1/2 week project • demos at the end

  32. Coming Up • Quad Trees • k-D Trees • Quiz (February 17th) • Project III due (February 17th by 5PM!) • Project IV distributed (February 18th)

More Related