1 Priority Queues Priority Queue ADT Applications of Priority Queues

Priority Queues
! 
! 
! 
Class #25:
Priority Queues & Binary Heaps
! 
Software Design III (CS 340): M. Allen, 30 March 2015
Many linear data-structures
are defined by the relative
ordering of entrance/exit
In stacks, objects enter and
leave in LIFO order
In queues, they do so in
FIFO order
In a priority queue, the order in which objects leave is
essentially independent of when they enter
! 
! 
Instead, objects are assigned an “importance value”
Whenever we retrieve an object, we get the one that has the
highest priority, however this is defined for the given data-type
Monday, 30 Mar. 2015"
Priority Queue ADT
! 
! 
Allows the ordering we need for priority
! 
3. 
4. 
! 
! 
findMin(): find the object with highest priority
! 
Assumes that priority is organized smallest-to-largest by
comparison (could be implemented in reverse order, too)
! 
deleteMin(): find object with highest priority, and
remove it from the queue at the same time
Usual other methods to construct, get size, etc.
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
These sorts of ordered structures are used in many
different situations where order is important:
! 
We then will have basic operations to:
1.  insert(): place a new object in the queue
! 
2. 
2"
Applications of Priority Queues
In general, we want our objects to be Comparable!
! 
Software Design III (CS 340)"
3"
! 
Data compression (Huffman codes)
Graph Search (Dijkstra’s shortest-path algorithm)
Artificial Intelligence (Heuristic game-tree search)
Operating Systems (load balancing, interrupts)
Simulation (particle systems, traffic simulation)
One example: Find the largest M items in a list of N things
! 
How do we manage this, if we don’t have enough memory to
store all N of the objects in working memory at once?
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
4"
1
Simple Ordered and Unordered
Implementations
! 
Implementing with an Unsorted Array
Two basic possibilities:
1. 
2. 
Keep an unordered list, and return the next by priority (searching to
find it whenever needed)
Keep the list ordered at all times, and simply return its first element
Operation
Return Size
insert(3)
insert(6)
insert(1)
deleteMin()
insert(4)
insert(5)
insert(2)
deleteMin()
Ordered
public UnsortedPriorityQueue( int capacity ) {
values = (T[]) new Comparable[capacity]; !
}!
Unordered!
1
2
3
3
3 6
1 3 6
3 !
3 6!
3 6 1!
1
2
3 6
3 6!
2
3
4
5
4
3
3
2
3
4
4
3
4
public class UnsortedPriorityQueue<T extends Comparable<? super T>> {!
private T[] values;!
private int size;!
6
5 6
4 5 6
5 6
3
3
3
3
6
6
6
6
!
Simple O(1) insert
public void insert( T t ) {!
values[size] = t;!
size++;!
}!
public T deleteMin() {!
O(n) to
int minIndex = 0;!
for( int i = 1; i < size; i++ )!
if( values[i].compareTo( values[minIndex] ) <= 0 ) !
minIndex = i;!
4!
4 5!
4 5 2!
4 5!
T result = values[minIndex];!
values[minIndex] = values[size – 1];!
size--;
!
return result;!
find least value
Swapping operation to
keep array contiguous
(since order doesn’t
matter, O(1) for this)
}!
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
5"
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
Implementing with a Sorted Array
Efficiency and Priority
public class UnsortedPriorityQueue<T extends Comparable<? super T>> {!
private T[] values;!
private int size;!
! 
public UnsortedPriorityQueue( int capacity ) {
values = (T[]) new Comparable[capacity]; !
}!
!
public void insert( T element ) {!
for ( int i = size – 1; i >= 0; i-- ) {!
if ( values[i].compareTo( element ) >= 0 ) {!
values[i + 1] = element;!
i = -1;!
}!
else!
values[i + 1] = values[i];!
size++;!
}!
public T deleteMin() {!
size--;
!
return values[size];!
}!
Monday, 30 Mar. 2015"
What we want is that all important operations are as
efficient as we can make them
! 
O(n) work to insert
new value at the
proper sorted location
Simple O(1) to find
smallest value
Array kept in reverse order (with
smallest at end) to reduce work,
since no swapping is needed
Software Design III (CS 340)"
7"
! 
6"
Small (and smart) compromise: to achieve overall better
performance, we give up the constant-time speed for some
operations in order to speed up the other ones, without ever
needing linear time for anything
Implementation!
insert()!
find/deleteMin()!
Unordered array!
O(1)!
O(n)!
Ordered Array!
O(n)!
O(1)!
GOAL!
log n!
log n!
To achieve this, we will use a tree-based structure
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
8"
2
Binary Heaps
! 
A tree-based implementation of a priority queue
! 
! 
! 
Binary Heaps
A complete binary tree
Has the additional heap property: Each node N has a priority
value greater than or equal to that of any of its children
We will keep nodes ordered by their priority values
! 
! 
Software Design III (CS 340)"
28"
9"
Heap Insertion
4"
18"
Unlike basic BST: these values can be duplicated
(procedure to break ties is up to the implementation)
Like basic BST: These values may or may not be distinct
from the actual data stored in the nodes
Monday, 30 Mar. 2015"
! 
4"
9"
19"
11"
4"
Heap Property
satisfied
4"
4"
Heap Property
satisfied
Monday, 30 Mar. 2015"
Heap Property not
satisfied (WHY?)
10"
Software Design III (CS 340)"
7"
12"
11"
16"
20"
13"
6"
15"
2"
! 
However, we cannot always insert there: the question is how
to preserve the heap-ordering property when we do so
Monday, 30 Mar. 2015"
11" 19"
4"
6"
14"
28"
5"
9"
25"
4"
9"
Heap Insertion (Bubbling Up)
5"
16"
18"
Not a complete
binary tree
4"
! 
4"
Since the heap is a complete tree, the first available spot we
could possibly insert is always on the bottom row of the tree,
in the first empty spot (reading left to right)
15"
4"
Software Design III (CS 340)"
11"
9"
25"
14"
7"
12"
11"
2"
13"
20"
An easy solution:
1. 
2. 
Start with the first open spot
If added item has higher priority than parent, swap them
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
12"
3
Heap Insertion (Bubbling Up)
Heap Insertion (Bubbling Up)
4"
2"
5"
15"
16"
! 
9"
25"
5"
2"
14"
7"
12"
11"
6"
13"
15"
20"
16"
Each time we swap, the newly placed parent node has
higher priority than the original
! 
! 
Thus, since we started with a heap, all other children have
lower priority than new parent too, preserving heap property
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
13"
Heap Removal
! 
12"
11"
13"
9"
14"
20"
Since we started from the correct place to keep the tree
complete, and since we preserved the ordering property, we
have a heap again once we are done
! 
Worst case: O(log n) operations (height of complete tree)
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
! 
14"
In order to keep the tree correctly complete, we remove
the item from last position and place it in the root
20"
4"
4"
15"
7"
12"
11"
15"
6"
13"
16"
20"
We must now turn the structure back into a heap again
Monday, 30 Mar. 2015"
14"
6"
3"
3"
! 
25"
7"
2"
?"
25"
9"
Heap Removal
Highest priority item is always at the root, and we can
return that object in constant time, O(1)
16"
4"
Software Design III (CS 340)"
15"
! 
9"
25"
14"
7"
12"
11"
6"
13"
20"
Now we must restore the heap ordering property
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
16"
4
Heap Removal (Bubbling Down)
! 
Heap Removal (Bubbling Down)
To restore the ordering, we simply swap downwards: we
swap the parent with highest-priority child, if it has higher
priority than the parent
! 
Using the highest priority (minimum) child value ensures that
highest priority item is correctly placed on top
3"
3"
9"
20"
4"
4"
15"
15"
9"
7"
16"
16"
25"
14"
12"
11"
Software Design III (CS 340)"
17"
Heap Removal (Bubbling Down)
! 
Worst case is again O(log n) operations
9"
4"
Monday, 30 Mar. 2015"
14"
7"
20"
11"
11"
13"
Software Design III (CS 340)"
Reading: Priority Queues & Heaps (Chapter 6)
! 
Meet: as usual.
! 
18"
6"
Monday/Wednesday: regular classroom
Friday: CS Lab (16 Wing)
Office Hours: Wing 210
! 
! 
25"
12"
! 
! 
16"
14"
The fact that we are using the highest priority child as the new
parent guarantees it will have higher priority than its children
! 
12"
25"
Monday, 30 Mar. 2015"
3"
15"
6"
This Week
When we are done, we have a balanced heap once again
! 
7"
13"
! 
Monday, 30 Mar. 2015"
20"
6"
Monday & Tuesday: 5:00–7:00
Thursday: 11:00–1:00
13"
Software Design III (CS 340)"
19"
Monday, 30 Mar. 2015"
Software Design III (CS 340)"
20"
5