CS711008Z Algorithm Design and Analysis Lecture 7

Total Page:16

File Type:pdf, Size:1020Kb

CS711008Z Algorithm Design and Analysis Lecture 7 CS711008Z Algorithm Design and Analysis Lecture 7. Binary heap, binomial heap, and Fibonacci heap Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China . 1 / 124 Outline Introduction to priority queue Various implementations of priority queue: Linked list: a list having n items is too long to support efficient ExtractMin and Insert operations simultaneously; Binary heap: using a tree rather than a linked list; Binomial heap: allowing multiple trees rather than a single tree to support efficient Union operation Fibonacci heap: implement DecreaseKey via simply cutting an edge rather than exchanging nodes, and control a “bushy” tree shape via allowing at most one child losing for any node. 2 / 124 Priority queue . 3 / 124 Priority queue: motivation Motivation: It is usually a case to extract the minimum from a set S of n numbers, dynamically. Here, the word “dynamically” means that on S, we might perform Insertion, Deletion and DecreaseKey operations. The question is how to organize the data to efficiently support these operations. 4 / 124 Priority queue Priority queue is an abstract data type similar to stack or queue, but each element has a priority associated with its name. A min-oriented priority queue must support the following core operations: 1 H=MakeHeap(): to create a new heap H; 2 Insert(H; x): to insert into H an element x together with its priority 3 ExtractMin(H): to extract the element with the highest priority; 4 DecreaseKey(H; x; k): to decrease the priority of element x; 5 Union(H1; H2): return a new heap containing all elements of heaps H1 and H2, and destroy the input heaps . 5 / 124 Priority queue is very useful Priority queue has extensive applications, such as: Dijkstra’s shortest path algorithm Prim’s MST algorithm Huffman coding A∗ searching algorithm HeapSort ...... 6 / 124 An example: Dijkstra’s algorithm . 7 / 124 Dijkstra’s algorithm [1959] Dijkstra(G; s; t) 1: key(s) = 0; //key(u) stores an upper bound of the shortest distance from s to u; 2: PQ: Insert (s); 3: for all node v =6 s do 4: key(v) = +1 5: PQ: Insert (v) //n times 6: end for 7: S = fg; // Let S be the set of explored nodes; 8: while S =6 V do 9: v∗ = PQ: ExtractMin(); //n times 10: S = S [ fv∗g; 11: for all v 2= S and < v∗; v >2 E do 12: if key(v∗) + d(v∗; v) < key(v) then 13: PQ:DecreaseKey(v; key(v∗) + d(v∗; v)); //m times 14: end if 15: end for 16: end while Here PQ denotes a min-priority queue. 8 / 124 Dijkstra’s algorithm: an example 9 24 s a d 14 18 2 15 19 b 30 11 f 6 20 e 16 44 c t . 9 / 124 Initialization S = fg PQ = fs(0); a(1); b(1); c(1); d(1); e(1); f(1); t(1)g 0 1 1 9 24 s a d 14 18 1 2 15 19 b 1 30 1 11 f 6 1 20 e 16 1 44 c t . 10 / 124 Step 1: ExtractMin S = fg PQ = fs(0); a(1); b(1); c(1); d(1); e(1); f(1); t(1)g 0 1 1 9 24 s a d ExtractMin returns s 14 18 1 2 15 19 b 1 30 1 11 f 6 1 20 e 16 1 44 c t . 11 / 124 Step 1: DecreaseKey S = fsg PQ = fa(1); b(1); c(1); d(1); e(1); f(1); t(1)g 0 1 1 9 24 s a d 14 18 DecreaseKey(a; 9) DecreaseKey(b; 14) 1 2 15 19 DecreaseKey(c; 15) b 1 30 1 11 f 6 1 20 e 16 1 44 c t . 12 / 124 ExtractMin returns a Step 2: ExtractMin S = fsg PQ = fa(9); b(14); c(15); d(1); e(1); f(1); t(1)g 0 9 1 9 24 s a d 14 18 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 13 / 124 Step 2: ExtractMin S = fsg PQ = fa(9); b(14); c(15); d(1); e(1); f(1); t(1)g 0 9 1 9 24 s a d ExtractMin returns a 14 18 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 13 / 124 Step 2: DecreaseKey S = fs; ag PQ = fb(14); c(15); d(1); e(1); f(1); t(1)g 0 9 1 9 24 s a d 14 18 DecreaseKey(d; 33) 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 14 / 124 ExtractMin returns b Step 3: ExtractMin S = fs; ag PQ = fb(14); c(15); d(33); e(1); f(1); t(1)g 0 9 33 9 24 s a d 14 18 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 15 / 124 Step 3: ExtractMin S = fs; ag PQ = fb(14); c(15); d(33); e(1); f(1); t(1)g 0 9 33 9 24 s a d ExtractMin returns b 14 18 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 15 / 124 Step 3: DecreaseKey S = fs; a; bg PQ = fc(15); d(33); e(1); f(1); t(1)g 0 9 33 9 24 s a d 14 18 DecreaseKey(d; 32) DecreaseKey(e; 44) 14 2 15 19 b 1 30 1 11 f 6 15 20 e 16 1 44 c t . 16 / 124 ExtractMin returns c Step 4: ExtractMin S = fs; a; bg PQ = fc(15); d(32); e(44); f(1); t(1)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 1 30 44 11 f 6 15 20 e 16 1 44 c t . 17 / 124 Step 4: ExtractMin S = fs; a; bg PQ = fc(15); d(32); e(44); f(1); t(1)g 0 9 32 9 24 s a d ExtractMin returns c 14 18 14 2 15 19 b 1 30 44 11 f 6 15 20 e 16 1 44 c t . 17 / 124 Step 4: DecreaseKey S = fs; a; b; cg PQ = fd(32); e(44); f(1); t(1)g 0 9 32 9 24 s a d 14 18 DecreaseKey(e; 35) DecreaseKey(t; 59) 14 2 15 19 b 1 30 44 11 f 6 15 20 e 16 1 44 c t . 18 / 124 ExtractMin returns d Step 5: ExtractMin S = fs; a; b; cg PQ = fd(32); e(35); t(59); f(1)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 1 30 35 11 f 6 15 20 e 16 59 44 c t . 19 / 124 Step 5: ExtractMin S = fs; a; b; cg PQ = fd(32); e(35); t(59); f(1)g 0 9 32 9 24 s a d ExtractMin returns d 14 18 14 2 15 19 b 1 30 35 11 f 6 15 20 e 16 59 44 c t . 19 / 124 Step 5: DecreaseKey S = fs; a; b; c; dg PQ = fe(35); t(59); f(1)g 0 9 32 9 24 s a d 14 18 DecreaseKey(t; 51) DecreaseKey(e; 34) 14 2 15 19 b 1 30 35 11 f 6 15 20 e 16 59 44 c t . 20 / 124 ExtractMin returns e Step 6: ExtractMin S = fs; a; b; c; dg PQ = fe(34); t(51); f(1)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 1 30 34 11 f 6 15 20 e 16 51 44 c t . 21 / 124 Step 6: ExtractMin S = fs; a; b; c; dg PQ = fe(34); t(51); f(1)g 0 9 32 9 24 s a d ExtractMin returns e 14 18 14 2 15 19 b 1 30 34 11 f 6 15 20 e 16 51 44 c t . 21 / 124 Step 6: DecreaseKey S = fs; a; b; c; d; eg PQ = ff(45); t(50)g 0 9 32 9 24 s a d 14 18 DecreaseKey(f; 45) DecreaseKey(t; 50) 14 2 15 19 b 1 30 34 11 f 6 15 20 e 16 51 44 c t . 22 / 124 ExtractMin returns f Step 7: ExtractMin S = fs; a; b; c; d; eg PQ = ff(45); t(50)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 30 45 34 11 f 6 15 20 e 16 50 44 c t . 23 / 124 Step 7: ExtractMin S = fs; a; b; c; d; eg PQ = ff(45); t(50)g 0 9 32 9 24 s a d ExtractMin returns f 14 18 14 2 15 19 b 30 45 34 11 f 6 15 20 e 16 50 44 c t . 23 / 124 Step 7: DecreaseKey S = fs; a; b; c; d; e; fg PQ = ft(50)g 0 9 32 9 24 s a d 14 18 14 2 15 19 b 30 45 34 11 f 6 15 20 e 16 50 44 c t . 24 / 124 Step 8: ExtractMin S = fs; a; b; c; d; e; f; tg PQ = fg 0 9 32 9 24 s a d 14 18 14 2 15 19 b 30 45 34 11 f 6 15 20 e 16 50 44 c t .
Recommended publications
  • An Alternative to Fibonacci Heaps with Worst Case Rather Than Amortized Time Bounds∗
    Relaxed Fibonacci heaps: An alternative to Fibonacci heaps with worst case rather than amortized time bounds¤ Chandrasekhar Boyapati C. Pandu Rangan Department of Computer Science and Engineering Indian Institute of Technology, Madras 600036, India Email: [email protected] November 1995 Abstract We present a new data structure called relaxed Fibonacci heaps for implementing priority queues on a RAM. Relaxed Fibonacci heaps support the operations find minimum, insert, decrease key and meld, each in O(1) worst case time and delete and delete min in O(log n) worst case time. Introduction The implementation of priority queues is a classical problem in data structures. Priority queues find applications in a variety of network problems like single source shortest paths, all pairs shortest paths, minimum spanning tree, weighted bipartite matching etc. [1] [2] [3] [4] [5] In the amortized sense, the best performance is achieved by the well known Fibonacci heaps. They support delete and delete min in amortized O(log n) time and find min, insert, decrease key and meld in amortized constant time. Fast meldable priority queues described in [1] achieve all the above time bounds in worst case rather than amortized time, except for the decrease key operation which takes O(log n) worst case time. On the other hand, relaxed heaps described in [2] achieve in the worst case all the time bounds of the Fi- bonacci heaps except for the meld operation, which takes O(log n) worst case ¤Please see Errata at the end of the paper. 1 time. The problem that was posed in [1] was to consider if it is possible to support both decrease key and meld simultaneously in constant worst case time.
    [Show full text]
  • Data Structures and Algorithms Binary Heaps (S&W 2.4)
    Data structures and algorithms DAT038/TDA417, LP2 2019 Lecture 12, 2019-12-02 Binary heaps (S&W 2.4) Some slides by Sedgewick & Wayne Collections A collection is a data type that stores groups of items. stack Push, Pop linked list, resizing array queue Enqueue, Dequeue linked list, resizing array symbol table Put, Get, Delete BST, hash table, trie, TST set Add, ontains, Delete BST, hash table, trie, TST A priority queue is another kind of collection. “ Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won't usually need your code; it'll be obvious.” — Fred Brooks 2 Priority queues Collections. Can add and remove items. Stack. Add item; remove the item most recently added. Queue. Add item; remove the item least recently added. Min priority queue. Add item; remove the smallest item. Max priority queue. Add item; remove the largest item. return contents contents operation argument value size (unordered) (ordered) insert P 1 P P insert Q 2 P Q P Q insert E 3 P Q E E P Q remove max Q 2 P E E P insert X 3 P E X E P X insert A 4 P E X A A E P X insert M 5 P E X A M A E M P X remove max X 4 P E M A A E M P insert P 5 P E M A P A E M P P insert L 6 P E M A P L A E L M P P insert E 7 P E M A P L E A E E L M P P remove max P 6 E M A P L E A E E L M P A sequence of operations on a priority queue 3 Priority queue API Requirement.
    [Show full text]
  • Priority Queues and Binary Heaps Chapter 6.5
    Priority Queues and Binary Heaps Chapter 6.5 1 Some animals are more equal than others • A queue is a FIFO data structure • the first element in is the first element out • which of course means the last one in is the last one out • But sometimes we want to sort of have a queue but we want to order items according to some characteristic the item has. 107 - Trees 2 Priorities • We call the ordering characteristic the priority. • When we pull something from this “queue” we always get the element with the best priority (sometimes best means lowest). • It is really common in Operating Systems to use priority to schedule when something happens. e.g. • the most important process should run before a process which isn’t so important • data off disk should be retrieved for more important processes first 107 - Trees 3 Priority Queue • A priority queue always produces the element with the best priority when queried. • You can do this in many ways • keep the list sorted • or search the list for the minimum value (if like the textbook - and Unix actually - you take the smallest value to be the best) • You should be able to estimate the Big O values for implementations like this. e.g. O(n) for choosing the minimum value of an unsorted list. • There is a clever data structure which allows all operations on a priority queue to be done in O(log n). 107 - Trees 4 Binary Heap Actually binary min heap • Shape property - a complete binary tree - all levels except the last full.
    [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]
  • CS210-Data Structures-Module-29-Binary-Heap-II
    Data Structures and Algorithms (CS210A) Lecture 29: • Building a Binary heap on 풏 elements in O(풏) time. • Applications of Binary heap : sorting • Binary trees: beyond searching and sorting 1 Recap from the last lecture 2 A complete binary tree How many leaves are there in a Complete Binary tree of size 풏 ? 풏/ퟐ 3 Building a Binary heap Problem: Given 풏 elements {푥0, …, 푥푛−1}, build a binary heap H storing them. Trivial solution: (Building the Binary heap incrementally) CreateHeap(H); For( 풊 = 0 to 풏 − ퟏ ) What is the time Insert(푥,H); complexity of this algorithm? 4 Building a Binary heap incrementally What useful inference can you draw from Top-down this Theorem ? approach The time complexity for inserting a leaf node = ?O (log 풏 ) # leaf nodes = 풏/ퟐ , Theorem: Time complexity of building a binary heap incrementally is O(풏 log 풏). 5 Building a Binary heap incrementally What useful inference can you draw from Top-down this Theorem ? approach The O(풏) time algorithm must take O(1) time for each of the 풏/ퟐ leaves. 6 Building a Binary heap incrementally Top-down approach 7 Think of alternate approach for building a binary heap In any complete 98 binaryDoes treeit suggest, how a manynew nodes approach satisfy to heapbuild propertybinary heap ? ? 14 33 all leaf 37 11 52 32 nodes 41 21 76 85 17 25 88 29 Bottom-up approach 47 75 9 57 23 heap property: “Every ? node stores value smaller than its children” We just need to ensure this property at each node.
    [Show full text]
  • Introduction to Algorithms and Data Structures
    Introduction to Algorithms and Data Structures Markus Bläser Saarland University Draft—Thursday 22, 2015 and forever Contents 1 Introduction 1 1.1 Binary search . .1 1.2 Machine model . .3 1.3 Asymptotic growth of functions . .5 1.4 Running time analysis . .7 1.4.1 On foot . .7 1.4.2 By solving recurrences . .8 2 Sorting 11 2.1 Selection-sort . 11 2.2 Merge sort . 13 3 More sorting procedures 16 3.1 Heap sort . 16 3.1.1 Heaps . 16 3.1.2 Establishing the Heap property . 18 3.1.3 Building a heap from scratch . 19 3.1.4 Heap sort . 20 3.2 Quick sort . 21 4 Selection problems 23 4.1 Lower bounds . 23 4.1.1 Sorting . 25 4.1.2 Minimum selection . 27 4.2 Upper bounds for selecting the median . 27 5 Elementary data structures 30 5.1 Stacks . 30 5.2 Queues . 31 5.3 Linked lists . 33 6 Binary search trees 36 6.1 Searching . 38 6.2 Minimum and maximum . 39 6.3 Successor and predecessor . 39 6.4 Insertion and deletion . 40 i ii CONTENTS 7 AVL trees 44 7.1 Bounds on the height . 45 7.2 Restoring the AVL tree property . 46 7.2.1 Rotations . 46 7.2.2 Insertion . 46 7.2.3 Deletion . 49 8 Binomial Heaps 52 8.1 Binomial trees . 52 8.2 Binomial heaps . 54 8.3 Operations . 55 8.3.1 Make heap . 55 8.3.2 Minimum . 55 8.3.3 Union . 56 8.3.4 Insert .
    [Show full text]
  • CS 270 Algorithms
    CS 270 Algorithms Week 10 Oliver Kullmann Binary heaps Sorting Heapification Building a heap 1 Binary heaps HEAP- SORT Priority 2 Heapification queues QUICK- 3 Building a heap SORT Analysing 4 QUICK- HEAP-SORT SORT 5 Priority queues Tutorial 6 QUICK-SORT 7 Analysing QUICK-SORT 8 Tutorial CS 270 General remarks Algorithms Oliver Kullmann Binary heaps Heapification Building a heap We return to sorting, considering HEAP-SORT and HEAP- QUICK-SORT. SORT Priority queues CLRS Reading from for week 7 QUICK- SORT 1 Chapter 6, Sections 6.1 - 6.5. Analysing 2 QUICK- Chapter 7, Sections 7.1, 7.2. SORT Tutorial CS 270 Discover the properties of binary heaps Algorithms Oliver Running example Kullmann Binary heaps Heapification Building a heap HEAP- SORT Priority queues QUICK- SORT Analysing QUICK- SORT Tutorial CS 270 First property: level-completeness Algorithms Oliver Kullmann Binary heaps In week 7 we have seen binary trees: Heapification Building a 1 We said they should be as “balanced” as possible. heap 2 Perfect are the perfect binary trees. HEAP- SORT 3 Now close to perfect come the level-complete binary Priority trees: queues QUICK- 1 We can partition the nodes of a (binary) tree T into levels, SORT according to their distance from the root. Analysing 2 We have levels 0, 1,..., ht(T ). QUICK- k SORT 3 Level k has from 1 to 2 nodes. Tutorial 4 If all levels k except possibly of level ht(T ) are full (have precisely 2k nodes in them), then we call the tree level-complete. CS 270 Examples Algorithms Oliver The binary tree Kullmann 1 ❚ Binary heaps ❥❥❥❥ ❚❚❚❚ ❥❥❥❥ ❚❚❚❚ ❥❥❥❥ ❚❚❚❚ Heapification 2 ❥ 3 ❖ ❄❄ ❖❖ Building a ⑧⑧ ❄ ⑧⑧ ❖❖ heap ⑧⑧ ❄ ⑧⑧ ❖❖❖ 4 5 6 ❄ 7 ❄ HEAP- ⑧ ❄ ⑧ ❄ SORT ⑧⑧ ❄ ⑧⑧ ❄ Priority 10 13 14 15 queues QUICK- is level-complete (level-sizes are 1, 2, 4, 4), while SORT ❥ 1 ❚❚ Analysing ❥❥❥❥ ❚❚❚❚ QUICK- ❥❥❥ ❚❚❚ SORT ❥❥❥ ❚❚❚❚ 2 ❥❥ 3 ♦♦♦ ❄❄ ⑧ Tutorial ♦♦ ❄❄ ⑧⑧ ♦♦♦ ⑧ 4 ❄ 5 ❄ 6 ❄ ⑧⑧ ❄❄ ⑧ ❄ ⑧ ❄ ⑧⑧ ❄ ⑧⑧ ❄ ⑧⑧ ❄ 8 9 10 11 12 13 is not (level-sizes are 1, 2, 3, 6).
    [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]
  • Lecture 6 — February 9, 2017 1 Wrapping up on Hashing
    CS 224: Advanced Algorithms Spring 2017 Lecture 6 | February 9, 2017 Prof. Jelani Nelson Scribe: Albert Chalom 1 Wrapping up on hashing Recall from las lecture that we showed that when using hashing with chaining of n items into m C log n bins where m = Θ(n) and hash function h :[ut] ! m that if h is from log log n wise family, then no C log n linked list has size > log log n . 1.1 Using two hash functions Azar, Broder, Karlin, and Upfal '99 [1] then used two hash functions h; g :[u] ! [m] that are fully random. Then ball i is inserted in the least loaded of the 2 bins h(i); g(i), and ties are broken randomly. log log n Theorem: Max load is ≤ log 2 + O(1) with high probability. When using d instead of 2 hash log log n functions we get max load ≤ log d + O(1) with high probability. n 1.2 Breaking n into d slots n V¨ocking '03 [2] then considered breaking our n buckets into d slots, and having d hash functions n h1; h2; :::; hd that hash into their corresponding slot of size d . Then insert(i), loads i in the least loaded of the h(i) bins, and breaks tie by putting i in the left most bin. log log n This improves the performance to Max Load ≤ where 1:618 = φ2 < φ3::: ≤ 2. dφd 1.3 Survey: Mitzenmacher, Richa, Sitaraman [3] Idea: Let Bi denote the number of bins with ≥ i balls.
    [Show full text]
  • Data Structures and Programming Spring 2016, Final Exam
    Data Structures and Programming Spring 2016, Final Exam. June 21, 2016 1 1. (15 pts) True or False? (Mark for True; × for False. Score = maxf0, Right - 2 Wrongg. No explanations are needed. (1) Let A1;A2, and A3 be three sorted arrays of n real numbers (all distinct). In the comparison model, constructing a balanced binary search tree of the set A1 [ A2 [ A3 requires Ω(n log n) time. × False (Merge A1;A2;A3 in linear time; then pick recursively the middle as root (in linear time).) (2) Let T be a complete binary tree with n nodes. Finding a path from the root of T to a given vertex v 2 T using breadth-first search takes O(log n) time. × False (Note that the tree is NOT a search tree.) (3) Given an unsorted array A[1:::n] of n integers, building a max-heap out of the elements of A can be performed asymptotically faster than building a red-black tree out of the elements of A. True (O(n) for building a max-heap; Ω(n log n) for building a red-black tree.) (4) In the worst case, a red-black tree insertion requires O(1) rotations. True (See class notes) (5) In the worst case, a red-black tree deletion requires O(1) node recolorings. × False (See class notes) (6) Building a binomial heap from an unsorted array A[1:::n] takes O(n) time. True (See class notes) (7) Insertion into an AVL tree asymptotically beats insertion into an AA-tree. × False (See class notes) (8) The subtree of the root of a red-black tree is always itself a red-black tree.
    [Show full text]
  • Data Structures Binary Trees and Their Balances Heaps Tries B-Trees
    Data Structures Tries Separated chains ≡ just linked lists for each bucket. P [one list of size exactly l] = s(1=m)l(1 − 1=m)n−l. Since this is a Note: Strange structure due to finals topics. Will improve later. Compressing dictionary structure in a tree. General tries can grow l P based on the dictionary, and quite fast. Long paths are inefficient, binomial distribution, easily E[chain length] = (lP [len = l]) = α. Binary trees and their balances so we define compressed tries, which are tries with no non-splitting Expected maximum list length vertices. Initialization O(Σl) for uncompressed tries, O(l + Σ) for compressed (we are only making one initialization of a vertex { the X s j AVL trees P [max len(j) ≥ j) ≤ P [len(i) ≥ j] ≤ m (1=m) last one). i j i Vertices contain -1,0,+1 { difference of balances between the left and T:Assuming all l-length sequences come with uniform probability and we have a sampling of size n, E[c − trie size] = log d. j−1 the right subtree. Balance is just the maximum depth of the subtree. k Q (s − k) P = k=0 (1=m)j−1 ≤ s(s=m)j−1=j!: Analysis: prove by induction that for depth k, there are between Fk P:qd ≡ probability trie has depth d. E[c − triesize] = d d(qd − j! k P and 2 vertices in every tree. Since balance only shifts by 1, it's pretty qd+1) = d qd. simple. Calculate opposite event { trie is within depth d − 1.
    [Show full text]
  • Readings Findmin Problem Priority Queue
    Readings • Chapter 6 Priority Queues & Binary Heaps › Section 6.1-6.4 CSE 373 Data Structures Winter 2007 Binary Heaps 2 FindMin Problem Priority Queue ADT • Quickly find the smallest (or highest priority) • Priority Queue can efficiently do: item in a set ›FindMin() • Applications: • Returns minimum value but does not delete it › Operating system needs to schedule jobs according to priority instead of FIFO › DeleteMin( ) › Event simulation (bank customers arriving and • Returns minimum value and deletes it departing, ordered according to when the event › Insert (k) happened) • In GT Insert (k,x) where k is the key and x the value. In › Find student with highest grade, employee with all algorithms the important part is the key, a highest salary etc. “comparable” item. We’ll skip the value. › Find “most important” customer waiting in line › size() and isEmpty() Binary Heaps 3 Binary Heaps 4 List implementation of a Priority BST implementation of a Priority Queue Queue • What if we use unsorted lists: • Worst case (degenerate tree) › FindMin and DeleteMin are O(n) › FindMin, DeleteMin and Insert (k) are all O(n) • In fact you have to go through the whole list • Best case (completely balanced BST) › Insert(k) is O(1) › FindMin, DeleteMin and Insert (k) are all O(logn) • What if we used sorted lists • Balanced BSTs › FindMin and DeleteMin are O(1) › FindMin, DeleteMin and Insert (k) are all O(logn) • Be careful if we want both Min and Max (circular array or doubly linked list) › Insert(k) is O(n) Binary Heaps 5 Binary Heaps 6 1 Better than a speeding BST Binary Heaps • A binary heap is a binary tree (NOT a BST) that • Can we do better than Balanced Binary is: Search Trees? › Complete: the tree is completely filled except • Very limited requirements: Insert, possibly the bottom level, which is filled from left to FindMin, DeleteMin.
    [Show full text]