Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.

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

Queues Briana B. Morrison Adapted from Alan Eugenio.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
The Priority Queue Abstract Data Type. Heaps. Adaptable Priority Queue. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich,
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
© 2004 Goodrich, Tamassia Priority Queues1. © 2004 Goodrich, Tamassia Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Chapter 8: Priority Queues
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
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.
Chapter 8: Priority Queues and Heaps Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
CSC 213 – Large Scale Programming Lecture 14: Sequence-based Priority Queues.
© 2004 Goodrich, Tamassia Priority Queues1. © 2004 Goodrich, Tamassia Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
Main Index Contents 11 Main Index Contents Week 4 – Stacks.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Containers Overview and Class Vector
Applications of Arrays (Searching and Sorting) and Strings
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
COMP20010: Algorithms and Imperative Programming Lecture 2 Data structures for binary trees Priority queues.
Priority Queues, Trees, and Huffman Encoding CS 244 This presentation requires Audio Enabled Brent M. Dingle, Ph.D. Game Design and Development Program.
Priority Queues and Heaps. Outline and Reading PriorityQueue ADT (§8.1) Total order relation (§8.1.1) Comparator ADT (§8.1.2) Sorting with a Priority.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
CH 8. HEAPS AND PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Lecture 7 Priority Queue Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
12/23/2015 2:18 AMPriority Queues1 Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
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.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Priority Queues Last Update: Oct 23, 2014 EECS2011: Priority Queues1.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 COMP9024: Data Structures and Algorithms Week Seven: Priority Queues Hui Wu Session 1, 2016
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Briana B. Morrison Adapted from Alan Eugenio
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Priority Queues 4/6/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Heaps 11/27/ :05 PM Heaps Heaps.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Ch. 8 Priority Queues And Heaps
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
Heaps and Priority Queues
Copyright © Aiman Hanna All rights reserved
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
© 2013 Goodrich, Tamassia, Goldwasser
Heaps and Priority Queues
Priority Queues © 2010 Goodrich, Tamassia Priority Queues
1 Lecture 10 CS2013.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118

Priority Queues 2 Topics API Application Implementation

Priority Queues 3 Priority Queue A Special form of queue from which items are removed according to their designated priority and not the order in which they entered. Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3.

Priority Queues 4 A PRIORITY QUEUE IS A CONTAINER IN WHICH ACCESS OR DELETION IS OF THE HIGHEST-PRIORITY ITEM, ACCORDING TO SOME WAY OF ASSIGNING PRIORITIES TO ITEMS.

Priority Queues 5

6

7

8

9

10

Priority Queues 11

Priority Queues 12

Priority Queues 13

Priority Queues 14

Priority Queues 15

Priority Queues 16

Priority Queues 17

Priority Queues 18

Priority Queues 19

Priority Queues 20

Priority Queues 21

Priority Queues 22

Priority Queues 23 CLASS priority_queue Constructor priority_queue(); Create an empty priority queue. Type T must implement the operator <. CLASS priority_queue Operations bool empty() const; Check whether the priority queue is empty. Return true if it is empty, and false otherwise. Create void pop(); Remove the item of highest priority from the queue. Precondition:The priority queue is not empty. Postcondition:The priority queue has 1 less element

Priority Queues 24 CLASS priority_queue Operations void push(const T& item); Insert the argument item into the priority queue. Postcondition: The priority queue contains a new element. int size() const; Return the number of items in the priority queue. T& top(); Return a reference to the item having the highest priority. Precondition: The priority queue is not empty. const T& top(); Constant version of top().

Priority Queues 25 Comparator ADT A comparator encapsulates the action of comparing two objects according to a given total order relation A generic priority queue uses a comparator as a template argument, to define the comparison function ( ) The comparator is external to the keys being compared. Thus, the same objects can be sorted in different ways by using different comparators. When the priority queue needs to compare two keys, it uses its comparator

Priority Queues 26 Using Comparators in C++ A comparator class overloads the “()” operator with a comparison function. Example: Compare two points in the plane lexicographically. class LexCompare { public: int operator()(Point a, Point b) { if (a.x b.x) return +1 else if (a.y b.y) return +1 else return 0; } }; To use the comparator, define an object of this type, and invoke it using its “()” operator: Example of usage: Point p(2.3, 4.5); Point q(1.7, 7.3); LexCompare lexCompare; if (lexCompare(p, q) 0) cout << “p greater than q”;

