34
Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

Embed Size (px)

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

Page 1: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

Priority QueuesCS 110: Data Structures and

AlgorithmsFirst Semester, 2010-2011

Page 2: 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

Page 3: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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

Page 4: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 5: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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

Page 6: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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

Page 7: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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

Page 8: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 9: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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

Page 10: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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

Page 11: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 12: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 13: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 14: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 15: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 16: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 17: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 18: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 19: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 20: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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

Page 21: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 22: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 23: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 24: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 25: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 26: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 27: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 28: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 29: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 30: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 31: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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)

Page 32: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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 )

Page 33: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

Time Complexity Summary

O( log n )O( log n )Heap

O( 1 )O( n )Sorted List

O( n )O( 1 )Unsorted List

RemovalInsertionOperation

Page 34: Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

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