2110211 Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.

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.
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
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
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
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.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
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.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
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.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
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.
Priority Queues (Heaps)
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
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.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2012.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
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)
CS 201 Data Structures and Algorithms
Priority Queues (Heaps)
Binary Heaps Priority Queues
Source: Muangsin / Weiss
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
Binary Heaps Priority Queues
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queue and Binary Heap Neil Tang 02/12/2008
BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a.
CSE332: Data Abstractions Lecture 4: Priority Queues
Binary Heaps Priority Queues
CE 221 Data Structures and Algorithms
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Heaps Priority Queues.
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Tree.
Priority Queues Binary Heaps
Presentation transcript:

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value (key) of each object (smaller value higher priority, or higher value higher priority) Example Applications: –printer -> print (dequeue) the shortest document first –operating system -> run (dequeue) the shortest job first –normal queue -> dequeue the first enqueued element first

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 2 Priority Queue (Heap) Operations insert (enqueue) deleteMin (dequeue) –smaller value higher priority –find, return, and remove the minimum element Priority Queue insertdeleteMin

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 3 Implementation using Linked List Unsorted linked list –insert takes O(1) time –deleteMin takes O(N) time Sorted linked list –insert takes O(N) time –deleteMin takes O(1) time

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 4 Implementation using Binary Search Tree insert takes O(log N) time deleteMin takes O(log N) time support other operations that are not required by priority queue (for example, findMax) deleteMin make the tree unbalanced

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 5 Binary Heap binary tree completely filled (bottom level is filled from left to right size between 2 h (bottom level has only one node) and 2 h+1 -1 A C G F B E J D HI

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 6 Array Implementation of Binary Heap A C G F B E J D HI ABCDEFGHI J left child is in position 2i right child is in position (2i+1) parent is in position i/2

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 7 Heap Order Property (for Minimum Heap) Any node is smaller than (or equal to) all of its children (any subtree is a heap) Smallest element is at the root (findMin take O(1) time)

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 8 Insert Create a hole in the next available location Move the hole up (swap with its parent) until data can be placed in the hole without violating the heap order property (called percolate up)

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 9 Insert Percolate Up -> move the place to put 14 up (move its parent down) until its parent <= 14 insert 14

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 10 Insert

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 11 deleteMin Create a hole at the root Move the hole down (swap with the smaller one of its children) until the last element of the heap can be placed in the hole without violating the heap order property (called percolate down)

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 12 deleteMin Percolate Down -> move the place to put 31 down (move its smaller child up) until its children >= 31

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 13 deleteMin

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 14 deleteMin

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 15 Running Time insert –worst case: takes O(log N) time, moves an element from the bottom to the top –on average: takes a constant time (2.607 comparisons), moves an element up levels deleteMin –worst case: takes O(log N) time –on average: takes O(log N) time (element that is placed at the root is large, so it is percolated almost to the bottom)

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 16 public class BinaryHeap { private static final int DEFAULT_CAPACITY = 100; private int currentSize; private Comparable [ ] array; public BinaryHeap( ) public BinaryHeap( int capacity ) public void insert( Comparable x ) throws Overflow public Comparable findMin( ) public Comparable deleteMin( )

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 17 public boolean isEmpty( ) public boolean isFull( ) public void makeEmpty( ) private void percolateDown( int hole ) private void buildHeap( ) }

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 18 public static void main( String [ ] args ) { int numItems = 10000; BinaryHeap h = new BinaryHeap( numItems ); int i = 37; for( i = 37; i != 0; i = ( i + 37 ) % numItems ) h.insert( new MyInteger( i ) ); for( i = 1; i < numItems; i++ ) if( ((MyInteger)( h.deleteMin( ) )).intValue( ) != i ) System.out.println( "Oops! " + i ); }

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 19 public BinaryHeap( ) { this( DEFAULT_CAPACITY ); } public BinaryHeap( int capacity ) { currentSize = 0; array = new Comparable[ capacity + 1 ]; } public void makeEmpty( ) { currentSize = 0; }

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 20 public void insert( Comparable x ) throws Overflow { if( isFull( ) ) throw new Overflow( ); int hole = ++currentSize; for( ; hole > 1 && x.compareTo( array[ hole / 2 ] ) < 0; hole /= 2 ) array[ hole ] = array[ hole / 2 ]; array[ hole ] = x; } public Comparable deleteMin( ) { if( isEmpty( ) ) return null; Comparable minItem = findMin( ); array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); return minItem; }

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 21 private void percolateDown( int hole ) { int child; Comparable tmp = array[ hole ]; for( ; hole * 2 <= currentSize; hole = child ) { child = hole * 2; if( child != currentSize && array[ child + 1 ].compareTo( array[ child ] ) < 0 ) child++; if( array[ child ].compareTo( tmp ) < 0 ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp; }

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 22 public boolean isEmpty( ) { return currentSize == 0; } public boolean isFull( ) { return currentSize == array.length - 1; } private void buildHeap( ) { for( int i = currentSize / 2; i > 0; i-- ) percolateDown( i ); } public Comparable findMin( ) { if( isEmpty( ) ) return null; return array[ 1 ]; }

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 23 Other Operations buildHeap –insert N times decreaseKey(p,  ) –decrease the value of the key and percolate up increaseKey(p,  ) –increase the value of the key and percolate down delete –decreaseKey(p, ฅ  ) and deleteMin( )

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 24 buildHeap insert N times each insert takes O(1) average time, O(log N) worst-case time buildHeap takes O(N) average time, O(N log N) worst-case time

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 25 buildHeap: another method Place N items in to the tree in any order and percolate down every node from bottom up to the root Worst-case time of buildHeap is the sum of worst-case time to percolate these nodes, or the sum of heights of these nodes This method has O(N) worst-case time

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 26 Application: Selection Problem Find the k th smallest element in a list Algorithm A –buildHeap from all elements and deleteMin k times –running time O(N + k log N) –if k = O(N / log N) then running time is O(N) –if k = O(N) then running time is O(N log N) –if k = N and record the values deleted by deleteMin --> heap sort

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 27 Application: Selection Problem Algorithm B (find the k th largest element) –Maintain a set S of k largest elements all the time –buildHeap from first k elements, the k th largest element (the smallest in S, called S k ) is on top –If the next element in the input is larger than S k, then S k is removed and the new one is inserted into S –Running time is O(k) + O(N-k) + O((N-k)log k) = O(N log k)

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 28 Application: Event Simulation Simulation of a bank with k tellers and C customers Each customer causes two events, arrival and departure. Each event has a timestamp Arrival events are put into a queue and departure events are put into a priority queue Find event that should occur next and process it Running time is O(C log(k+1))

Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 29 d-Heaps In a d-heap, all nodes have d children A binary heap is a 2-heap insert takes O(log d N) deleteMin takes O(d log d N)