Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
The Heap ADT In this section of notes you will learn about a new abstract data type, the heap, as well how heaps can be used.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
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.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Sorting with Heaps Observation: Removal of the largest item from a heap can be performed in O(log n) time Another observation: Nodes are removed in order.
Binary Heap.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps & Priority Queues
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Internal and External Sorting External Searching
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
HeapSort 25 March HeapSort Heaps or priority queues provide a means of sorting: 1.Construct a heap, 2.Add each item to it (maintaining the heap.
CS 367 Introduction to Data Structures Lecture 8.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
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)
"Teachers open the door, but you must enter by yourself. "
Priority Queues and Heaps
October 30th – Priority QUeues
Heap Sort Example Qamar Abbas.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
ADT Heap data structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Wednesday, April 18, 2018 Announcements… For Today…
Priority Queue & Heap CSCI 3110 Nan Chen.
Section 10 Questions Heaps.
B-Tree Insertions, Intro to Heaps
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
Hassan Khosravi / Geoffrey Tien
"Teachers open the door, but you must enter by yourself. "
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Tables and Priority Queues
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Presentation transcript:

Priority Queues and Heaps

October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the back of the queue  remove – remove an item from the front  peek – return the item at the front of the queue without removing it  It is assumed that these operations will be implemented efficiently  That is, in constant time

 Either with an array  Or with a linked list October 2004John Edgar front 4 6 back 7 3

 Queues are first-in first-out (FIFO)  Priority queues are a fancier type of queue  Maintains an ordering of items in the queue, not necessarily first-in first-out October 2004John Edgar4

September 2004John Edgar

 Items in a priority queue are given a priority value  Which could be numerical or something else  The highest priority item is removed first  Uses include  System requests  Data structure to support Dijkstra’s Algorithm September 2004John Edgar6

 Can items be inserted and removed efficiently from a priority queue?  Using an array, or  Using a linked list?  Note that items are not removed based on the order in which they are inserted September 2004John Edgar7 Now we’ll see how we can do these efficiently (using a different data structure)

 Items in a priority queue have a priority  Not necessarily numerical  Could be lowest first or highest first  The highest priority item is removed first  Priority queue operations  Insert  Remove in priority queue order ▪ Both operations should be performed in at most O(log n) time October 2004John Edgar8

September 2004John Edgar9

 Items have to be removed in priority order  This can only be done efficiently if the items are ordered in some way  One option would be to use a balanced binary search tree  Binary search trees are fully ordered and insertion and removal can be implemented in O(log n) time ▪ Some operations (e.g. removal) are complex ▪ Although operations are O(logn) they require quite a lot of structural overhead  There is a much simpler binary tree solution October 2004John Edgar10

 A heap is binary tree with two properties  Heaps are complete  All levels, except the bottom, are completely filled in  The leaves on the bottom level are as far to the left as possible.  Heaps are partially ordered  The value of a node is at least as large as its children’s values, for a max heap or  The value of a node is no greater than its children’s values, for a min heap October 2004John Edgar11

October 2004John Edgar12 complete binary trees incomplete binary trees

October 2004John Edgar Heaps are not fully ordered, an inorder traversal would result in: Heaps are not fully ordered, an inorder traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 17, 41, 29 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 17, 41, 29

 A heap can implement a priority queue  The item at the top of the heap must always be the highest priority value  Because of the partial ordering property  Implement priority queue operations:  Insertions – insert an item into a heap  Removal – remove and return the heap’s root  For both operations preserve the heap property October 2004John Edgar14

September 2004John Edgar15

 Heaps can be implemented using arrays  There is a natural method of indexing tree nodes  Index nodes from top to bottom and left to right as shown on the right  Because heaps are complete binary trees there can be no gaps in the array October 2004John Edgar

 It will be necessary to find the index of the parents of a node  Or the children of a node  The array is indexed from 0 to n – 1  Each level's nodes are indexed from: ▪ 2 level – 1 to 2 level+1 – 2 (where the root is level 0)  The children of a node i, are the array elements indexed at 2i + 1 and 2i + 2  The parent of a node i, is the array element indexed at floor((i – 1) / 2) October 2004John Edgar17

index value October 2004John Edgar Heap Underlying Array

September 2004John Edgar19

 On insertion the heap properties have to be maintained; remember that  A heap is a complete binary tree and  A partially ordered binary tree  There are two general strategies that could be used to maintain the heap properties  Make sure that the tree is complete and then fix the ordering or  Make sure the ordering is correct first  Which is better? October 2004John Edgar20

 The insertion algorithm first ensures that the tree is complete  Make the new item the first available (left-most) leaf on the bottom level  i.e. the first free element in the underlying array  Fix the partial ordering  Compare the new value to its parent  Swap them if the new value is greater than the parent  Repeat until this is not the case ▪ Referred to as bubbling up, or trickling up October 2004John Edgar21

