Presentation is loading. Please wait.

Presentation is loading. Please wait.

7.2 Priority Queue ADTs Priority queue concepts

Similar presentations


Presentation on theme: "7.2 Priority Queue ADTs Priority queue concepts"— Presentation transcript:

1 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

2 Priority queue concepts
A priority queue is a queue in which the elements are prioritized. The least element 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.

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

4 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: Read a person, p, from inputfile Add p to the priority queue pqueue. 3. While pqueue is not empty, repeat: Remove the least element from pqueue into p Write p to outputfile. 4. Terminate.

5 Priority Queue ADT: 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.

6 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

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

8 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.) }

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

10 Implementation using sorted SLLs
Examples of representing priority queues with sorted SLLs. first Empty priority queue: Initially: first Hamed 1958 Maged 1960 After adding Ali: first Hamed 1958 Maged 1960 Ali 1981 After adding Layla: first Hamed 1958 Maged 1960 Ali 1981 Layla 1982 After two removals: first Ali 1981 Layla 1982

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

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

13 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.Prioritize(); 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(); } } QuePrSLL.CPP

14 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

15 Comparison of implementations
We can similarly implement a priority queue using a sorted or an unsorted array. Time complexities of main operations: Operation Sorted SLL Unsorted SLL Sorted Array Unsorted Array Add O(n) O(1) Delete getFirst

16 Comparison (continued)
Consider our sorting application again. To sort a file containing n persons, we must perform n Add operations followed by n Delete 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.


Download ppt "7.2 Priority Queue ADTs Priority queue concepts"

Similar presentations


Ads by Google