1 / 29

Course Outline

Course Outline. Introduction and Algorithm Analysis (Ch. 2) Hash Tables: dictionary data structure (Ch. 5) Heaps: priority queue data structures (Ch. 6) Balanced Search Trees: general search structures (Ch. 4.1-4.5) Union-Find data structure (Ch. 8.1–8.5)

odeda
Download Presentation

Course Outline

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. Course Outline • Introduction and Algorithm Analysis (Ch. 2) • Hash Tables: dictionary data structure (Ch. 5) • Heaps: priority queue data structures (Ch. 6) • Balanced Search Trees: general search structures (Ch. 4.1-4.5) • Union-Find data structure (Ch. 8.1–8.5) • Graphs: Representations and basic algorithms • Topological Sort (Ch. 9.1-9.2) • Minimum spanning trees (Ch. 9.5) • Shortest-path algorithms (Ch. 9.3.2) • B-Trees: External-Memory data structures (Ch. 4.7) • kD-Trees: Multi-Dimensional data structures (Ch. 12.6) • Misc.: Streaming data, randomization

  2. Binary search trees • Values in the left subtree < the value in the node < values in the right subtree • find, findMin, and findMax • insert a new element • Find the location for the new element • Insert a new node • remove an element • Find the element • If the element is a leaf node, delete the node and done • If it has one child, delete the node and fix the link in the parent node • If it has two children, find a replacement node (how?) • Complexity: O(N) for find, insert, and remove

  3. Intuition of Making a Good Tree • How do we guarantee O(log N) complexity for find, insert, and remove? • Constant time effort to determine which branch • Total time is proportional to tree height • If tree is balanced, height is O(log n)

  4. Alternate insertionsand deletions randomly generated

  5. An average case analysis • Ideal case: O(log N) • Average case without remove • All orders of insertions are equally likely a i elements <a n-i-1 elements > a All orders of insertions: Average path length P(i) All orders of insertions: Average path length P(n-i-1)

  6. Tree Rotations d b b d A E C E A C right rotation left rotation

  7. AVL trees • height of a tree: max depth of a node in the tree • AVL tree: • A binary search tree such that at each node X,the height of the leftand right subtreesof X can differ by at most 1 • Bound on the height of an AVL tree with N nodes • S(h) : the min number of nodes in an AVL tree of height h • S(h) = S(h – 1) + S(h –2) +1 • Claim: S(h) = Fh+3 – 1 • h = O(log N) • Maintaining AVL trees upon insert, remove: • Single and double rotations

  8. AVL Trees - Insertion • Member (Find) operation (as in BST) • Insert at leaf • Update balance information upward from insertion • At the first node that |balance|>=2, four cases: • insertion was in left subtree of left child • insertion was in left subtree of right child • insertion was in right subtree of left child • insertion was in right subtree of right child • 1 and 4 are fixed by single rotation • 2 and 3 are fixed by double rotation

  9. Single Rotation (left-left) • Y cannot be at X’s level • Y cannot be at Z’s level either 1 2 2 1 Z X Y Z X Y

  10. Single Rotation (right-right) 2 1 1 2 Z X X Y Y Z • Y cannot be at Z’s level • Y cannot be at X’s level either

  11. Double Rotation (left-right): Single Won’t Work • Single rotation does not work because it does not make Y any shorter 1 2 2 1 Z X Y Z X Y

  12. Double Rotation (left-right): First Step • First rotation between 2 and 1 3 3 2 1 D D 1 2 C A B C A B

  13. Double Rotation (left-right): Second Step • Second rotation between 2 and 3 2 3 3 2 1 D 1 C D B C A B A

  14. Double Rotation (left-right): Summary 2 3 3 1 1 D 2 D B C A A C B

  15. Double Rotation (right-left): First Step • First Rotation between 2 and 3 1 1 A 2 A 3 2 3 B D C D C B

  16. Double Rotation (right-left): Second Step • Second Rotation between 1 and 2 2 1 A 3 2 1 3 D B B C A C D

  17. Double Rotation (right-left): Summary 2 1 3 A 3 1 2 D B C A D C B

  18. Insertion into an AVL tree AvlTree AvlNode element height left right AvlTree::insert(x, t) if (t = NULL) then t = new AvlNode(x, …); elseif (x < telement) then insert (x, tleft); if (height(tleft) – height(tright) = 2) thenif (x < tleftelement ) then rotateWithLeftChild (t); else doubleWithLeftChild (t); else if (telement < x ) then insert (x, tright); if (height(tright) – height(tleft) = 2) thenif (trightelement < x) then rotateWithRightChild (t); else doubleWithRightChild (t); theight = max{height(tleft), height(tright)}+1; • Tree height • Search • Insertion: • search to find insertion point • adjust the tree height • rotation if necessary

  19. Removing an element from an AVL tree • Similar process: • locate & delete the element • adjust tree height • perform necessary rotations (up to log (n+1)) • Example • Complexity of operations: • O(h) £O(log n)n: number of nodes, h: tree height

  20. Deletion Example 15 delete 10 15 10 30 5 30 5 12 17 35 12 17 35 14 20 31 40 14 20 31 40 50 50 15 30 15 35 12 30 12 17 31 40 5 14 17 35 5 14 20 50 20 31 40 50

  21. Splay trees G X P P G D X A A B C D B C G X P D P A X C G B A B C D • Splayat a node: rotate the node up to the root • Basic operations (repeat until node X is at root): • zig-zag: • zig-zig:

  22. Splay trees • Idea: always splay at a node after it is accessed • Details: • after find, splay at the found node (or last node on search path if not found) • after insert, splay at the new node • after delete, splay at last node on search path

  23. Splay trees Starting with an empty tree,any m operations (find, insert, delete)take O(m log n) time,where n is the maximum # of items ever in the tree • Proof: “credit accounting” • just count the cost of splaying • each operation gets 3 lg n + 1 coins • one coin pays for one splay step • each node holds floor(lg(size of subtree)) coins

  24. Splay trees G X P P G D X A A B C D B C G X P D P A X C G B A B C D • Splayat a node: rotate the node up to the root • Basic operations (repeat until node X is at root): • zig-zag: • zig-zig:

  25. Splay trees • Idea: always splay at a node after it is accessed • Details: • after find, splay at the found node (or last node on search path if not found) • after insert, splay at the new node • after delete, splay at last node on search path

  26. Splay trees: the main theorem Starting with an empty tree,any m operations (find, insert, delete)take O(m log n) time,where n is the maximum # of items ever in the tree Lemma: Any sequence of m operations requires at most 4 m log n + 2 splay steps. (The theorem follows immediately from the lemma.)

  27. Splay trees: proving the lemma • Each operation gets 4 log n + 2 coins (3 log n + 1is enough for all but insert ) • One splay step costs one coin, from somewhere • Some of the leftover coins are left “on the tree”to pay for later operations Credit invariant:Every tree node holds log(subtree size) coins (rounded down to an integer)

  28. Splay trees: maintaining the credit invariant G X P P G D X A A B C D B C • lss(x) = log(subtree size) before step; lss’ after step • zig-zag step costs 1 coin (to do the step) plus lss’(x) – lss(x) + lss’(p) – lss(p) + lss’(g) – lss(g)(to maintain the invariant) • this is always <= 3(lss’(x) – lss(x)) [complex argument] • similarly for zig-zig splay step; 1 more for last half step • total cost of splay is <= 3(lss(root))+1 = 3 log n + 1

  29. Splay trees: end of the main proof • maintaining invariant during splay costs <= 3 log n + 1 coins • maintaining invariant during insert may cost an extra log n + 1 coins (because lss goes up) • maintaining credit invariant never costs more than 4 log n + 2 coins • Lemma: Any sequence of m operations requires at most 4 m log n + 2 splay steps. • Theorem: Starting with an empty tree, any m operations (find, insert, delete) take O(m log n) time

More Related