1 / 9

Priority Queue

Priority Queue. Data Structures CS322 Prepared By: L.Fatimah Alanizi. Priority Queue. Suppose that you have a few assignments from different courses. Which assignment will you want to work on first? You set your priority based on due days. Due days are called keys.

Télécharger la présentation

Priority Queue

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. Priority Queue Data Structures CS322 Prepared By: L.FatimahAlanizi

  2. Priority Queue Suppose that you have a few assignments from different courses. Which assignment will you want to work on first? You set your priority based on due days. Due days are called keys.

  3. What is Priority Queue? • a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. • An entry in a priority queue is simply a (key, value) pair • In a priority queue, an entry with high priority is served before an entry with low priority, So the order is determined by key. • Exe: Highest priority entry will served first ,so it is in the first order: kmin • The smallest key: If we have a finite number of entries , then the smallest key, is the key that satisfies kmin< k for any other key k • If two entries have the same key value, they are served according to their arrival in the queue.

  4. What’s so different? Stacks and Queues: • Removal order determined by order of inserting (stack LIFO, Queue FIFO) Sequences: • User chooses exact placement when inserting and explicitly chooses removal order Priority Queue • Removalorder determined by key • Key may be part of element data or separate Examples: • Processes scheduled by CPU • Hospital Emergency Rooms • College admissions process for students

  5. Priority Queue ADT insertItem(k,e): insert element e with key k extractMin( )/removeMin() : return element with minimum key and remove from queue minElement( ): Return (but do not remove) an element with the smallest key; an error condition occurs if the priority queue is empty. minKey( ): Return a smallest key; an error condition occurs if the priority queue is empty size( ): return number of elements isEmpty( ): size == 0?

  6. Priority Queue implementation 1) Implementing PQ with Unsorted Sequence: • Keys in the sequence are not sorted • Each call to insertItem(k, e) inserts element in the last of Sequence • O(1) time • Each call to extractMin( ) )/ removeMin() traverses the entire sequence to find the minimum, then removes element • O(n) time • What about other operations?

  7. Priority Queue implementation Implementing PQ with Unsorted Sequence: Example: Assume we have the elements stored in an unsorted sequence show here. To perform the extractMin()/ removeMin() operation, we have to inspect all elements to find the element (0,0) that has the smallest key.

  8. Priority Queue implementation 2) Implementing PQ with Sorted Sequence: • Keys in the sequence are sorted • Each call to insertItem(k, e) traverses sorted sequence to find correct position, then does insert • O(n) worst case • Each call to extractMin( )/removeMin()does remove first element • O(1) time • What about other operations?

  9. Priority Queue implementation (6,6) 2) Implementing PQ with Sorted Sequence: Example: To insert the pair (6,6), we have to scan through the sequence until we find the right place (between (4,4) and (7,7)).

More Related