CSE 373 Data Structures Lecture 12

Slides:



Advertisements
Similar presentations
 Have to copy one array into another (N)
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Nick Harvey & Kevin Zatloukal
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
Splay Trees and B-Trees
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
§8 Binomial Queues Haven’t we had enough about queues? What is a binomial queue for? Well, what is the average time for insertions with leftist or skew.
Christopher Moh 2005 Application of Data Structures.
Binomial Queues Text Read Weiss, §6.8 Binomial Queue Definition of binomial queue Definition of binary addition Building a Binomial Queue Sequence of inserts.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Chapter 21 Binary Heap.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Cpt S 223 – Advanced Data Structures Priority Queues
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
1 CSE 326 Data Structures Part 6: Priority Queues, AKA Heaps Henry Kautz Autumn 2002.
Change Keys in heaps Fibonacci heap Zhao Xiaobin.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Review for Exam 2 Topics covered (since exam 1): –Splay Tree –K-D Trees –RB Tree –Priority Queue and Binary Heap –B-Tree For each of these data structures.
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Exam 3 Review Data structures covered: –Hashing and Extensible hashing –Priority queues and binary heaps –Skip lists –B-Tree –Disjoint sets For each of.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Priority Queues (Heaps)
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Priority Queues Chuan-Ming Liu
Hashing Exercises.
Part-D1 Priority Queues
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
CMSC 341 Lecture 13 Leftist Heaps
© 2013 Goodrich, Tamassia, Goldwasser
Fundamental Structures of Computer Science
Heap Sort CSE 2011 Winter January 2019.
Binary and Binomial Heaps
Priority Queues CSE 373 Data Structures.
Priority Queues Supports the following operations. Insert element x.
CS 6310 Advanced Data Structure Wei-Shian Wang
Fundamental Structures of Computer Science II
CSE 373 Data Structures Lecture 12
Heaps & Multi-way Search Trees
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

CSE 373 Data Structures Lecture 12 Binomial Queues CSE 373 Data Structures Lecture 12

Binomial Queues - Lecture 12 Reading Reading Section 6.8, 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Merging heaps Binary Heap has limited (fast) functionality FindMin, DeleteMin and Insert only does not support fast merges of two heaps For some applications, the items arrive in prioritized clumps, rather than individually Is there somewhere in the heap design that we can give up a little performance so that we can gain faster merge capability? 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Binomial Queues are designed to be merged quickly with one another Using pointer-based design we can merge large numbers of nodes at once by simply pruning and grafting tree structures More overhead than Binary Heap, but the flexibility is needed for improved merging speed 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Worst Case Run Times Binary Heap Binomial Queue Insert (log N) (log N) FindMin (1) O(log N) DeleteMin (log N) (log N) Merge (N) O(log N) 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Binomial queues give up (1) FindMin performance in order to provide O(log N) merge performance A binomial queue is a collection (or forest) of heap-ordered trees Not just one tree, but a collection of trees each tree has a defined structure and capacity each tree has the familiar heap-order property 4/25/03 Binomial Queues - Lecture 12

Binomial Queue with 5 Trees depth 4 3 2 1 number of elements 24 = 16 23 = 8 22 = 4 21 = 2 20 = 1 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Structure Property Each tree contains two copies of the previous tree the second copy is attached at the root of the first copy The number of nodes in a tree of depth d is exactly 2d B2 B1 B0 depth 2 1 number of elements 22 = 4 21 = 2 20 = 1 4/25/03 Binomial Queues - Lecture 12

Powers of 2 (one more time) Any number N can be represented in base 2: A base 2 value identifies the powers of 2 that are to be included 23 = 810 22 = 410 21 = 210 20 = 110 Hex16 Decimal10 1 1 3 3 1 4 4 1 1 5 5 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Numbers of nodes Any number of entries in the binomial queue can be stored in a forest of binomial trees Each tree holds the number of nodes appropriate to its depth, i.e., 2d nodes So the structure of a forest of binomial trees can be characterized with a single binary number 1012  1·22 + 0·21 + 1·20 = 5 nodes 4/25/03 Binomial Queues - Lecture 12

Structure Examples 4 4 8 5 8 7 22 = 4 21 = 2 20 = 1 22 = 4 21 = 2 N=210=102 22 = 4 21 = 2 20 = 1 N=410=1002 22 = 4 21 = 2 20 = 1 4 9 4 9 8 5 8 7 N=310=112 22 = 4 21 = 2 20 = 1 N=510=1012 22 = 4 21 = 2 20 = 1

