0% found this document useful (0 votes)
4 views30 pages

Priority Queues

Priority queues are essential for applications where objects have different priorities, such as medical triaging and air-traffic control. The priority queue ADT allows for efficient insertion and removal of entries based on their keys, with methods like insert, removeMin, and min. Various implementations exist, including unsorted and sorted lists, and priority queues can also be used for sorting algorithms like selection-sort and insertion-sort.

Uploaded by

sarthakvdelhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views30 pages

Priority Queues

Priority queues are essential for applications where objects have different priorities, such as medical triaging and air-traffic control. The priority queue ADT allows for efficient insertion and removal of entries based on their keys, with methods like insert, removeMin, and min. Various implementations exist, including unsorted and sorted lists, and priority queues can also be used for sorting algorithms like selection-sort and insertion-sort.

Uploaded by

sarthakvdelhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Priority Queues

COMP 352: Data Structures and Algorithms

Diego Elias Costa


Chapter 9

2
Priority
Queues

3
Why do we need Priority Queues?
• FIFO is not enough for all queue-like applications
• In many applications, objects are not treated equally.
• ”All animals are equal, but some are more equal than others…”

• Deal with processing objects under different priorities


• Triaging in a medical care center
• Air-traffic control
• Auction house

4
Priority Queue ADT
• A priority queue stores a collection • Additional methods
of entries • min()
returns, but does not remove, an entry
• Each entry is a pair with smallest key, or null if the the
(key, value) priority queue is empty
• Main methods of the Priority Queue • size(), isEmpty()
ADT
• insert(k, v) • Applications:
inserts an entry with key k and value v • Standby flyers
• removeMin() • Auctions
removes and returns the entry with
smallest key, or null if the the priority • Stock market
queue is empty

© 2014 Goodrich, Tamassia, Goldwasser Priority Queues 5


Priority Queue Interface

© 2014 Goodrich, Tamassia, Goldwasser Priority Queues 6


Example Key

• A sequence of priority queue methods: Value

© 2014 Goodrich, Tamassia, Goldwasser 7 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 
• Comparability property: either x  y or y  x
• Antisymmetric property: x  y and y  x  x = y
• Transitive property: x  y and y  z  x  z

© 2014 Goodrich, Tamassia, Goldwasser Priority Queues 8


Entry ADT
• An entry in a priority queue is • As a Java interface:
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

© 2014 Goodrich, Tamassia, Goldwasser Priority Queues 9


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

© 2014 Goodrich, Tamassia, Goldwasser Priority Queues 10


String Length Comparator
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. How to make
it simpler?

11
String Length Comparator
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.

public class StringLengthComparator implements Comparator<String> {


/** Compared two strings according to their length **/
public int compare(String a, String b){
return a.length() – b.length();
}
}

12
String Length Comparator
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.

public class StringLengthComparator implements Comparator<String> {


/** Compared two strings according to their length **/
public int compare(String a, String b){
return a.length() – b.length();
}
}

13
String Length Comparator
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.

public class StringLengthComparator implements Comparator<String> {


/** Compared two strings according to their length */
public int compare(String a, String b){
return a.length() – b.length();
}
}

14
Lexicographic Comparator
/** Comparator for 2D points under the standard /** A point in the plane with integer
lexicographic order. */ coordinates */
public class Lexicographic implements public class Point2D {
Comparator {
protected int xc, yc; // coordinates
int xa, ya, xb, yb;
public Point2D(int x, int y) {
public int compare(Object a, Object b)
throws ClassCastException { xc = x;
xa = ((Point2D) a).getX(); yc = y;
ya = ((Point2D) a).getY(); }

xb = ((Point2D) b).getX();
public int getX() {
return xc;
yb = ((Point2D) b).getY();
}
if (xa != xb)
public int getY() {
return (xb - xa);
else
return yc;
return (yb - ya); }
} }
} © 2014 Goodrich, Tamassia, Goldwasser Priority Queues 15
Quiz: Implementing Comparators?
• How do we compare string alphabetically?

• How to implement a comparator of soccer team rankings in a


championship?

• Could we implement a generic Object comparator?

16
Sequence-based Priority Queue
• Implementation with an unsorted • Implementation with a sorted list
list
4 5 2 3 1 1 2 3 4 5

• Performance: • Performance:
• insert takes O(1) time since we can
• insert takes O(n) time since we have to
insert the item at the beginning or find the place where to insert the item
end of the sequence
• removeMin and min take O(1) time, since
• removeMin and min take O(n) time
the smallest key is at the beginning
since we have to traverse the entire
sequence to find the smallest key

© 2014 Goodrich, Tamassia, Goldwasser Priority Queues 17


Implementing
Priority Queues

18
Abstract Priority Queue class

Our Entry nested object

How to compare elements is defined at


the creation of the priority queue

19
Unsorted List Implementation
Using Linked-based
implementation makes it flexible to
remove elements from the middle

Search for element


with the smallest key

© 2014 Goodrich, Tamassia, Goldwasser 20 Priority Queues


Unsorted List Implementation (part 2)

Adding at an
arbitrary position
(first or last)

© 2014 Goodrich, Tamassia, Goldwasser 21 Priority Queues


Sorted List Implementation

Search for a smaller key to


insert the new element after.

© 2014 Goodrich, Tamassia, Goldwasser 22 Priority Queues


Sorted List Implementation (part 2)

Given the list is sorted, the first element is


guaranteed to have the smallest key.

© 2014 Goodrich, Tamassia, Goldwasser 23 Priority Queues


Using Priority
Queues for Sorting

24
Priority Queue Sorting
• We can use a priority queue to sort a list Algorithm PQ-Sort(S, C)
of comparable elements Input list S, comparator C for the
1. Insert the elements one by one with a elements of S
series of insert operations Output list S sorted in increasing
2. Remove the elements in sorted order order according to C
with a series of removeMin operations
P  priority queue with
comparator C
• The running time of this sorting method while S.isEmpty ()
depends on the priority queue
e  S.remove(S.first ())
implementation
P.insert (e, )
while P.isEmpty()
e  P.removeMin().getKey()
S.addLast(e)

© 2014 Goodrich, Tamassia, Goldwasser


25
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 insert 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(n2) time

© 2014 Goodrich, Tamassia, Goldwasser Priority Queues


26
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) ()

© 2014 Goodrich, Tamassia, Goldwasser 27 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:
1. Inserting the elements into the priority queue with n insert 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(n2) time

© 2014 Goodrich, Tamassia, Goldwasser 28 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) ()
© 2014 Goodrich, Tamassia, Goldwasser Priority Queues
29
In-place Insertion-Sort
• Instead of using an external data structure, 5 4 2 3 1
we can implement selection-sort and
insertion-sort in-place 5 4 2 3 1
• A portion of the input sequence itself serves
as the priority queue 4 5 2 3 1
• For in-place insertion-sort
• We keep sorted the initial portion of the 2 4 5 3 1
sequence
• We can use swaps instead of modifying the 2 3 4 5 1
sequence

1 2 3 4 5

1 2 3 4 5
© 2014 Goodrich, Tamassia, Goldwasser Priority Queues 30

You might also like