Skew Heaps and Binomial Qs • Amortized Runtime • Skew Heaps • Binomial Queues Ashish Sabharwal Autumn, 2003 • Comparing Implementations of Priority Qs

Total Page:16

File Type:pdf, Size:1020Kb

Skew Heaps and Binomial Qs • Amortized Runtime • Skew Heaps • Binomial Queues Ashish Sabharwal Autumn, 2003 • Comparing Implementations of Priority Qs Today’s Outline CSE 326: Data Structures • Binary Heaps: average runtime of insert • Leftist Heaps: re-do proof of property #1 Topic #5: Skew Heaps and Binomial Qs • Amortized Runtime • Skew Heaps • Binomial Queues Ashish Sabharwal Autumn, 2003 • Comparing Implementations of Priority Qs 2 Binary Heaps: Average runtime of Insert Right Path in a Leftist Tree is Short (#1) Recall: Insert-in-Binary-Heap(x) { Claim: The right path is as short as any in the tree. Put x in the next available position Proof: (By contradiction) percolateUp(last node) Pick a shorter path: D1 < D2 } Say it diverges from right path at x x ≤ How long does this percolateUp(last node) take? npl(L) D1-1 because of the path of – Worst case: (tree height), i.e. (log n) length D1-1 to null L R D – Average case: (1) Why?? 1 D2 ≥ npl(R) D2-1 because every node on right path is leftist Average runtime of insert in binary heap = ¡ (1) 3 Leftist property at x violated! 4 A Twist in Complexity Analysis: Skew Heaps The Amortized Case Problems with leftist heaps If a sequence of M operations takes O(M f(n)) time, – extra storage for npl we say the amortized runtime is O(f(n)). – extra complexity/logic to maintain and check npl – two pass iterative merge (requires stack!) • Worst case time per operation can still be large, say O(n) – right side is “often” heavy and requires a switch • Worst case time for any sequence of M operations is O(M f(n)) Solution: skew heaps • Average time per operation for any sequence is O(f(n)) – blind adjusting version of leftist heaps Is this the same as average time? – merge always switches children when fixing right path – iterative method has only one pass – amortized time for merge, insert, and deleteMin is (log n) 5 – however, worst case time for all three is (n) 6 1 Merging Two Skew Heaps Example merge merge 5 3 3 merge T1 a a 7 5 7 merge 10 12 5 merge 3 14 12 10 14 L1 R1 L1 10 12 a < b R1 8 7 8 T 8 2 b b 3 14 5 7 L R 2 2 L2 R2 8 10 14 Only one step per iteration, with children always switched 7 12 8 Runtime Analysis: Skew Heap Code Worst-case and Amortized void merge(heap1, heap2) { case { • No worst case guarantee on right path length! heap1 == NULL: return heap2; • All operations rely on merge heap2 == NULL: return heap1; heap1.findMin() < heap2.findMin(): worst case complexity of all ops = temp = heap1.right; heap1.right = heap1.left; heap1.left = merge(heap2, temp); • Will do amortized analysis later in the course return heap1; (see chapter 11 if curious) otherwise: return merge(heap2, heap1); • Result: M merges take time M log n } } amortized complexity of all ops = 9 10 Yet Another Data Structure: ATaB: Comparing Heaps Binomial Queues • Binary Heaps • Leftist Heaps • Structural property – Forest of binomial trees with at most one tree of any height • d-Heaps • Skew Heaps What’s a forest? What’s a binomial tree? • Order property – Each binomial tree has the heap-order property Still scope for improvement! 11 My opinion: Beautiful and elegant! 12 2 The Binomial Tree, Bh Binomial Q with n elements h • Bh has height h and exactly 2 nodes Binomial Q with n elements has a unique structural • Bh is formed by making Bh-1 a child of another Bh-1 representation in terms of binomial trees! • Root has exactly h children ≈ h ’ Write n in binary: n = 1101 = 13 • Number of nodes at depth d is binomial coeff. ∆ (base 2) (base 10) «d – Hence the name; we will not use this last property 1 B3 1 B2 No B1 1 B0 B0 B1 B2 B3 13 14 Properties of Binomial Q Operations on Binomial Q • At most one binomial tree of any height • Will again define merge as the base operation – insert, deleteMin, buildBinomialQ will use merge • n nodes Ω binary representation is of size ? Ω deepest tree has height ? • Can we do increaseKey efficiently? Ω number of trees is ? decreaseKey? Define: height(forest F) = maxtree T in F { height(T) } • What about findMin? Binomial Q with n nodes has height ¡ (log n) 15 16 Merging Two Binomial Qs Complexity of Merge Essentially like adding two binary numbers! Constant time for each height Max height is log n 1. Combine the two forests 2. For k from 1 to maxheight { ← Ω worst case running time = ( ) a. m total number of Bk’s in the two BQs # of 1’s b. if m=0: continue; 0+0 = 0 c. if m=1: continue; 1+0 = 1 d. if m=2: combine the two Bk’s to form a Bk+1 1+1 = 1+c e. if m=3: retain one Bk and 1+1+c = 1+c combine the other two to form a Bk+1 } Claim: When this process ends, the forest has at most one tree of any height 17 18 3 Insert in a Binomial Q deleteMin in Binomial Q Insert(x): Similar to leftist or skew heap deleteMin: Similar to leftist and skew heaps A tiny bit more complicated runtime Worst case complexity: same as merge ( ) Average case complexity: (1) Why?? Hint: Think of adding 1 to 1101 19 20 deleteMin: Example deleteMin: Example BQ 7 3 4 Result: 7 8 5 4 7 8 5 find and delete 7 smallest root 3 merge without the shaded part 8 5 BQ’ runtime: 7 21 22 buildBinomialQ To Do Call insert n times on an initially empty BQ • Project #1 due tonight! – Bring printout to section tomorrow runtime: naïve O(n log n) • Written homework #1 – will be out later today; I’ll send an email careful analysis (n) idea: count the number of times one needs to combine trees • Revise binary search tree basics • Begin reading chapter 4 in the book 23 24 4.
Recommended publications
  • Lecture 04 Linear Structures Sort
    Algorithmics (6EAP) MTAT.03.238 Linear structures, sorting, searching, etc Jaak Vilo 2018 Fall Jaak Vilo 1 Big-Oh notation classes Class Informal Intuition Analogy f(n) ∈ ο ( g(n) ) f is dominated by g Strictly below < f(n) ∈ O( g(n) ) Bounded from above Upper bound ≤ f(n) ∈ Θ( g(n) ) Bounded from “equal to” = above and below f(n) ∈ Ω( g(n) ) Bounded from below Lower bound ≥ f(n) ∈ ω( g(n) ) f dominates g Strictly above > Conclusions • Algorithm complexity deals with the behavior in the long-term – worst case -- typical – average case -- quite hard – best case -- bogus, cheating • In practice, long-term sometimes not necessary – E.g. for sorting 20 elements, you dont need fancy algorithms… Linear, sequential, ordered, list … Memory, disk, tape etc – is an ordered sequentially addressed media. Physical ordered list ~ array • Memory /address/ – Garbage collection • Files (character/byte list/lines in text file,…) • Disk – Disk fragmentation Linear data structures: Arrays • Array • Hashed array tree • Bidirectional map • Heightmap • Bit array • Lookup table • Bit field • Matrix • Bitboard • Parallel array • Bitmap • Sorted array • Circular buffer • Sparse array • Control table • Sparse matrix • Image • Iliffe vector • Dynamic array • Variable-length array • Gap buffer Linear data structures: Lists • Doubly linked list • Array list • Xor linked list • Linked list • Zipper • Self-organizing list • Doubly connected edge • Skip list list • Unrolled linked list • Difference list • VList Lists: Array 0 1 size MAX_SIZE-1 3 6 7 5 2 L = int[MAX_SIZE]
    [Show full text]
  • Advanced Data Structures
    Advanced Data Structures PETER BRASS City College of New York CAMBRIDGE UNIVERSITY PRESS Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo Cambridge University Press The Edinburgh Building, Cambridge CB2 8RU, UK Published in the United States of America by Cambridge University Press, New York www.cambridge.org Information on this title: www.cambridge.org/9780521880374 © Peter Brass 2008 This publication is in copyright. Subject to statutory exception and to the provision of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published in print format 2008 ISBN-13 978-0-511-43685-7 eBook (EBL) ISBN-13 978-0-521-88037-4 hardback Cambridge University Press has no responsibility for the persistence or accuracy of urls for external or third-party internet websites referred to in this publication, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate. Contents Preface page xi 1 Elementary Structures 1 1.1 Stack 1 1.2 Queue 8 1.3 Double-Ended Queue 16 1.4 Dynamical Allocation of Nodes 16 1.5 Shadow Copies of Array-Based Structures 18 2 Search Trees 23 2.1 Two Models of Search Trees 23 2.2 General Properties and Transformations 26 2.3 Height of a Search Tree 29 2.4 Basic Find, Insert, and Delete 31 2.5ReturningfromLeaftoRoot35 2.6 Dealing with Nonunique Keys 37 2.7 Queries for the Keys in an Interval 38 2.8 Building Optimal Search Trees 40 2.9 Converting Trees into Lists 47 2.10
    [Show full text]
  • Kd Trees What's the Goal for This Course? Data St
    Today’s Outline - kd trees CSE 326: Data Structures Too much light often blinds gentlemen of this sort, Seeing the forest for the trees They cannot see the forest for the trees. - Christoph Martin Wieland Hannah Tang and Brian Tjaden Summer Quarter 2002 What’s the goal for this course? Data Structures - what’s in a name? Shakespeare It is not possible for one to teach others, until one can first teach herself - Confucious • Stacks and Queues • Asymptotic analysis • Priority Queues • Sorting – Binary heap, Leftist heap, Skew heap, d - heap – Comparison based sorting, lower- • Trees bound on sorting, radix sorting – Binary search tree, AVL tree, Splay tree, B tree • World Wide Web • Hash Tables – Open and closed hashing, extendible, perfect, • Implement if you had to and universal hashing • Understand trade-offs between • Disjoint Sets various data structures/algorithms • Graphs • Know when to use and when not to – Topological sort, shortest path algorithms, Dijkstra’s algorithm, minimum spanning trees use (Prim’s algorithm and Kruskal’s algorithm) • Real world applications Range Query Range Query Example Y A range query is a search in a dictionary in which the exact key may not be entirely specified. Bellingham Seattle Spokane Range queries are the primary interface Tacoma Olympia with multi-D data structures. Pullman Yakima Walla Walla Remember Assignment #2? Give an algorithm that takes a binary search tree as input along with 2 keys, x and y, with xÃÃy, and ÃÃ ÃÃ prints all keys z in the tree such that x z y. X 1 Range Querying in 1-D
    [Show full text]
  • Binary Trees, Binary Search Trees
    Binary Trees, Binary Search Trees www.cs.ust.hk/~huamin/ COMP171/bst.ppt Trees • Linear access time of linked lists is prohibitive – Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)? Trees • A tree is a collection of nodes – The collection can be empty – (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r Some Terminologies • Child and parent – Every node except the root has one parent – A node can have an arbitrary number of children • Leaves – Nodes with no children • Sibling – nodes with same parent Some Terminologies • Path • Length – number of edges on the path • Depth of a node – length of the unique path from the root to that node – The depth of a tree is equal to the depth of the deepest leaf • Height of a node – length of the longest path from that node to a leaf – all leaves are at height 0 – The height of a tree is equal to the height of the root • Ancestor and descendant – Proper ancestor and proper descendant Example: UNIX Directory Binary Trees • A tree in which no node can have more than two children • The depth of an “average” binary tree is considerably smaller than N, eventhough in the worst case, the depth can be as large as N – 1. Example: Expression Trees • Leaves are operands (constants or variables) • The other nodes (internal nodes) contain operators • Will not be a binary tree if some operators are not binary Tree traversal • Used to print out the data in a tree in a certain order • Pre-order traversal – Print the data at the root – Recursively print out all data in the left subtree – Recursively print out all data in the right subtree Preorder, Postorder and Inorder • Preorder traversal – node, left, right – prefix expression • ++a*bc*+*defg Preorder, Postorder and Inorder • Postorder traversal – left, right, node – postfix expression • abc*+de*f+g*+ • Inorder traversal – left, node, right.
    [Show full text]
  • Exam Questions Chapter 1
    Exam questions Mark Allen Weiss School of Computer Science Florida International University University Park Miami FL Abstract This rep ort contains a collection of multiplechoice questions organized by b o ok chapter that can b e used 1 a for examinations Answers are provided at the end This rep ort was typeset in L T X Original source is E available Chapter What is the approximate value of log a b c d e none of the ab ove If log n equals what is the value of log n a b c d e none of the ab ove When p erforming a pro of by induction which is the case that is trivially true a the basis b the inductive hypothesis c the lemma d the theorem e none of the ab ove 1 Many of these questions app ear in dierent form in the Instructors Resource Manual for Algorithms Data Structures and Problem Solving with C published by AddisonWesley The following routine violates which rules of recursion function Recurse N Integer return Integer is begin if N then return else return N Recurse N Recurse N end if end Recurse a No base case b Fails to make progress c Performs redundant work d Two of the ab ove e All of a b and c Which of the following is the most likely result of failing to make progress towards a base case in a recursive call a compiler enters into an innite lo op b error at compilation time c exception is raised at run time d recursive routine enters an innite lo op when it runs e recursive routine terminates with bad value but no other error Answers B A A D C Chapter Which of the following functions grows fastest
    [Show full text]
  • Data Structures Binary Search Trees • Dictionary ADT / Search ADT • Quick Tree Review • Binary Search Trees
    Today’s Outline CSE 326: Data Structures Binary Search Trees • Dictionary ADT / Search ADT • Quick Tree Review • Binary Search Trees 5/30/2008 1 5/30/2008 2 The Dictionary ADT ADTs Seen So Far •jfogarty • Priority Queue • Data: James •Stack Fogarty –Push –Insert – a set of insert(jfogarty, ….) CSE 666 –Pop – DeleteMin (key, value) • phenry pairs Peter • Queue Henry CSE 002 – Enqueue find(boqin) – Dequeue • Operations: • boqin Then there is decreaseKey… • boqin – Insert (key, Bo, Qin, … Bo Qin value) CSE 002 – Find (key) Need pointer! Why? The Dictionary ADT is also Because find not efficient. – Remove (key)called the “Map ADT” 5/30/2008 3 5/30/2008 4 A Modest Few Uses Implementations insert find delete Θ(1) Θ(n) Θ(n) •Sets • Unsorted Linked-list • Dictionaries • Networks : Router tables Θ(1) Θ(n) Θ(n) • Unsorted array • Operating systems : Page tables • Compilers : Symbol tables log n + n Θ(log n) log n + n Probably the most widely used ADT! • Sorted array SO CLOSE! What limits the performance? 5/30/2008 5 5/30/2008 Time to move elements, can we mimic BinSearch with BST?6 Tree Calculations Tree Calculations Example Recall: height is max t How high is this tree? A number of edges from root to a leaf B C D E F G Find the height of the tree... H I height(t) = 1 + max {height(t.left), height(t.right)} height(B) = 1 runtime: height(C) = 4 J K L L Θ(N) (constant time for each node; so height(A) = 5 each node visited twice) M N 5/30/2008 7 5/30/2008 8 More Recursive Tree Calculations: Inorder Traversal Tree Traversals void traverse(BNode t){
    [Show full text]
  • Chapter 6 Priority Queues
    Chapter 6 Priority Queues Introduction Want to delete from queue according to priority. o Max priority queue – delete the greatest. o Min priority queue – delete the least. Insert normally, but delete based on priority. We can implement priority queues using binary search trees, ordered or unordered lists, ordered or unordered arrays, etc. Let assume a linked list implementation. o Unordered . Insert – (1) . Delete – (n) . o Ordered . Insert – (n) . Delete – (1) . What if we used a BST? What would happen with successive deletes? A splay tree? What would happen with successive deletes? Is there a way of getting the good run time without having to have the expense of pointers? Heaps Heap – complete binary tree in which each node is smaller than its parent. o Is this the same thing as a binary search tree (BST)? The binary tree for the heap is implemented as an array. This allows us easy access to children and parents as seen in the previous chapter. Used as a priority queue – regular insertion, priority deletion. Insertion – put the new node at the first empty position, and sift the element up (if needed). See Figure 1. o (height) (log n) Figure 1 Insertion into a Heap Priority Queues Page 1 Deletion – select root. Swap with last position (which will no longer be part of queue), and sift- down. See Figure 2. o (height) (log n) Figure 2 Deletion from Heap Initialization – insert n items into tree O(n log n) worst case BUT experiments have shown that on average, it only moves up an element 1.6 levels.
    [Show full text]
  • Red-Black Trees Important: There Are Two Questions N:O 6 – One About AVL Trees and One About Red-Black Trees
    Data structures and algorithms Re-examination Solution suggestions Thursday, 2020-08-20, 8:30–12:30, in Canvas and Zoom Examiner(s) Peter Ljunglöf, Nick Smallbone, Pelle Evensen. Allowed aids Course literature, other books, the internet. You are not allowed to discuss the problems with anyone! Submitting Submit your answers in one single PDF or Word or OpenOffice document. ​ ​ You can write your answers directly into the red boxes in this file. Pen & paper Some questions are easier to answer using pen and paper, which is fine! In that case, take a photo of your answer and paste into your answer document. Make sure the photo is readable! Assorted notes You may answer in English or Swedish. Excessively complicated answers might be rejected. Write legibly – we need to be able to read and understand your answer! Exam review If you want to discuss the grading, please contact Peter via email. There are 6 basic, and 3 advanced questions, and two points per question. So, the highest possible mark is 18 in total (12 from the basic and 6 from the advanced). Here is what you need for each grade: ● To pass the exam, you must get 8 out of 12 on the basic questions. ​ ​ ● To get a four, you must get 9 out of 12 on the basic questions, plus 2 out of 6 on the advanced. ​ ​ ● To get a five, you must get 10 out of 12 on the basic questions, plus 4 out of 6 on the advanced. ​ ​ ● To get VG (DIT181), you must get 9 out of 12 on the basic, plus 3 out of 6 on the advanced.
    [Show full text]
  • Amortized Complexity Verified
    J. Automated Reasoning manuscript No. (will be inserted by the editor) Amortized Complexity Verified Tobias Nipkow and Hauke Brinkop Abstract A framework for the analysis of the amortized complexity of functional data structures is formalized in the proof assistant Isabelle/HOL and applied to a number of standard examples and to the following non-trivial ones: skew heaps, splay trees, splay heaps and pairing heaps. The proofs are completely algebraic and are presented in some detail. 1 Introduction Amortized analysis [43,7] bounds the average cost of an operation in a sequence of operations in the worst case. In this paper we formalize a simple framework for amortized analysis of functional programs in the theorem prover Isabelle/HOL [37] and apply it to both the easy standard examples and the more challenging examples of skew heaps, splay trees, splay heaps and pairing heaps. This is an extended version of a previous publication [32]: the framework is generalized from unary to n-ary operations and an analysis of pairing heaps has been added. We are aiming for a particularly lightweight framework that supports proofs at a high level of abstraction. Therefore all algorithms are modeled as recursive functions in the logic. The contributions of the paper are as follows: – A lightweight and flexible framework for amortized complexity analysis in a theorem prover. – The first complexity analyses for skew heaps, splay trees, splay heaps and pairing heaps in a theorem prover. – The first purely algebraic proof of the amortized complexity of pairing heaps. The last point needs some explanation. For pairing heaps we had to recast the original proof by Fredman et al.
    [Show full text]
  • Data Structures
    Data structures PDF generated using the open source mwlib toolkit. See http://code.pediapress.com/ for more information. PDF generated at: Thu, 17 Nov 2011 20:55:22 UTC Contents Articles Introduction 1 Data structure 1 Linked data structure 3 Succinct data structure 5 Implicit data structure 7 Compressed data structure 8 Search data structure 9 Persistent data structure 11 Concurrent data structure 15 Abstract data types 18 Abstract data type 18 List 26 Stack 29 Queue 57 Deque 60 Priority queue 63 Map 67 Bidirectional map 70 Multimap 71 Set 72 Tree 76 Arrays 79 Array data structure 79 Row-major order 84 Dope vector 86 Iliffe vector 87 Dynamic array 88 Hashed array tree 91 Gap buffer 92 Circular buffer 94 Sparse array 109 Bit array 110 Bitboard 115 Parallel array 119 Lookup table 121 Lists 127 Linked list 127 XOR linked list 143 Unrolled linked list 145 VList 147 Skip list 149 Self-organizing list 154 Binary trees 158 Binary tree 158 Binary search tree 166 Self-balancing binary search tree 176 Tree rotation 178 Weight-balanced tree 181 Threaded binary tree 182 AVL tree 188 Red-black tree 192 AA tree 207 Scapegoat tree 212 Splay tree 216 T-tree 230 Rope 233 Top Trees 238 Tango Trees 242 van Emde Boas tree 264 Cartesian tree 268 Treap 273 B-trees 276 B-tree 276 B+ tree 287 Dancing tree 291 2-3 tree 292 2-3-4 tree 293 Queaps 295 Fusion tree 299 Bx-tree 299 Heaps 303 Heap 303 Binary heap 305 Binomial heap 311 Fibonacci heap 316 2-3 heap 321 Pairing heap 321 Beap 324 Leftist tree 325 Skew heap 328 Soft heap 331 d-ary heap 333 Tries 335 Trie
    [Show full text]
  • Chapter 6 Heaps
    Introduction some systems applications require that items be processed Chapter 6 in specialized ways printing Heaps may not be best to place on a queue some jobs may be more ________________ small 1-page jobs should be printed before a 100-page job operating system _________________ processes run only for a slice of time queue: FIFO some jobs may take too long other jobs are more important and should not wait 2 Heap Model Heap Implementation specialized queue required heaps can be implemented with heap (priority queue) , with insertions at head provides at least insert _______________ deleteMin deleteMin: finds, returns, and removes min (or max) ordered linked list (worse due to number of ) other operations common insert used in other applications deleteMin external sorting binary search tree algorithms insert discrete event simulation deleteMin since only min is deleted, tree will be _______________ overkill since other included operations not required 3 4 Binary Heap Binary Heap binary heap common structure property simply termed heap completely filled two properties except level, which is filled from left to right ________________ complete binary tree has between and nodes heap _____________ height: operations can one of the properties can be represented with an array (no necessary) operation must continue until heap properties have been array position restored, which is typically simple left child in right child in parent in operations simple maximum heap size must be ______________________ 5 6 Binary Heap Binary Heap heap-order property
    [Show full text]
  • Some Terminologies
    1/9/2014 Some Terminologies Binary Trees, Binary Search Trees www.cs.ust.hk/~huamin/COMP17 1/bst. ppt • Child and parent – Every node except the root has one parent – A node can have an arbitrary number of children • Leaves – Nodes with no children • Sibling – nodes with same parent Trees Some Terminologies • Linear access time of linked lists is • Path prohibitive • Length – number of edges on the path – Does there exist any simple data structure • Depth of a node for which the running time of most – length of the unique path from the root to that node operations (search, insert, delete) is O(log – The depth of a tree is equal to the depth of the deepest leaf N)? • Height of a node – length of the longest path from that node to a leaf – all leaves are at height 0 – The height of a tree is equal to the height of the root • Ancestor and descendant – Proper ancestor and proper descendant Trees Example: UNIX Directory • A tree is a collection of nodes – The collection can be empty – (recursive definition) If not empty, a tree consists of a distinguished node r (the root ), and zero or more nonempty subtrees T1, T 2, ...., T k, each of whose roots are connected by a directed edge from r 1 1/9/2014 Binary Trees Preorder, Postorder and Inorder • A tree in which no node can have more than two children • Preorder traversal – node, left, right – prefix expression • ++a*bc*+*defg • The depth of an “average” binary tree is considerably smaller than N, eventhough in the worst case, the depth can be as large as N – 1.
    [Show full text]