Priority Queues © 2010 Goodrich, Tamassia Priority Queues

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

Priority Queues1 Priority Queues (Antrian Berprioritas)
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.
Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
© 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 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.
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.
Adaptable Priority Queues1 3 a 5 g 4 e © 2010 Goodrich, Tamassia.
1 Priority Queues Stock trading (motivation) The priority queue ADT Implementing a priority queue with a sequence Elementary sorting Issues in sorting.
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)
CH 8. HEAPS AND PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
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.
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.
Quotes “From each according to his ability, to each according to his needs” -- Karl Marx/Queue ADT “In America, first you get the sugar, then you get the.
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.
1 COMP9024: Data Structures and Algorithms Week Seven: Priority Queues Hui Wu Session 1, 2016
Adaptable Priority Queues1 3 a 5 g 4 e © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java,
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.
Queues Chapter 4.
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
Copyright © Aiman Hanna All rights reserved
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
Adaptable Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Lecture 9 CS2013.
Heaps and Priority Queues
1 Lecture 10 CS2013.
CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

Priority Queues © 2010 Goodrich, Tamassia Priority Queues 4/23/2019 10:30 PM Priority Queues © 2010 Goodrich, Tamassia 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 ADT insert(k, x) inserts an entry with key k and value x removeMin() removes and returns the entry with smallest key Additional methods min() returns, but does not remove, an entry with smallest key size(), isEmpty() Applications: Standby flyers Auctions Stock market © 2010 Goodrich, Tamassia Priority Queues

Total Order Relations Keys in a priority queue can be arbitrary objects on which an order is defined Two distinct entries 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 © 2010 Goodrich, Tamassia Priority Queues

Entry ADT As a Java interface: An entry in a priority queue is simply a key-value pair Priority queues store entries to allow for efficient insertion and removal based on keys Methods: getKey: returns the key for this entry getValue: returns the value associated with this entry As a Java interface: /** * Interface for a key-value * pair entry **/ public interface Entry<K,V> { public K getKey(); public V getValue(); } © 2010 Goodrich, Tamassia Priority Queues

Comparator ADT Primary method of the Comparator ADT A comparator encapsulates the action of comparing two objects according to a given total order relation A generic priority queue uses an auxiliary comparator The comparator is external to the keys being compared When the priority queue needs to compare two keys, it uses its comparator Primary method of the Comparator ADT compare(x, y): returns an integer i such that i < 0 if a < b, i = 0 if a = b i > 0 if a > b An error occurs if a and b cannot be compared. © 2010 Goodrich, Tamassia Priority Queues

Example Comparator Lexicographic comparison of 2-D points: /** Comparator for 2D points under the standard lexicographic order. */ public class Lexicographic implements Comparator { int xa, ya, xb, yb; public int compare(Object a, Object b) throws ClassCastException { xa = ((Point2D) a).getX(); ya = ((Point2D) a).getY(); xb = ((Point2D) b).getX(); yb = ((Point2D) b).getY(); if (xa != xb) return (xb - xa); else return (yb - ya); } Point objects: /** Class representing a point in the plane with integer coordinates */ public class Point2D { protected int xc, yc; // coordinates public Point2D(int x, int y) { xc = x; yc = y; } public int getX() { return xc; public int getY() { return yc; © 2010 Goodrich, Tamassia Priority Queues

Priority Queue Sorting We can use a priority queue to sort a set of comparable elements Insert the elements one by one with a series of insert operations 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.removeFirst () P.insert (e, ) while P.isEmpty() e  P.removeMin().getKey() S.addLast(e) © 2010 Goodrich, Tamassia Priority Queues

Sequence-based Priority Queue Implementation with an unsorted list Performance: insert takes O(1) time since we can insert the item at the beginning or end of the sequence removeMin and min take O(n) time since we have to traverse the entire sequence to find the smallest key Implementation with a sorted list Performance: insert takes O(n) time since we have to find the place where to insert the item removeMin and min take O(1) time, since the smallest key is at the beginning 4 5 2 3 1 1 2 3 4 5 © 2010 Goodrich, Tamassia Priority Queues

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 insert operations takes O(n) time 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(n2) time © 2010 Goodrich, Tamassia Priority Queues

Selection-Sort Example Sequence S Priority Queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (7,4) .. .. .. (g) () (7,4,8,2,5,3,9) Phase 2 (a) (2) (7,4,8,5,3,9) (b) (2,3) (7,4,8,5,9) (c) (2,3,4) (7,8,5,9) (d) (2,3,4,5) (7,8,9) (e) (2,3,4,5,7) (8,9) (f) (2,3,4,5,7,8) (9) (g) (2,3,4,5,7,8,9) () © 2010 Goodrich, Tamassia Priority Queues

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 insert operations takes time proportional to 1 + 2 + …+ n 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(n2) time © 2010 Goodrich, Tamassia Priority Queues

Insertion-Sort Example Sequence S Priority queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (4,7) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9) Phase 2 (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) .. .. .. (g) (2,3,4,5,7,8,9) () © 2010 Goodrich, Tamassia Priority Queues

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 swaps instead of modifying the sequence 5 4 2 3 1 5 4 2 3 1 4 5 2 3 1 2 4 5 3 1 2 3 4 5 1 1 2 3 4 5 1 2 3 4 5 © 2010 Goodrich, Tamassia Priority Queues