Describes basic understanding of priority queues, their applications, methods, implementation with sorted/unsorted list, sorting applications with insertion sort and selection sort with their running times.
3. Priority Queues
Many factors determine the future value of an asset.
Let’s call this “Utility”.
Utility(asset) = priority or importance that Utility() return the
minimum value.
Use Utility() as key or order of assets in the list.
4. Definition Priority Queue
Is an Abstract Data Type.
Storing collection of prioritized elements.
Supports
arbitrary element insertion.
removal of elements in order of priority.
5. Priority Queue ADT
Each entry is a pair (key, value).
Key: used to identify or weigh that element.
Two distinct entries in a priority queue can have the same key.
Mathematical concept of total order relation, must satisfy:
Reflexive property: k ≤ k.
Antisymmetric property: if k1 ≤ k2 and k2 ≤ k1 , then k1 = k2.
Transitive property: if k1 ≤ k2 and k2 ≤ k3, then k1 ≤ k3.
7. Main methods of Priority Queue ADT
size():
Return the number of entries in the queue.
isEmpty():
Test whether the queue is empty.
min():
Return (do not remove) an entry with smallest key.
insert(k, x):
Insert an entry with key k and value x.
removeMin():
Remove and return the entry with smallest key.
8. Sorting with a Priority Queue
Sorting a collection S of n comparable elements
with a priority queue Q, called PriorityQueueSort.
Hint: Using insert() and removeMin()
If you have been asked which assets the company should purchase for investment. There are many assets like stock, , real-estate which you can choose from a big list.// What would be your criteria for choosing any asset that you would like to buy?
<number>
<number>
<number>
<number>
<number>
<number>
An important application of a priority queue is sorting. The algorithm for sorting a collection S of n comparable elements with a priority queue Q, called PriorityQueueSort, consists of two phases:
<number>
Let it be an exercise for you, if you have understood the priority queue methods in previous slide this exercise wont be very challenging,// here hint has also been given
Note that the running time of this sorting method depends on the priority queue implementation.
<number>
In this session we'll learn how to implement a priority queue by storing its entries in a list S , where the list can be sorted or unsorted.
for unsorted list, //simple way of performing insert operation is to add the element at the end of list , by executing method addLast()./
/ This implementation of method insert takes O(1) time as inserting entries at the end of list does not take into account the ordering of the keys.
To perform removeMin method , we have to inspect all the elements of list to find an entry with minimum k.
So it taken O(n) time, where n is the number of entries, or data size.// thats make implementation with an unsorted list is also called as "fast insertion and slow removal".
<number>
An alternative implementation of a priority queue is in a sorted list.
,// here insertion requires a complete scan of the list to find the appropriate position to insert the new entry so it takes O(n) time, while// removeMin method just needs to access the first element of the list as that will be the one which is least in the sorted list.and so it takes O(1) times.// it is also called as fast removal and slow insertions,
<number>
<number>
Ttwo most popular techniques for sorting, selection-sort and insertion-sort.
<number>
In selection -sort
Consider we have to sort n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[0], the first element.
Then finding the second smallest element of A, and exchanging it with A[1], the second element. Continue in this manner for the first n−1 elements of A. This method of sorting is known as selection sort.
<number>
// Selection sort occurs in two phases, in first phase we insert all the elements in priority queue P using simple insert() method,//
we ll pick first item// from the sequence insert into P,then second element// , third and so on, this way we have complete data in prority queue now, now in Phase 2 //we repeatedly remove the elements from P using the removeMin() method. we ll find the least element in the qyueue, and put it back in sequence S, // //, we ll do this till the last element in priority queue , when all the elements of queue are moved to sequence S in order.
In this whole process bottleneck is in phase 2, where we repeatedly remove an entry with smallest key from Queue P. We start with priority queue size equals n and incrementally decreases with each removeMin until it becomes 0.
<number>
//Thus, the first removeMin operation takes time O(n) as there are n entries, then the second one// takes time O(n − 1) as one entry has already been moved to sequence S and so number of elements are (n-1) now and so on, //until the last (nth) operation takes time O(1). Therefore, //the total time needed for the second phase is //
<number>
// Insertion of the second entry requires one comparison with the first entry, in order to find its proper position.
//Insertion of the third entry requires two comparisons, and
// fourth entry requires three comparisons and so on. This method is known as insertion sort, where in each iteration, one entry is added to the sorted list.
Inserting after comparison
<number>
In Phase 1, we remove the// first element of list and insert it into Priority queue P,, then second element// is removed from the sequence and we scan the data in priority queue , until we find the correct place for this element. , same step is repeated for //third element, it is compared with existing two elements in priority queue and placed at right position, and this is repeated for the whole data set.// // //
In Phase 2, we call removeMin method on Priority queue repeatedly,// here because of phase 1 every time it returns the first element of the list and// // we add that element at the end of Sequence S, and this way we get sorted sequence S.
<number>
Analyzing the running time of this whole process, we see that phase 1 is the bottleneck of this sorting ,//as for placing first element , there is no comparison , //while for second element , there is 1 comparison and so on. and like selection sort, //insertion sort also runs in O(n2) time
<number>