100 likes | 439 Views
The Priority Queue ADT. A priority queue is a restricted form of a list, where items are arranged according to their priorities (or keys ). The key assigned to an item may not be unique. Example: a multi-user operating system, where jobs are processed not as they
E N D
The Priority Queue ADT A priority queue is a restricted form of a list, where items are arranged according to their priorities (or keys). The key assigned to an item may not be unique. Example: a multi-user operating system, where jobs are processed not as they arrive but according to their priorities. Stats front of the priority queue Print Bank rear of the “mini queue” with priority 3 Copy Check rear of the “mini queue” with priority 2 Update Average Test rear of the “mini queue” with priority 1
The total order relation The relation among items on the priority queue is that of total order, <=. The total order relation satisfied the following properties: Reflexivity, i.e. k <= k Antisymetry, i.e. if k1 <= k2 and k2 <= k1 then k1 = k2 Transitivity, i.e. if k1 <= k2 and k2 <= k3 then k1 <= k3 Priority Queue ADT operations: - insertItem (key, data): inserts data in the priority queue according to key - removeItem (): removes the item with the smallest key (in the min priority queue), or the item with the largest key (in the max priority queue). - size() - empty() - minItem() / maxitem(): (returns the item with the smallest/largest key) - minKey() / maxKey() :(returns the smallest/largest key)
Sorting with a priority queue Given a sequence of n items, a priority queue can be used to sort this sequence as follows: Step 1: Insert the n items in a priority queue by means on n insertItem operations. Step 2: Remove items from the priority queue by means of n removeItem operations. Algorithm PriorityQueueSort (S, PQ): Input: Sequence, S, of n items, and empty priority queue, PQ. Output: Sequence S sorted by the total order relation. while ! S.empty() do item := S.removeFirst() PQ.insertItem(item, item) while ! PQ.empty do item := PQ.removeItem() S.insertLast(item)
Implementation of a priority queue as unordered sequence Each item on a priority queue is a composition of two objects -- the key, stating the priority of that item, and the data. A pair (key, data) is a simple composition pattern, which can be defined by means of the following class. class Item { private int key, data; private Item next, prev; public Item () { key = 0; data = 0; next = null; prev = null; } public Item (int newKey, int newData, Item newNext, Item newPrev) { key = newKey; data = newData; next = newNext; prev = newPrev; }
public int getKey () { return key; } public int getData () { return data; } public Item getNext () { return next; } public Item getPrev () { return prev; } public void setKey (int newKey) { key = newKey; } public void setData (int newData) { data = newData; } public void setNext (Item newNext) { next = newNext; } public void setPrev (Item newPrev) { prev = newPrev; } public void displayItem () { System.out.println ("key: " + key + "; data: " + data); } } Implementation of a priority queue as unordered sequence (Item class, cont.)
The Comparator ADT To compare key, we use comparator objects. These are external to the keys to be compared and are defined by the following set of operations: lessThan (a,b) Returns true if a is less than b lessThanOrEqual (a,b) Returns true if a is less than or equal to b greaterThan (a,b) Returns true if a is greater than b greaterThanOrEqual (a,b) Returns true if a is greater than or equal to b equal (a,b) Returns true if a is equal b comparable (a) Returns true if a can be compared Comparators allow objects to be compared w.r.t. the specified ordering relationship. For example, in min priority queues items will be compared with the lessThan operator, while in max priority queues the greaterThan operator will be used; no other change in the implementation is needed.
The Comparator Class class Comparator { public boolean equal (int a, int b) { return (a == b); } public boolean lessThan (int a, int b) { return (a < b); } public boolean lessThanOrEqual (int a, int b) { return ((a == b) || (a < b)); } public boolean greaterThan (int a, int b) { return (a > b); } public boolean greaterThanOrEqual (int a, int b) { return ((a > b) || (a == b)); } }
Implementation of a priority queue with unordered sequence (cont.) Consider the elements of the sequence, S, to be composition objects (comprised by key and data). Then, we can implement insertItem() by using the insertFirst() method of the sequence ADT. This method has O(1) efficiency. The resulting sequence, however, is not ordered. Therefore, the rest of the methods minItem(), maxItem(), removeItem(), minKey() and maxKey() require the whole sequence to be traversed and thus their efficiency is O(n).
Implementation of a priority queue with ordered sequence An alternative implementation of a priority queue utilizes a sequence sorted by keys, such that the first element of the sequence has the smallest key (for min priority queues) or the largest key (for max priority queues). Then, methods minItem(), maxItem(), removeItem(), minKey() and maxKey() are all O(1) methods (within min or max priority queue frameworks), but insertItem() method will require the traversal of the entire sequence to find the location of the item to be inserted; thus its efficiency is O(n).
class PQADT implements PQ { private int size; private Item header, trailer; Comparator relation = new Comparator (); public PQADT () { ... body of the constructor... } public boolean empty () { ... body of empty ... } public int size () { ... body of size ... } public Item searchItem (int key) { Item temp = header.getNext(); while (temp != trailer) { if (relation.lessThan(temp.getKey(), key)) temp = temp.getNext(); else return temp.getPrev(); } return temp.getPrev(); } public int minItem () { return header.getNext().getData(); } public int minKey () { return header.getNext().getKey(); } public void insertItem (int newKey, int newData) { Item temp = searchItem(newKey); Item newItem = new Item (newKey, newData, temp.getNext(), temp); temp.getNext().setPrev(newItem); temp.setNext(newItem); size++; } public int removeItem () { int data; data = header.getNext().getData(); size--; header.setNext(header.getNext().getNext()); return data; } public void traverse () { System.out.println ("Current priority queue: "); Item current = header.getNext(); while (current != trailer) { current.displayItem(); current = current.getNext(); } System.out.println(); } } Implementation of a priority queue with ordered sequence (the PQADT class)