Binomial Queues - Lecture 12 What is a merge? There is a direct correlation between the number of nodes in the tree the representation of that number in base 2 and the actual structure of the tree When we merge two queues of sizes N1 and N2, the number of nodes in the new queue is the sum of N1+N2 We can use that fact to help see how fast merges can be accomplished 4/25/03 Binomial Queues - Lecture 12

There are no comparisons and there is no restructuring. + BQ.2 9 BQ.1 Example 1. Merge BQ.1 and BQ.2 Easy Case. There are no comparisons and there is no restructuring. N=110=12 22 = 4 21 = 2 20 = 1 4 + BQ.2 8 N=210=102 22 = 4 21 = 2 20 = 1 4 9 = BQ.3 8 N=310=112 22 = 4 21 = 2 20 = 1

This is an add with a carry out. Example 2. Merge BQ.1 and BQ.2 This is an add with a carry out. It is accomplished with one comparison and one pointer change: O(1) 1 BQ.1 3 N=210=102 22 = 4 21 = 2 20 = 1 4 + BQ.2 6 N=210=102 22 = 4 21 = 2 20 = 1 1 = BQ.3 4 3 6 N=410=1002 22 = 4 21 = 2 20 = 1

BQ.1 Example 3. Merge BQ.1 and BQ.2 Part 1 - Form the carry. + BQ.2 7 Example 3. Merge BQ.1 and BQ.2 Part 1 - Form the carry. BQ.1 3 N=310=112 22 = 4 21 = 2 20 = 1 4 8 + BQ.2 6 N=310=112 22 = 4 21 = 2 20 = 1 7 = carry 8 N=210=102 22 = 4 21 = 2 20 = 1

Part 2 - Add the existing values and the carry. + BQ.2 7 1 7 + BQ.1 carry 8 3 N=210=102 22 = 4 21 = 2 20 = 1 N=310=112 22 = 4 21 = 2 20 = 1 4 8 Example 3. Part 2 - Add the existing values and the carry. + BQ.2 6 N=310=112 22 = 4 21 = 2 20 = 1 1 7 = BQ.3 4 3 8 6 N=610=1102 22 = 4 21 = 2 20 = 1

Binomial Queues - Lecture 12 Merge Algorithm Just like binary addition algorithm Assume trees X0,…,Xn and Y0,…,Yn are binomial queues Xi and Yi are of type Bi or null C0 := null; //initial carry is null// for i = 0 to n do combine Xi,Yi, and Ci to form Zi and new Ci+1 Zn+1 := Cn+1 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Exercise 4 9 2 13 1 8 7 10 15 12 N=310=112 22 = 4 21 = 2 20 = 1 N=710=1112 22 = 4 21 = 2 20 = 1 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 O(log N) time to Merge For N keys there are at most log2 N trees in a binomial forest. Each merge operation only looks at the root of each tree. Total time to merge is O(log N). 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Insert Create a single node queue B0 with the new item and merge with existing queue O(log N) time 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 DeleteMin Assume we have a binomial forest X0,…,Xm Find tree Xk with the smallest root Remove Xk from the queue Remove root of Xk (return this value) This yields a binomial forest Y0, Y1, …,Yk-1. Merge this new queue with remainder of the original (from step 3) Total time = O(log N) 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Implementation Binomial forest as an array of multiway trees FirstChild, Sibling pointers 0 1 2 3 4 5 6 7 1 2 5 5 2 1 4 7 10 9 9 4 7 10 13 8 12 15 13 8 12 15 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 DeleteMin Example FindMin 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 Remove min 5 2 1 5 2 9 4 7 10 9 4 7 10 13 8 12 13 8 12 15 15 Return this 1 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 Old forest 5 2 5 2 9 9 New forest 0 1 2 3 4 5 6 7 4 7 10 13 8 12 10 7 4 15 12 13 8 15 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 5 2 Merge 9 5 2 0 1 2 3 4 5 6 7 10 4 7 9 13 8 12 10 7 4 15 12 13 8 15 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Why Binomial? B4 B3 B2 B1 B0 tree depth d 4 3 2 1 nodes at depth k 1, 4, 6, 4, 1 1, 3, 3, 1 1, 2, 1 1, 1 1 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Other Priority Queues Leftist Heaps O(log N) time for insert, deletemin, merge The idea is to have the left part of the heap be long and the right part short, and to perform most operations on the left part. Skew Heaps (“splaying leftist heaps”) O(log N) amortized time for insert, deletemin, merge 4/25/03 Binomial Queues - Lecture 12

Binomial Queues - Lecture 12 Exercise Solution 4 9 2 13 1 + 8 7 10 15 12 2 1 4 7 10 9 13 8 12 15 4/25/03 Binomial Queues - Lecture 12