CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
1 CSE 326: Data Structures Priority Queues – Binary Heaps.
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
Nick Harvey & Kevin Zatloukal
CSE 373 Data Structures Lecture 12
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.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
CSE 326: Data Structures Lecture #13 Priority Queues and Binary Heaps Nick Deibel Winter Quarter 2002.
§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.
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.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2010W2 1.
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.
Priority Queues (Heaps)
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Dan Grossman Spring 2012.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
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)
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Internal and External Sorting External Searching
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
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.
1 Chapter 6: Priority Queues, AKA Heaps. 2 Queues with special properties Consider applications –ordering CPU jobs –searching for the exit in a maze (or.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
CSE 326: Data Structures Priority Queues (Heaps)
October 30th – Priority QUeues
Hashing Exercises.
CMSC 341 Lecture 13 Leftist Heaps
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE 326: Data Structures Lecture #4 Heaps more Priority Qs
CMSC 341 Lecture 14 Priority Queues & Heaps
Heaps and the Heapsort Heaps and priority queues
Heap Sort CSE 2011 Winter January 2019.
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CSE 373 Data Structures and Algorithms
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
CSE 326: Data Structures Lecture #5 Heaps More
Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures Lecture 12
Heaps & Multi-way Search Trees
CSE 373: Data Structures and Algorithms
Presentation transcript:

CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007

2 Adminstrivia Due today: Homework #1 Released today: Homework #2

3 BuildHeap: Floyd’s Method Add elements arbitrarily to form a complete tree. Pretend it’s a heap and fix the heap-order property!

4 Buildheap pseudocode private void buildHeap() { for ( int i = currentSize/2; i > 0; i-- ) percolateDown( i ); } runtime:

5 BuildHeap: Floyd’s Method

6 Finally… runtime:

7 Facts about Heaps Observations: finding a child/parent index is a multiply/divide by two operations jump widely through the heap each percolate step looks at only two new nodes inserts are at least as common as deleteMins Realities: division/multiplication by powers of two are equally fast looking at only two new pieces of data: bad for cache! with huge data sets, disk accesses dominate

8 CPU – 1 cycle Cache – ~10 cycles Memory – ~200 cycles Disk – ~200,000 cycles Cycles to access:

A Solution: d-Heaps Each node has d children Still representable by array Good choices for d: –(choose a power of two for efficiency) –fit one set of children in a cache line –fit one set of children on a memory page/disk block

10 Operations on d-Heap Insert : runtime = deleteMin: runtime = Does this help insert or deleteMin more?

11 One More Operation Merge two heaps. Ideas?

12 New Operation: Merge Given two heaps, merge them into one –First attempt: aren’t two heaps “just like” one heap after deleteMin? NO. Violates the completeness property –Second attempt: Foreach element in smaller heap, insert into larger one Runtime? –Third attempt: Concatenate the arrays of the two heaps, then run buildHeap Runtime?

13 Merging heaps Binary Heap is a special purpose hot rod –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?

14 Binomial Queues 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

15 Worst Case Run Times Insert FindMin DeleteMin Merge  (log N)  (1)  (N)  (log N) Binary Heap  (log N) O(log N)  (log N) Binomial Queue

16 Binomial Queues 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

17 Binomial Queue with 5 Trees B0B0 B1B1 B2B2 B3B3 B4B4 depth number of elements = = = = = 1

18 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 2 d B0B0 B1B1 B2B2 depth number of elements = = = 1

19 Powers of 2 Any number N can be represented in base 2 –A base 2 value identifies the powers of 2 that are to be included 2 0 = = = = 8 10 Hex 16 Decimal

20 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, ie 2 d nodes So the structure of a forest of binomial trees can be characterized with a single binary number –100 2  1· · ·2 0 = 4 nodes

Structure Examples 4 8 N=2 10 = = = = 42 1 = 22 0 = = 4 N=3 10 =11 2 N=4 10 =100 2 N=5 10 = = = = 2

22 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, the number of nodes in the new queue is the sum of N 1 +N 2 We can use that fact to help see how fast merges can be accomplished

4 8 N=2 10 = = = 1 N=3 10 = = = 1 N=1 10 = = 22 2 = 42 0 = 1 9 Example 1. Merge BQ.1 and BQ.2 Easy Case. There are no comparisons and there is no restructuring. BQ.1 + BQ.2 = BQ.3

4 6 N=2 10 = = = 1 N=4 10 = = = 1 N=2 10 = = 22 2 = 42 0 = 1 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) BQ.1 + BQ.2 = BQ

4 6 N=3 10 = = = 1 N=2 10 = = = 1 N=3 10 = = 22 2 = 42 0 = 1 Example 3. Merge BQ.1 and BQ.2 Part 1 - Form the carry. BQ.1 + BQ.2 = carry

4 6 N=3 10 = = = 1 N=6 10 = = = 1 N=3 10 = = 22 2 = 42 0 = 1 Example 3. Part 2 - Add the existing values and the carry. + BQ.1 + BQ.2 = BQ = 22 0 = 1 N=2 10 = = 4 carry

27 Merge Algorithm Just like binary addition algorithm Assume trees X 0,…,X n and Y 0,…,Y n are binomial queues –X i and Y i are of type B i or null C 0 := null; //initial carry is null// for i = 0 to n do combine X i,Y i, and C i to form Z i and new C i+1 Z n+1 := C n+1

28 Exercise = 22 0 = = 42 1 = 22 0 = 1 N=3 10 =11 2 N=5 10 = =

29 O(log N) time to Merge For N keys there are at most  log 2 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).

30 Insert Create a single node queue B 0 with the new item and merge with existing queue O(log N) time

31 DeleteMin 1.Assume we have a binomial forest X 0,…,X m 2.Find tree X k with the smallest root 3.Remove X k from the queue 4.Remove root of X k (return this value) –This yields a binomial forest Y 0, Y 1, …,Y k-1. 5.Merge this new queue with remainder of the original (from step 3) Total time = O(log N)

32 Implementation Binomial forest as an array of multiway trees –FirstChild, Sibling pointers –Sibling pointers act like a linked list

33 DeleteMin Example 1 And return this

34 DeleteMin step 1: remove min 1 Return this Keep these

New forest Old forest DeleteMin step 2: new forest

Merge DeleteMin step 3: merge forests

37 Merge in detail

38 Merge step 1: 1-trees Carry out

39 97 Merge step 2: 2-trees Carry out Carry in Merging two trees is just a stack push! 7 12

40 Merge step 3: 4-trees Carry in 2 Carry out

41 Merge step 4: 8-trees Carry in

42 Why Binomial? B0B0 B1B1 B2B2 B3B3 B4B4 tree depth d nodes at depth k 4 1, 4, 6, 4, 1 3 1, 3, 3, 1 2 1, 2, 1 1 1, 1 0 1

43 Other Priority Queues Leftist Heaps –O(log N) time for insert, deletemin, merge Skew Heaps –O(log N) amortized time for insert, deletemin, merge Calendar Queues –O(1) average time for insert and deletemin –Assuming insertions are “random”

44 Exercise Solution