Priority Queues 27 Applications Applications:  Standby flyers  Auctions  Stock market Sorting Huffman Coding

Priority Queues 28 Sorting with a Priority Queue We can use a priority queue to sort a set of comparable elements 1.Insert the elements one by one with a series of push(e) operations 2.Remove the elements in sorted order with a series of pop() operations The running time of this sorting method depends on the priority queue implementation Algorithm PQ-Sort(S, C) Input sequence S, comparator C for the elements of S Output sequence S sorted in increasing order according to C P  priority queue with comparator C while !S.empty () e  S.remove (S. first ()) P.push(e) while !P.empty() e  P.top() P.pop() S.insertLast(e)

Priority Queues 29

Priority Queues 30

Priority Queues 31

Priority Queues 32

Priority Queues 33

Priority Queues 34

Priority Queues 35

Priority Queues 36

Priority Queues 37

Priority Queues 38

Priority Queues 39

Priority Queues 40

Priority Queues 41

Priority Queues 42

Priority Queues 43

Priority Queues 44

Priority Queues 45

Priority Queues 46

Priority Queues 47

Priority Queues 48

Priority Queues 49

Priority Queues 50 Huffman Trees Problem Input: A set of symbols, each with a frequency of occurrence. Desired output: A Huffman tree giving a code that minimizes the bit length of strings consisting of those symbols with that frequency of occurrence. Strategy: Starting with single-symbol trees, repeatedly combine the two lowest-frequency trees, giving one new tree of frequency = sum of the two frequencies. Stop when we have a single tree.

Priority Queues 51 Huffman Trees (2) Implementation approach:  Use a priority queue to find lowest frequency trees  Use binary trees to represent the Huffman (de)coding trees Example: b=13, c=22, d=32 a=64 e=103 Combine b and c: bc=35 Combine d and bc: d(bc)=67 Combine a and d(bc): a(d(bc))=131 Combine e and a(d(bc)): e(a(d(bc)))= done

Priority Queues 52 Huffman Tree Example e=103 a=64 d=32 b=13c=

Priority Queues 53

Priority Queues 54

Priority Queues 55

Priority Queues 56

Priority Queues 57

Priority Queues 58

Priority Queues 59

Priority Queues 60

Priority Queues 61

Priority Queues 62

Priority Queues 63

Priority Queues 64

Priority Queues 65

Priority Queues 66

Priority Queues 67

Priority Queues 68

Priority Queues 69

Priority Queues 70

Priority Queues 71

Priority Queues 72

Priority Queues 73

Priority Queues 74

Priority Queues 75

Priority Queues 76 PQ Implementation How would you implement a priority queue? Several possibilities exist….

Priority Queues 77 Sequence-based Priority Queue Implementation with an unsorted list Performance:  push takes O(1) time since we can insert the item at the beginning or end of the sequence  pop, top take O(n) time since we have to traverse the entire sequence to find the smallest key Implementation with a sorted list Performance:  push takes O(n) time since we have to find the place where to insert the item  pop, top take O(1) time since the smallest key is at the beginning of the sequence Implementation 1Implementation 2

Priority Queues 78 Selection-Sort Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequence Running time of Selection-sort:  Inserting the elements into the priority queue with n push operations takes O(n) time  Removing the elements in sorted order from the priority queue with n pop operations takes time proportional to 1  2  …  n Selection-sort runs in O(n 2 ) time Implementation 1 – like sorting a hand of cards (find smallest, next smallest)

Priority Queues 79 Insertion-Sort Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted sequence Running time of Insertion-sort:  Inserting the elements into the priority queue with n push operations takes time proportional to 1  2  …  n  Removing the elements in sorted order from the priority queue with a series of n pop operations takes O(n) time Insertion-sort runs in O(n 2 ) time Implementation 2 – like sorting a hand of cards (put first in order, 2 nd in order)

Priority Queues 80 In-place Insertion-sort Instead of using an external data structure, we can implement selection-sort and insertion-sort in-place A portion of the input sequence itself serves as the priority queue For in-place insertion-sort  We keep sorted the initial portion of the sequence  We can use swapElements instead of modifying the sequence

Priority Queues 81 Summary Slide §- Priority queue -Pop() returns the highest priority item (largest or smallest). -Normally implemented by a heap, which is discussed later in the class. -The push() and pop() operations have running time O(log 2 n)