October 2004John Edgar22 Insert 81 Insert 81 index value

October 2004John Edgar23 Insert 81 index value (13-1)/2 = 6 81 is less than 98 so finished 81 is less than 98 so finished 8129 index value index value index value

September 2004John Edgar24

 Make a temporary copy of the root’s data  Similarly to the insertion algorithm, first ensure that the heap remains complete  Replace the root node with the right-most leaf  i.e. the highest (occupied) index in the array  Swap the new root with its largest valued child until the partially ordered property holds  i.e. bubble down  Return the root’s data October 2004John Edgar25

index value October 2004John Edgar Remove (root) Remove (root)

index value October 2004John Edgar Remove (root) replace root with right-most leaf replace root with right-most leaf 17 index value

17 index value October 2004John Edgar Remove (root) swap with larger child ?? children of root: 2*0+1, 2*0+2 = 1, 2 17 index value

41 index value index value October 2004John Edgar Remove (root) children of 1: 2*1+1, 2*1+2 = 3, 4 17 ?? swap with larger child

17 index value index value October 2004John Edgar Remove (root) children of 4: 2*4+1, 2*4+2 = 9, 10 swap with larger child ? ?

September 2004John Edgar31

 Helper functions are usually written for preserving the heap property  bubbleUp ensures that the heap property is preserved from the start node up to the root  bubbleDown ensures that the heap property is preserved from the start node down to the leaves  These functions may be implemented recursively or iteratively October 2004John Edgar32

Go to terminal October 2004John Edgar33

October 2004John Edgar34

October 2004John Edgar35  Lab next week

October 2004John Edgar36

 For both insertion and removal the heap performs at most height swaps  For insertion at most height comparisons ▪ To bubble up the array  For removal at most height * 2 comparisons ▪ To bubble down the array (have to compare two children)  Height of a complete binary tree is  log 2 (n)   Both insertion and removal are therefore O(logn) October 2004John Edgar37

September 2004John Edgar38

 Observation 1: Removal of a node from a heap can be performed in O(logn) time  Observation 2: Nodes are removed in order  Conclusion: Removing all of the nodes one by one would result in sorted output  Analysis: Removal of all the nodes from a heap is a O(n*logn) operation October 2004John Edgar39

AA heap can be used to return sorted data IIn O(n*logn) time HHowever, we can’t assume that the data to be sorted just happens to be in a heap! AAha! But we can put it in a heap. IInserting an item into a heap is a O(logn) operation so inserting n items is O(n*logn) BBut we can do better than just repeatedly calling the insertion algorithm October 2004John Edgar40

 To create a heap from an unordered array repeatedly call bubbleDown  Any subtree in a heap is itself a heap  Call bubbleDown on elements in the upper ½ of the array  Start with index n/2 and work up to index 0 ▪ i.e. from the last non-leaf node to the root  bubbleDown does not need to be called on the lower half of the array (the leaves)  Since bubbleDown restores the partial ordering from any given node down to the leaves October 2004John Edgar41

October 2004John Edgar Assume unsorted input is contained in an array as shown here (indexed from top to bottom and left to right)

October 2004John Edgar n = 12, (n-1)/2 = 5 bubbleDown(5)

October 2004John Edgar bubbleDown(5) bubbleDown(4) n = 12, (n-1)/2 = 5

October 2004John Edgar bubbleDown(5) bubbleDown(4) bubbleDown(3) n = 12, (n-1)/2 = 5

October 2004John Edgar bubbleDown(5) bubbleDown(4) bubbleDown(3) bubbleDown(2) n = 12, (n-1)/2 = 5

October 2004John Edgar bubbleDown(5) bubbleDown(4) bubbleDown(3) bubbleDown(2) 2976 bubbleDown(1) 2948 n = 12, (n-1)/2 = 5

October 2004John Edgar bubbleDown(5) bubbleDown(4) bubbleDown(3) bubbleDown(2) bubbleDown(1) bubbleDown(0) 9489 n = 12, (n-1)/2 = 5

 bubbleDown is called on half the array  The cost for bubbleDown is O(height)  It would appear that heapify cost is O(n*logn)  In fact the cost is O(n)  The analysis is complex but  bubbleDown is only called on n/2 nodes  and mostly on sub-trees October 2004John Edgar49

 Heapify the array  Repeatedly remove the root  After each removal swap the root with the last element in the tree  The array is divided into a heap part and a sorted part  At the end of the sort the array will be sorted in reverse order October 2004John Edgar50

 The algorithm runs in O(n*logn) time  Considerably more efficient than selection sort and insertion sort  The same average case complexity as MergeSort and QuickSort  The sort can be carried out in-place  That is, it does not require that a copy of the array to be made October 2004John Edgar51

September 2004John Edgar52

 Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using an array  Implement the heapSort algorithm October 2004John Edgar53

 Java Ch. 12  C++ Ch. 11 October 2004John Edgar54