Upload
bianca-levine
View
18
Download
0
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
Priority QueueData Structures
CS322Prepared By: L.Fatimah Alanizi
2
Priority Queue
Course Priority Due dayDatabase Systems 2 October 3
UNIX 4 October 10
Data Structure & Algorithm
1 September 29
Structured Systems Analysis
3 October 7
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
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.
What is Priority Queue?
4
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 orderPriority Queue• Removal order 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
What’s so different?
5
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?
Priority Queue ADT
6
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?
Priority Queue implementation
7
1) 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.
Priority Queue implementation
(8,8) (7,7) (0,0) (4,4) (1,1)
8
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?
Priority Queue implementation
9
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)).
Priority Queue implementation
(8,8) (7,7) (4,4) (1,1) (0,0)
(6,6)