View
217
Download
0
Category
Preview:
DESCRIPTION
PriorityQueue Interface public interface PriorityQueue { public int size(); public boolean isEmpty(); public Entry insert( int key, Object value) public Entry min() throws EmptyPriorityQueueException; public Entry removeMin() throws EmptyPriorityQueueException; } Could be an Object instead of int to allow for other key types; e.g. String
Citation preview
Priority QueuesCS 110: Data Structures and
AlgorithmsFirst 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 Interfacepublic interface PriorityQueue{ public int size(); public boolean isEmpty(); public Entry insert( int key, Object value) public Entry min()
throws EmptyPriorityQueueException; public Entry removeMin()
throws EmptyPriorityQueueException;}
Could be an Object instead of int to allow for other key types; e.g. String
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
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(4,C)
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
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(4,C)
Insertion into a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(4,C)
(2,T)
Insertion into a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(4,C)
(2,T)
Insertion into a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(4,C)
(2,T)
Insertion into a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (2,T)
(5,A) (6,Z)
(4,C)
(20,B)
Insertion into a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (2,T)
(5,A) (6,Z)
(4,C)
(20,B)
Insertion into a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (6,Z)
(5,A) (2,T)
(4,C)
(20,B)
Insertion into a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (6,Z)
(5,A) (2,T)
(4,C)
(20,B)
Insertion into a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (6,Z)
(5,A) (4,C)
(2,T)
(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
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(4,C)
Removal from a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(4,C)
Removal from a Heap
(16,X) (25,J) (14,E) (12,H) (11,S) (13,W)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
Removal from a Heap
(16,X) (25,J) (14,E) (12,H) (11,S)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(13,W)
Removal from a Heap
(16,X) (25,J) (14,E) (12,H) (11,S)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(13,W)
Removal from a Heap
(16,X) (25,J) (14,E) (12,H) (11,S)
(15,K) (9,F) (7,Q) (20,B)
(5,A) (6,Z)
(13,W)
Removal from a Heap
(16,X) (25,J) (14,E) (12,H) (11,S)
(15,K) (9,F) (7,Q) (20,B)
(13,W) (6,Z)
(5,A)
Removal from a Heap
(16,X) (25,J) (14,E) (12,H) (11,S)
(15,K) (9,F) (7,Q) (20,B)
(13,W) (6,Z)
(5,A)
Removal from a Heap
(16,X) (25,J) (14,E) (12,H) (11,S)
(15,K) (13,W) (7,Q) (20,B)
(9,F) (6,Z)
(5,A)
Removal from a Heap
(16,X) (25,J) (14,E) (12,H) (11,S)
(15,K) (13,W) (7,Q) (20,B)
(9,F) (6,Z)
(5,A)
Removal from a Heap
(16,X) (25,J) (14,E) (13,W) (11,S)
(15,K) (12,H) (7,Q) (20,B)
(9,F) (6,Z)
(5,A)
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
O( log n )O( log n )Heap
O( 1 )O( n )Sorted List
O( n )O( 1 )Unsorted List
RemovalInsertionOperation
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
Recommended