160 likes | 462 Views
7.2 Priority Queue ADTs. Priority queue concepts Applications of priority queues A priority queue ADT – requirements, contract Implementation of priority queues using linked lists. Priority queue concepts. A priority queue is a queue in which the elements are prioritized .
E N D
7.2Priority Queue ADTs • Priority queue concepts • Applications of priority queues • A priority queue ADT – requirements, contract • Implementation of priority queues using linked lists
Priority queue concepts • A priorityqueue is a queue in which the elements are prioritized. • The leastelement in the priority queue is of the highest priority is always removed first. • The length of a priority queue is the number of elements it contains. • An empty priority queue has length zero.
Applications of priority queues • Priority queues are used in many computing applications. • For example, many operating systems used a scheduling algorithm where the next process executed is the one with the shortest execution time or the highest priority. • For another example, consider the problem of sorting a file of data representing persons. • We can use a priority queue to sort the data by: • first adding all of the persons to a queue, • sort data in certain order according to the specified priority, and then • removing them in turn from the priority queue (Front element first) .
Applications of priority queues (continued) • The following algorithm will sort the data: To sort an unsorted file of person data, input, to give a sorted file, output:1. Let pqueue be an empty priority queue.2. While inputfile contains more data, repeat: 2.1. Read a person, p, from inputfile. 2.2. Add p to the priority queue pqueue.3. While pqueue is not empty, repeat: 3.1. Remove the least element from pqueue into p. 3.2. Write p to outputfile.4. Terminate.
Priority Queue ADT: requirements • Requirements: 1 It must be possible to make a priority queue empty. 2 It must be possible to test whether a priority queue is empty. 3 It must be possible to obtain the length of a priority queue. 4 It must be possible to add an element to a priority queue. 5 It must be possible to remove the least element in a priority queue. 6 It must be possible to access the least element in a priority queue without removing it.
Priority Queue ADT: contract • Possible contract: public PriorityQueue { // Each PriorityQueue object is a queue that has // its elements prioritized or to be chosen // according to a specified priority
Priority Queue ADT: contract (continued) //////////// Accessors //////////// public:bool isEmpty (); // Return true if and only if this priority queue is empty. int size (); // Return the length of this priority queue. void getLeast (); // Return the least element of this priority queue.// (If there are several least elements, return any// of them.)
Priority Queue ADT: contract (continued) //////////// Transformers //////////// void clear (); // Make this priority queue empty. void add (elem); // Add elem to this priority queue. Object removeLeast(); // Remove and return the least element from this // priority queue. (If there are several least elements,// remove the same element that would be returned// by getLeast.) }
Implementation of priority queues using SLLs • Represent an (unbounded) priority queue by: • an SLL, each node containing one element. • a variable length. • Two choices: • keep the elements of the linked list sorted, so the least element is always the first element in the SLL. Insert new elements at the correct position in the SLL. Header contains link to first node only. • keep the elements of the priority queue unsorted. Insert new elements at the end of the SLL. getLeast and removeLeast must search the entire SLL for the least element. Header contains links to the front and rear nodes.
first Hamed 1958 Maged 1960 Initially: After addingAli: first Ali 1981 Hamed 1958 Maged 1960 Empty priority queue: first After addingLayla: first Ali 1981 Layla 1982 Hamed 1958 Maged 1960 After tworemovals: first Ali 1981 Layla 1982 Implementation using sorted SLLs • Examples of representing priority queues with sorted SLLs.
Implementation using sorted SLLs (cont’d) • C++ implementation: The PriorityQueueSLL is a QueueSLL ADT with the following modifications: • The class name is changed from QueSLL to PriorityQueSLL. • The PriorityQueSLL ADT class now contains the Prioritize() method that sort the queue according to the required priority (ascending Data value). Other QueSLL methods are kept unchanged in PriorityQueSLL.
Implementation using sorted SLLs (cont’d) // Build the Prioritized Queue void PriorityQueSLL::Prioritize() { QueNode *p, *after, temp; cout << "\nPrioritized (Sorted) Queue:" << endl; cout << "=================================" << endl; if(Front) { p = Front; while(p) { after = p->Next; while(after) { if(after->Data < p->Data) { temp = *p; *p = *after; *after = temp; after->Next = p->Next; p->Next = temp.Next; } after = after->Next; } p = p->Next; } } }
Implementation using sorted SLLs (cont’d) void main() { PriorityQueSLL PQlist; PQlist.Add('a'); PQlist.Add('b'); PQlist.Add('c'); PQlist.PrintQue(); cout << "Number of Nodes in Queue = " << PQlist.Size() << endl; PQlist.Add('f'); PQlist.Add('m'); PQlist.Add('e'); PQlist.Add('z'); PQlist.Add('a'); PQlist.Add('k'); PQlist.PrintQue(); cout << "Number of Nodes in Queue = " << PQlist.Size() << endl; PQlist.Prioritize(); PQlist.PrintQue(); cout << "\n\nDeleting Nodes from Queue" << endl; cout << "\n\nNo Nodes in Queue" << endl; cout << "== =================" << endl; while (!PQlist.QueIsEmpty()) { cout << PQlist.Size() << " "; PQlist.PrintQue(); PQlist.Delete(); } cout << "Number of Nodes in Queue = " << PQlist.Size() << endl; } QuePrSLL.CPP
Output a b c Number of Nodes in Queue = 3 a b c f m e z a k Number of Nodes in Queue = 9 Prioritized (Sorted) Queue: ======================= a a b c e f k m z Deleting Nodes from Queue No Nodes in Queue == ============ 9 a a b c e f k m z 8 a b c e f k m z 7 b c e f k m z 6 c e f k m z 5 e f k m z 4 f k m z 3 k m z 2 m z 1 z Queue is Empty Number of Nodes in Queue = 0 QuePrSLL.CPP
Comparison of implementations • We can similarly implement a priority queue using a sorted or an unsorted array. • Time complexities of main operations:
Comparison (continued) • Consider our sorting application again. • To sort a file containing n persons, we must perform nAdd operations followed by nDelete operations. • For the array- and SLL-based implementations, either the Add operation or the Delete operation has time complexity O(n). • Thus our sorting algorithm will have an overall time complexity of O(n2). • Clearly we can do better than this: we need a more efficient data structure.