Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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. Priority Due day Database Systems 2 October 3 UNIX 4 October 10 Data Structure & Algorithm 1 September 29 Structured Systems Analysis 3 October 7 Course 2 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. 3 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 • 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 4 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? 5 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? 6 Priority Queue implementation 1) Implementing PQ with Unsorted Sequence: Example: Assume we have the elements stored in an unsorted sequence show here. (1,1) (4,4) (0,0) (7,7) (8,8) To perform the extractMin()/ removeMin() operation, we have to inspect all elements to find the element (0,0) that has the smallest key. 7 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? 8 Priority Queue implementation 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)). (0,0) (1,1) (4,4) (7,7) (6,6) 9 (8,8)