Transcript Lecture 10
Priority Queues
CS 110: Data Structures and Algorithms
First Semester, 2010-2011
Definition
► The
Priority Queue Data Structure
stores elements with assigned priorities
► insertion of an object must include a priority level
(key)
► removal of an object is based on the priority levels
of the elements in the queue; the object whose
level is smallest gets removed first
►
► Need
an Entry interface/class
Entry encapsulates a key and the object/record
stored
► Could be an interface so the actual class containing
the key and object implements this interface
►
PriorityQueue Interface
public interface PriorityQueue
Could be an Object instead of
{
int to allow for other key types;
e.g. String
public int size();
public boolean isEmpty();
public Entry insert( int key, Object value)
public Entry min()
throws EmptyPriorityQueueException;
public Entry removeMin()
throws EmptyPriorityQueueException;
}
List-based Implementations
► Using
either an array or a linked list, the
elements can be stored as a sequence of
entries
► The sequence of entries can be stored in
the order the elements arrive
(unsorted list implementation )
► Or, sorted by key
(sorted list implementation)
Unsorted List Implementation
► Insertion
is done such that the incoming
element is appended to the list
►
An O( 1 ) operation
► Removal
involves scanning the array or the
linked list and determining the element with
the minimum-valued key
If using an array, elements need to be adjusted
upon removal
► An O( n ) operation regardless because of the
scan
►
Sorted List Implementation
► Insertion
is done such that the incoming element
is stored or inserted in its proper place (elements
are sorted by key)
►
An O( n ) operation
► Removal
►
►
►
operation:
If using an array, the sequence is stored in decreasing
order, and removal involves returning the last element
If using a linked list, the sequence is stored in
increasing order, and removal involves returning the
first element (head of the list)
An O( 1 ) operation regardless
Using a Heap
►A
heap is a (complete) binary tree of entries
such that for every node except for the root,
the node’s key is greater than or equal to its
parent’s
► The root of a heap contains the element with
the minimum-valued key (highest priority)
► Insertion and removal operations for a heap
both run in O( log n ) time
Heap Data Structure
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(20,B)
Heaps and BTs using Arrays
► Heaps
are complete (all levels filled up
except perhaps for last level) which
makes an array implementation of a
binary tree most appropriate
► But… we need to ensure that the
completeness of binary tree stays even
after insertion or removal of elements
Insertion into a Heap
Add new element at the end of the array
► Compare the key of the newly added element with it’s
parent’s key to check if heap property is observed
► If heap property is violated, swap element at last
position with element at its parent
►
►
►
►
repeat this process for the parent
stop once heap property is satisfied
In effect, the new element is “promoted” to its
appropriate level
Inserting into a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(20,B)
Insertion into a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(20,B)
(2,T)
Insertion into a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(20,B)
(2,T)
Insertion into a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(20,B)
(2,T)
Insertion into a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(2,T)
(20,B)
Insertion into a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(2,T)
(20,B)
Insertion into a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(2,T)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(6,Z)
(20,B)
Insertion into a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(2,T)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(6,Z)
(20,B)
Insertion into a Heap
(2,T)
(5,A)
(15,K)
(16,X)
(25,J)
(4,C)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(6,Z)
(20,B)
Removal from a Heap
►
Vacate root position of the tree
►
Element in the root to be returned by the method
Get last element in the array, place it in the root
position
► Compare this element’s key with the root’s children’s
keys, and check if the heap property is observed
► If heap property is violated, swap root element with
the child with minimum key value
►
►
►
repeat process for the child position
stop when heap property is satisfied
Removal from a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(20,B)
Removal from a Heap
(4,C)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(20,B)
Removal from a Heap
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(13,W)
(20,B)
Removal from a Heap
(13,W)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(20,B)
Removal from a Heap
(13,W)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(20,B)
Removal from a Heap
(13,W)
(5,A)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(20,B)
Removal from a Heap
(5,A)
(13,W)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(20,B)
Removal from a Heap
(5,A)
(13,W)
(15,K)
(16,X)
(25,J)
(6,Z)
(9,F)
(14,E)
(12,H)
(7,Q)
(11,S)
(20,B)
Removal from a Heap
(5,A)
(9,F)
(15,K)
(16,X)
(25,J)
(6,Z)
(13,W)
(14,E)
(12,H)
(7,Q)
(11,S)
(20,B)
Removal from a Heap
(5,A)
(9,F)
(15,K)
(16,X)
(25,J)
(6,Z)
(13,W)
(14,E)
(12,H)
(7,Q)
(11,S)
(20,B)
Removal from a Heap
(5,A)
(9,F)
(15,K)
(16,X)
(25,J)
(6,Z)
(12,H)
(14,E)
(13,W)
(7,Q)
(11,S)
(20,B)
Why O( log n )?
Worst-case number of swaps is proportional to the
height of the tree
► What is the height h of a binary tree with n nodes?
► If binary tree is complete:
►
►
►
►
►
►
2h <= n < 2h+1
log 2h <= log n < log 2h+1
h <= log n < h+1
h is O( log n )
Insertion and removal for a heap is O( log n )
Time Complexity Summary
Operation Insertion
Unsorted List O( 1 )
Removal
O( n )
Sorted List
O( n )
O( 1 )
Heap
O( log n )
O( log n )
About Priority Queues
► Choose
an implementation that fits the
application’s requirements
►
Note trade-off between insertion and removal
time complexity
► Keys
need not be integers
Need the concept of a comparator (e.g., can’t
use ≤ operator for String values )
► Value of the key parameter in the insert method
needs to be validated if the key is of type Object
►