Priority Queues
Priority Queues
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…”
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
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.
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.
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.
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?
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
18
Abstract Priority Queue class
19
Unsorted List Implementation
Using Linked-based
implementation makes it flexible to
remove elements from the middle
Adding at an
arbitrary position
(first or last)
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)
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) ()
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