Copyright © Aiman Hanna All rights reserved

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 -
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
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.
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.
COMP20010: Algorithms and Imperative Programming Lecture 2 Data structures for binary trees Priority queues.
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)
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.
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.
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
Bucket-Sort and Radix-Sort
Array Lists, Node Lists & Sequences
Algorithms Chapter 3 With Question/Answer Animations
Priority Queues and Heaps
Copyright © Aiman Hanna All rights reserved
Part-D1 Priority Queues
Breadth-First Search (BFS)
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
Ordered Maps & Dictionaries
Heaps and Priority Queues
Copyright © Aiman Hanna All rights reserved
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.
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Linked Lists & Iterators
Copyright © Aiman Hanna All rights reserved
Heaps and Priority Queues
Copyright © Aiman Hanna All rights reserved
Priority Queues © 2010 Goodrich, Tamassia Priority Queues
1 Lecture 10 CS2013.
Copyright © Aiman Hanna All rights reserved
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

Copyright © 2011-2016 Aiman Hanna All rights reserved Priority Queues Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of : Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, 2010. ISBN 978-0-470-38326-1. Data Structures and the Java Collections Framework by William J. Collins, 3rdedition, ISBN 978-0-470-48267-4. Both books are published by Wiley. Copyright © 2010-2011 Wiley Copyright © 2010 Michael T. Goodrich, Roberto Tamassia Copyright © 2011 William J. Collins Copyright © 2011-2016 Aiman Hanna All rights reserved

Coverage Section 8.1: The Priority Queue ADT Priority Queues 12/30/2018 8:10 PM Coverage Section 8.1: The Priority Queue ADT Section 8.2: Priority Queue List Implementation Sorting with Priority Queue Selection Sort Insertion Sort Section 3.1.2: Selection Sort Priority Queues

Priority Queue ADT A priority queue (P.Q.) is an ADT for storing a collection of prioritized elements; the elements are referred to as values. P.Q. supports arbitrary insertion of elements, however the removal of the elements is made in order of priorities. Consequently, a P.Q. is fundamentally different from other position-based ADTs (such as stacks, queues, D.Qs, etc.), where operations are conducted on specific positions. P.Q. ADT stores elements according to their priorities and exposes no notion of positions to the user. Priority Queues

Priority Queue ADT A key can be used to indicate the priority of a value (p.s. value means an element here). Each entry in the P.Q. is hence a pair of (key, value) Main methods of the Priority Queue ADT insert(k, x): insert an entry with key k and value x into PQ, and return the entry storing them removeMin(): remove and returns the entry with smallest key (smallest key indicates first priority). Priority Queues

Priority Queue ADT Additional methods Applications: min(): return the entry with smallest key, but do not remove it size(), isEmpty() Applications: Auctions Stock market … Priority Queues

Priority Queue ADT Example of a P.Q. Operations Output Priority Queue Notice that the “Priority Queue” column is somewhat deceiving since it shows that the entries are sorted by keys, which is more than required of a P.Q. Operations Output Priority Queue insert(5, A) e1 [= (5, A)] {(5, A)} insert(9, C) e2 [= (9, C)] {(5, A), (9, C)} insert(3, B) e3 [= (3, B)] {(3, B), (5, A), (9, C)} insert(7, D) e4 [= (7, D)] {(3, B), (5, A), (7, D), (9, C)} min() e3 removeMin() {(5, A), (7, D), (9, C)} size() 3 e1 {(7, D), (9, C)} e4 {(9, C)} 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. P.Q. needs a comparison rule that will never contradict itself. In order for a comparison rule, which we denote by ≤, to be robust, it must define a total order relation. Priority Queues

Total Order Relations The comparison rule must be defined for each pair of keys and must satisfy the following properties: Reflexive property: k  k Antisymmetric property: k1  k2  k2  k1  k1 = k2 Transitive property: k1  k2  k2  k3  k1  k3 A comparison rule that satisfies these three properties will never lead to a comparison contradiction. Priority Queues

Entries & Comparators Two important questions must be asked: How do we keep track of the associations between keys and values? How do we compare keys so as to determine the smallest key? The definition of a P.Q. implicitly makes use of two special kinds of objects, which answer the above questions: The entry object The comparator object Priority Queues

