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.

Slides:



Advertisements
Similar presentations
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)
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.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
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.
Heapsort. 2 Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort.
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.
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!
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.
Heaps. 2 A complete binary tree Nodes contain Comparable objects Each node contains no smaller (or no larger) than objects in its descendants Maxheap.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
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.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Information and Computer Sciences University of Hawaii, Manoa
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queues Dr. David Matuszek
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
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.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
CS 201 Data Structures and Algorithms
Priority Queues and Heaps
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Source: Muangsin / Weiss
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.
CSE 373: Data Structures and Algorithms
Priority Queues.
Heapsort.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
Priority Queues.
1 Lecture 10 CS2013.
Priority Queues.
Heapsort.
Heaps By JJ Shepherd.
Heapsort.
Chapter 9 The Priority Queue ADT
CO 303 Algorithm Analysis and Design
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

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 is the first one removed (You could also define a largest-first-out priority queue) The definition of “smallest” is up to the programmer (for example, you might define it by implementing Comparator or Comparable ) If there are several “smallest” elements, the implementer must decide which to remove first Remove any “smallest” element (don’t care which) Remove the first one added

3 A priority queue ADT Here is one possible ADT: PriorityQueue() : a constructor void add(Comparable o) : inserts o into the priority queue Comparable remove() : removes and returns the least element Comparable peek() : returns (but does not remove) the least element boolean isEmpty() : returns true iff empty int size() : returns the number of elements void clear() : discards all elements

4 Evaluating implementations When we choose a data structure, it is important to look at usage patterns If we load an array once and do thousands of searches on it, we want to make searching fast—so we would probably sort the array If we load a huge array and expect to do only a few searches, we probably don’t want to spend time sorting the array For almost all uses of a queue (including a priority queue), we eventually remove everything that we add Hence, when we analyze a priority queue, neither “add” nor “remove” is more important—we need to look at the timing for “add + remove”

5 Array implementations A priority queue could be implemented as an unsorted array (with a count of elements) Adding an element would take O(1) time (why?) Removing an element would take O(n) time (why?) Hence, adding and removing an element takes O(n) time This is an inefficient representation A priority queue could be implemented as a sorted array (again, with a count of elements) Adding an element would take O(n) time (why?) Removing an element would take O(1) time (why?) Hence, adding and removing an element takes O(n) time Again, this is inefficient

6 Linked list implementations A priority queue could be implemented as an unsorted linked list Adding an element would take O(1) time (why?) Removing an element would take O(n) time (why?) A priority queue could be implemented as a sorted linked list Adding an element would take O(n) time (why?) Removing an element would take O(1) time (why?) As with array representations, adding and removing an element takes O(n) time Again, these are inefficient implementations

7 Binary tree implementations A priority queue could be represented as a binary search tree Insertion times would range from O(log n) to O(n) (why?) Removal times would range from O(log n) to O(n) (why?) A priority queue could be represented as a balanced binary search tree Insertion and removal could destroy the balance We need an algorithm to rebalance the binary tree Good rebalancing algorithms require only O(log n) time, but are complicated

8 Heap implementation A priority queue can be implemented as a heap In order to do this, we have to define the heap property In Heapsort, a node has the heap property if it is at least as large as its children For a priority queue, we will define a node to have the heap property if it is as least as small as its children (since we are using smaller numbers to represent higher priorities) Heapsort: Blue node has the heap property Priority queue: Blue node has the heap property

9 Array representation of a heap Left child of node i is 2*i + 1, right child is 2*i + 2 Unless the computation yields a value larger than lastIndex, in which case there is no such child Parent of node i is (i – 1)/2 Unless i == lastIndex = 5

10 Using the heap To add an element: Increase lastIndex and put the new value there Reheap the newly added node This is called up-heap bubbling or percolating up Up-heap bubbling requires O(log n) time To remove an element: Remove the element at location 0 Move the element at location lastIndex to location 0, and decrement lastIndex Reheap the new root node (the one now at location 0 ) This is called down-heap bubbling or percolating down Down-heap bubbling requires O(log n) time Thus, it requires O(log n) time to add and remove an element

11 Comments A priority queue is a data structure that is designed to return elements in order of priority Efficiency is usually measured as the sum of the time it takes to add and to remove an element Simple implementations take O(n) time Heap implementations take O(log n) time Balanced binary tree implementations take O(log n) time Binary tree implementations, without regard to balance, can take O(n) (linear) time Thus, for any sort of heavy-duty use, heap or balanced binary tree implementations are better

12 Java 5 java.util.PriorityQueue Java 5 finally has a PriorityQueue class, based on heaps Has redundant methods because it implements two similar interfaces PriorityQueue queue = new PriorityQueue (); Uses the natural ordering of elements (that is, Comparable ) There is another constructor that takes a Comparator argument boolean add(E o) – from the Collection interface boolean offer(E o) – from the Queue interface E peek() – from the Queue interface boolean remove(Object o) – from the Collection interface E poll() – from the Queue interface (returns null if queue is empty) void clear() int size()

13 The End