1 / 16

Priority Queues

CS 105. Priority Queues. Definition. The Priority Queue Data Structure stores elements with assigned priorities insertion of an object must include a priority level (key)

sophiab
Télécharger la présentation

Priority Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 105 Priority Queues

  2. 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

  3. 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.

  4. 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)

  5. 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

  6. 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

  7. 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

  8. The Heap Data Structure (4,C) (6,Z) (5,A) (9,F) (15,K) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (8,W)

  9. 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

  10. 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

  11. Insertion (2,T) (4,C) (4,C) (6,Z) (2,T) (5,A) 2,T (9,F) (6,Z) (15,K) (7,Q) (20,B) (2,T) 20,B (16,X) (25,J) (14,E) (12,H) (11,S) (8,W) (8,W)

  12. 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

  13. Removal (4,C) (5,A) (13,W) (4,C) (13,W) (6,Z) (9,F) (5,A) (13,W) (9,F) (12,H) (15,K) (7,Q) (20,B) (13,W) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W)

  14. 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 )

  15. Time complexity summary Operation Insertion Removal Unsorted List O( 1 ) O( n ) Sorted List O( n ) O( 1 ) Heap O( log n ) O( log n )

  16. About Priority Queues Choose an implementation that fits the application’s requirements Note tradeoff 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 insert method needs to be validated if key is of type Object

More Related