Entry ADT An entry in a priority queue is simply a key-value pair That is, an entry object is actually composed of a key and a value objects Priority queues store entries to allow for efficient insertion and removal based on keys Methods of Entry ADT: getKey: returns the key for this entry getValue: returns the value associated with this entry Priority Queues

Entry ADT As a Java interface: /** * Interface for a key-value * pair entry **/ public interface Entry<K,V> { public K getKey(); public V getValue(); } Priority Queues

Comparator ADT It is important to define a way for specifying the total order relation for comparing keys. One possibility is to use a particular key type that the P.Q. can compare. The problem with such approach is that the utilization of different keys would require the creation of different/multiple P.Qs. An alternative strategy is to require the keys to be able to compare themselves to one another. This solution allows us to write a general P.Q. that can store instances of a key class that has a well-established natural ordering. Priority Queues

Comparator ADT It is possible to have comparable objects by implementing the java.lang.Comparable interface. The problem with such approach however is that there are cases where the keys will be required to provide more information than they should/expected to, such as their comparison rules. For instance, there are two natural ways to compare “7” and “21”. “7” is < “21” if the rule is integer comparison, where “21” is < “7” if the rule is lexicographic ordering. Such cases would require the keys themselves to provide their comparison rules. Priority Queues

Comparator ADT Instead, we can use special comparator objects that are external to the keys to supply the comparison rules. A comparator encapsulates the action of comparing two objects according to a given total order relation. A generic priority queue uses an auxiliary comparator. We assume that a priority queue is given a comparator object when it is constructed. The P.Q. uses its comparator for keys comparisons. Priority Queues

Comparator ADT Primary method of the Comparator ADT: compare(a, b): 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. The java.util.Comparator interface correspond to the above comparator ADT. 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; 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 Priority Queues

Priority Queue Sorting 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) Notice that in the above code, the elements of the input sequence S serve as keys of the priority queue P. 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 Priority Queues

Selection-Sort Selection Sort algorithm works as follows: Find the minimum value in the collection (list/sequence, P.Q., etc.) Swap it with the value in the first position Repeat the steps above for the remainder of the list (starting at the second position and advancing each time) Click here to view some illustrative animations What is the running time? Priority Queues

Selection-Sort Running time of the PQ-Sort(S, C) Selection-sort; that is when the P.Q. is implemented with unsorted sequence: (First loop): Inserting the elements into the priority queue with n insert operations takes O(n) time (Second loop) Removing the elements in sorted order (repeated seclection) from the priority queue with n removeMin operations takes time proportional to n + n -1 + n -2 + …+ 3 + 2 + 1 Resulting in a total of O(n + n2)  O(n2)  Selection-sort runs in O(n2) time Priority Queues

Selection-Sort Example Sequence S Priority Queue P Input: (7,4,8,2,5,3,9) () Phase 1 (first loop) (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 (second loop) (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) () Priority Queues

Insertion-Sort Insertion Sort algorithm is as follows: Removes an element (possibly arbitrary) from the input data Insert the element into the correct position in the already-sorted list Repeat until no input elements remain. Click here to view some illustrative animations What is the running time? What is best case? Priority Queues

Insertion-Sort Running time of the PQ-Sort(S, C) Insertion-sort; that is when the P.Q. is implemented with sorted sequence (obtained after phase 1 (first loop) is finished): (First loop): Inserting the elements into the priority queue with n insert operations takes time proportional to 1 + 2 + …+ n (Second loop): Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time Resulting in a total of O(n2+ n)  O(n2)  Insertion-sort runs in O(n2) time Priority Queues

Insertion-Sort Special case: Resulting in a total of O(n+ n)  O(n) If the sequence is already (by luck) sorted, then (First loop): Sorting the list will take O(n) time (Second loop): Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time Resulting in a total of O(n+ n)  O(n) Priority Queues

Insertion-Sort Example Sequence S Priority queue P Input: (7,4,8,2,5,3,9) () Phase 1 (first loop) (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 (secondl oop) (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) .. .. .. (g) (2,3,4,5,7,8,9) () 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 Priority Queues