12/23/2015 2:18 AMPriority Queues1 Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
Advertisements

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)
Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -
Priority Queue Erick, Eka, Reddy © Sekolah Tinggi Teknik Surabaya 1.
Priority Queue and Heap 1. 2 Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the.
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,
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.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
© 2004 Goodrich, Tamassia Priority Queues1. © 2004 Goodrich, Tamassia Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection.
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.
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.
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.
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
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.
CSC401 – Analysis of Algorithms Lecture Notes 4 Trees and Priority Queues Objectives: General Trees and ADT Properties of Trees Tree Traversals Binary.
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.
1 Priority Queues Stock trading (motivation) The priority queue ADT Implementing a priority queue with a sequence Elementary sorting Issues in sorting.
COMP20010: Algorithms and Imperative Programming Lecture 2 Data structures for binary trees Priority queues.
Binary Trees Priority Queues, and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer.
Priority Queues & Heaps Chapter 9. Iterable Collection Abstract Collection Queue List Abstract Queue Priority Queue Array List Abstract List Vector Stack.
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)
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
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.
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.
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
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.
Priority Queues Last Update: Oct 23, 2014 EECS2011: Priority Queues1.
Priority Queues CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CMPSCI Introduction to Introduction to Programming with Data Structures Lecture 18 Binary Search Trees and Priority Queues Lecture 18 Binary Search.
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.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
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,
Priority Queues Chuan-Ming Liu
COMP9024: Data Structures and Algorithms
Part-D1 Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Priority Queues © 2014 Goodrich, Tamassia, Goldwasser Priority Queues
Heaps and Priority Queues
Chapter 2, Sections 4 and 5 Priority Queues Heaps Dictionaries
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
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
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 © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Lecture 9 CS2013.
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:

12/23/2015 2:18 AMPriority Queues1 Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118

12/23/2015 2:18 AMPriority Queues2 Outline and Reading PriorityQueue ADT (§7.1) Total order relation (§7.1.1) Comparator ADT (§7.1.4) Sorting with a priority queue (§7.1.2) Selection-sort (§7.2.3) Insertion-sort (§7.2.3)

12/23/2015 2:18 AMPriority Queues3 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority Queue ADT insertItem(k, o) inserts an item with key k and element o removeMin() removes the item with the smallest key Additional methods minKey(k, o) returns, but does not remove, the smallest key of an item minElement() returns, but does not remove, the element of an item with smallest key size(), isEmpty() Applications: Standby flyers Auctions Stock market

12/23/2015 2:18 AMPriority Queues4 Total Order Relation Keys in a priority queue can be arbitrary objects on which an order is defined Two distinct items in a priority queue can have the same key Mathematical concept of total order relation  Reflexive property: x  x Antisymmetric property: x  y  y  x  x = y Transitive property: x  y  y  z  x  z

12/23/2015 2:18 AMPriority Queues5 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

12/23/2015 2:18 AMPriority Queues6 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”;

12/23/2015 2:18 AMPriority Queues7 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 insertItem(e, e) operations 2.Remove the elements in sorted order with a series of removeMin() 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.isEmpty () e  S.remove (S. first ()) P.insertItem(e, e) while !P.isEmpty() e  P.minElement() P.removeMin() S.insertLast(e)

Example page 317 (7.2) 12/23/2015 2:18 AMPriority Queues8 OperationoutputPriority Queue insertItem(5,A)-{(5,A)} insertItem(9,C)-{(5,A), (9,C)} insertItem(3,B)-{(3,B),(5,A), (9,C)} insertItem(7,D)-{(3,B),(5,A),(7,D), (9,C)} minElement()B{(3,B),(5,A),(7,D), (9,C)} minKey()3{(3,B),(5,A),(7,D), (9,C)} removeMin-{(5,A),(7,D), (9,C)} Size()3{(5,A),(7,D), (9,C)} minElement()A{(5,A),(7,D), (9,C)} removeMin()-{(7,D), (9,C)} removeMin()-{ (9,C)} removeMin()-{} removeMin()“error”{} isEmpty()true{}

12/23/2015 2:18 AMPriority Queues9 Sequence-based Priority Queue Implementation with an unsorted sequence Store the items of the priority queue in a list-based sequence, in arbitrary order Performance: insertItem takes O(1) time since we can insert the item at the beginning or end of the sequence removeMin, minKey and minElement take O(n) time since we have to traverse the entire sequence to find the smallest key Implementation with a sorted sequence Store the items of the priority queue in a sequence, sorted by key Performance: insertItem takes O(n) time since we have to find the place where to insert the item removeMin, minKey and minElement take O(1) time since the smallest key is at the beginning of the sequence

12/23/2015 2:18 AMPriority Queues10 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: 1.Inserting the elements into the priority queue with n insertItem operations takes O(n) time 2.Removing the elements in sorted order from the priority queue with n removeMin operations takes time proportional to 1  2  …  n Selection-sort runs in O(n 2 ) time

12/23/2015 2:18 AMPriority Queues11 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: 1.Inserting the elements into the priority queue with n insertItem operations takes time proportional to 1  2  …  n 2.Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time Insertion-sort runs in O(n 2 ) time

12/23/2015 2:18 AMPriority Queues12 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