1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs. 21.1--21.5. When they've got two queues going, there's never any queue!

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

PRIORITY QUEUES AND HEAPS Lecture 19 CS2110 Spring
Priority Queues CSC 172 SPRING 2002 LECTURE 16. Priority Queues Model Set with priorities associate with elements Priorites are comparable by a < operator.
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
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
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.
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. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Spring 2015.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queues Dr. David Matuszek
PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Fall 2014.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Spring 2015.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
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 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.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Heaps & Priority Queues
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
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.
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.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
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.
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
Heaps And Priority Queues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heaps & Priority Queues
Stacks Linked Lists Queues Heaps Hashes
7/23/2009 Many thanks to David Sun for some of the included slides!
CSE 373: Data Structures and Algorithms
Priority Queues.
CE 221 Data Structures and Algorithms
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.
Priority Queues.
Priority Queues.
Heaps By JJ Shepherd.
Tree.
Heaps.
Presentation transcript:

1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs When they've got two queues going, there's never any queue! P.J. Heaps. (The only quote I could find that had both queues and heaps in it.)

2 In various contexts, one needs a list of items, each with a priority. Operations: 1. Add an item (with some priority). 2. Find an item with maximum priority. 3. Remove a maximum-priority item. That is a priority queue. Example: files waiting to be printed, print in order of size (smaller the size, the higher the priority). Example: Job scheduler. Many processes waiting to be executed. Those with higher priority numbers are most important and should be give time before others. Priority queue

3 /** An instance is a priority queue */ public interface PriorityQueue { void insert(Comparable x);/** Insert x into the priority queue */ void makeEmpty();/** Make the queue empty */ boolean isEmpty();/** = “queue is empty” */ int size();/** = the size of the queue */ /** = largest item in this queue --throw exception if queue is empty*/ Comparable findMax(); /** = delete and return largest item --throw exception if queue is empty*/ Comparable removeMax(); } Priority-queue interface x.compareTo(y) is used to see which has higher priority, x or y. Objects x and y could have many fields. Weiss also allows the possibility of changing the priority of an item in the queue. We don’t discuss that feature.

4 Possible implementations. Assume queue has n items. We look at average-case times. O(1) means constant time. 1.Unordered array segment b[0..n-1]. Insert: O(1), findMax: O(n), removeMax: O(n). 2. Ordered array segment b[0..n-1]. Insert: O(n), findMax:O(1), removeMax: O(n) 3. Ordered array segments b[0..n-1], from largest to smallest. Insert: O(n), findMax:O(1), removeMax: O(1) 4.binary search tree (if depth of tree is a minimum). Insert: O(log n), findMax:O(log n), removeMax: O(log n) But how do we keep the tree nicely balanced? Priority-queue implementations

5 Possible implementations. Assume queue has n items. We look at average-case times. O(1) means constant time. 5. Special case. Suppose the possible priorities are 0..n-1. Keep an array priority[0..n-1] in which priority[p] is a linked list of items with priority p. Variable highestp: the highest p for which priority[p] is not empty (-1 if none) Insert, worst case: O(1), findMax:O(1), removeMax: O(n) Priority-queue implementations 0 1 null 2 …

6 Definition of a heap First, number nodes in breadth-first order. A complete binary tree is one in which: if node number n is present, so are nodes 0..n-1. A heap is a complete binary tree in which: the value in any node is at least the values in its children Caution: Weiss numbers nodes 1..n instead of 0..n-1.

7 Because a heap is a complete binary tree, it goes nicely in an array b Place node k in b[k]! The parent of node k is in b[(k-1)/2]. The parent of node 9 is in b[(9-1)/2], which is b[4] The parent of node 8 is in b[(8-1)/2], which is b[3] The children of node k are in b[k*2 + 1] b[k*2 + 2] Children of 3 are in b[7] and b[8]

8 Where is the maximum value of a heap? Max value of a heap is in node 0. Node 0 is in b[0] in our array implementation. Therefore, retrieving the max takes time O(1) (constant time)

9 Removing the max from an n-node tree takes time O(log n) /** Precondition: n > 0. Remove max value from heap. */

10 Removing the max from an n-node tree takes time O(log n) /** Remove max value from heap. Precondition: n > 0. */ n= n – 1; b[0]= b[n]; // Bubble b[0] down to its proper place in b[0..n-1] int k= 0; // inv: the only possible offender of the heap property is b[k] while (k has a child that is larger) { Let h be the larger of k’s childs; Swap b[h] and b[k]; k= h; } Whenever some computation is messy, write a function for it

11 /** k is a node in b[0..n-1]. If k has a larger child, return the larger of its children; otherwise, return k */ public static int f(int[] b, int n, int k) { int h= 2*k+1; // first child; will be the larger child if (h >= n) return k; // k has no children // Set h to index of the larger of k’s childs. if (h+1 0) h= h+1; if (b[k].compareTo(b[h] <= 0) return h; return k; } The children of node k are in b[k*2 + 1], b[k*2 + 2]

12 Removing the max from an n-node tree takes time O(log n) /** Remove max value from heap. Precondition: n > 0. */ n= n – 1; b[0]= b[n]; // Bubble b[0] down to its proper place in b[0..n-1] int k= 0; h= f(k); // inv: the only possible offender of the heap property is b[k] if k offends, h is its larger child; otherwise, h = k while (h != k) { Swap b[h] and b[k]; k= h; h= f(k); }

13 Inserting a value into a heap takes time O(log n) /** Insert ob into heap b[0..n-1]. */ b[n]= ob; n= n+1; Bubble b[n-1] up

14 /** Sort b[0..n-1] */ // Make b[0..n-1] into a heap invariant: b[0..k-1] is a heap (P1 below) for (int k= 0; k != n; k= k+1) { Bubble b[k] up } // Sort the heap b[0..n-1] invariant: Picture P2 below int h= n; while (h != 0) { h= h-1; Swap b[0] and b[h]; Bubble b[0] down in b[0..h-1] } Heap sort this is a heap sorted, >= b[0..h-1] 0 h n Each part time O(n log n) Total time is O(n log n) this is a heap 0 k n P1: b P2: b