Download presentation
Presentation is loading. Please wait.
Published byDenis Harmon Modified over 9 years ago
1
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118
2
Priority Queues 2 Topics API Application Implementation
3
Priority Queues 3 Priority Queue A Special form of queue from which items are removed according to their designated priority and not the order in which they entered. Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3.
4
Priority Queues 4 A PRIORITY QUEUE IS A CONTAINER IN WHICH ACCESS OR DELETION IS OF THE HIGHEST-PRIORITY ITEM, ACCORDING TO SOME WAY OF ASSIGNING PRIORITIES TO ITEMS.
5
Priority Queues 5
6
6
7
7
8
8
9
9
10
10
11
Priority Queues 11
12
Priority Queues 12
13
Priority Queues 13
14
Priority Queues 14
15
Priority Queues 15
16
Priority Queues 16
17
Priority Queues 17
18
Priority Queues 18
19
Priority Queues 19
20
Priority Queues 20
21
Priority Queues 21
22
Priority Queues 22
23
Priority Queues 23 CLASS priority_queue Constructor priority_queue(); Create an empty priority queue. Type T must implement the operator <. CLASS priority_queue Operations bool empty() const; Check whether the priority queue is empty. Return true if it is empty, and false otherwise. Create void pop(); Remove the item of highest priority from the queue. Precondition:The priority queue is not empty. Postcondition:The priority queue has 1 less element
24
Priority Queues 24 CLASS priority_queue Operations void push(const T& item); Insert the argument item into the priority queue. Postcondition: The priority queue contains a new element. int size() const; Return the number of items in the priority queue. T& top(); Return a reference to the item having the highest priority. Precondition: The priority queue is not empty. const T& top(); Constant version of top().
25
Priority Queues 25 Comparator ADT A comparator encapsulates the action of comparing two objects according to a given total order relation A generic priority queue uses a comparator as a template argument, to define the comparison function ( ) The comparator is external to the keys being compared. Thus, the same objects can be sorted in different ways by using different comparators. When the priority queue needs to compare two keys, it uses its comparator
26
Priority Queues 26 Using Comparators in C++ A comparator class overloads the “()” operator with a comparison function. Example: Compare two points in the plane lexicographically. class LexCompare { public: int operator()(Point a, Point b) { if (a.x b.x) return +1 else if (a.y b.y) return +1 else return 0; } }; To use the comparator, define an object of this type, and invoke it using its “()” operator: Example of usage: Point p(2.3, 4.5); Point q(1.7, 7.3); LexCompare lexCompare; if (lexCompare(p, q) 0) cout << “p greater than q”;
27
Priority Queues 27 Applications Applications: Standby flyers Auctions Stock market Sorting Huffman Coding
28
Priority Queues 28 Sorting with a Priority Queue We can use a priority queue to sort a set of comparable elements 1.Insert the elements one by one with a series of push(e) operations 2.Remove the elements in sorted order with a series of pop() operations The running time of this sorting method depends on the priority queue implementation Algorithm PQ-Sort(S, C) Input sequence S, comparator C for the elements of S Output sequence S sorted in increasing order according to C P priority queue with comparator C while !S.empty () e S.remove (S. first ()) P.push(e) while !P.empty() e P.top() P.pop() S.insertLast(e)
29
Priority Queues 29
30
Priority Queues 30
31
Priority Queues 31
32
Priority Queues 32
33
Priority Queues 33
34
Priority Queues 34
35
Priority Queues 35
36
Priority Queues 36
37
Priority Queues 37
38
Priority Queues 38
39
Priority Queues 39
40
Priority Queues 40
41
Priority Queues 41
42
Priority Queues 42
43
Priority Queues 43
44
Priority Queues 44
45
Priority Queues 45
46
Priority Queues 46
47
Priority Queues 47
48
Priority Queues 48
49
Priority Queues 49
50
Priority Queues 50 Huffman Trees Problem Input: A set of symbols, each with a frequency of occurrence. Desired output: A Huffman tree giving a code that minimizes the bit length of strings consisting of those symbols with that frequency of occurrence. Strategy: Starting with single-symbol trees, repeatedly combine the two lowest-frequency trees, giving one new tree of frequency = sum of the two frequencies. Stop when we have a single tree.
51
Priority Queues 51 Huffman Trees (2) Implementation approach: Use a priority queue to find lowest frequency trees Use binary trees to represent the Huffman (de)coding trees Example: b=13, c=22, d=32 a=64 e=103 Combine b and c: bc=35 Combine d and bc: d(bc)=67 Combine a and d(bc): a(d(bc))=131 Combine e and a(d(bc)): e(a(d(bc)))=234... done
52
Priority Queues 52 Huffman Tree Example e=103 a=64 d=32 b=13c=22 0 0 0 01 1 1 1 0 10 110 11101111
53
Priority Queues 53
54
Priority Queues 54
55
Priority Queues 55
56
Priority Queues 56
57
Priority Queues 57
58
Priority Queues 58
59
Priority Queues 59
60
Priority Queues 60
61
Priority Queues 61
62
Priority Queues 62
63
Priority Queues 63
64
Priority Queues 64
65
Priority Queues 65
66
Priority Queues 66
67
Priority Queues 67
68
Priority Queues 68
69
Priority Queues 69
70
Priority Queues 70
71
Priority Queues 71
72
Priority Queues 72
73
Priority Queues 73
74
Priority Queues 74
75
Priority Queues 75
76
Priority Queues 76 PQ Implementation How would you implement a priority queue? Several possibilities exist….
77
Priority Queues 77 Sequence-based Priority Queue Implementation with an unsorted list Performance: push takes O(1) time since we can insert the item at the beginning or end of the sequence pop, top take O(n) time since we have to traverse the entire sequence to find the smallest key Implementation with a sorted list Performance: push takes O(n) time since we have to find the place where to insert the item pop, top take O(1) time since the smallest key is at the beginning of the sequence 4523112345 Implementation 1Implementation 2
78
Priority Queues 78 Selection-Sort Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequence Running time of Selection-sort: Inserting the elements into the priority queue with n push operations takes O(n) time Removing the elements in sorted order from the priority queue with n pop operations takes time proportional to 1 2 … n Selection-sort runs in O(n 2 ) time 45231 Implementation 1 – like sorting a hand of cards (find smallest, next smallest)
79
Priority Queues 79 Insertion-Sort Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted sequence Running time of Insertion-sort: Inserting the elements into the priority queue with n push operations takes time proportional to 1 2 … n Removing the elements in sorted order from the priority queue with a series of n pop operations takes O(n) time Insertion-sort runs in O(n 2 ) time 12345 Implementation 2 – like sorting a hand of cards (put first in order, 2 nd in order)
80
Priority Queues 80 In-place Insertion-sort Instead of using an external data structure, we can implement selection-sort and insertion-sort in-place A portion of the input sequence itself serves as the priority queue For in-place insertion-sort We keep sorted the initial portion of the sequence We can use swapElements instead of modifying the sequence 54231542314523124531234511234512345
81
Priority Queues 81 Summary Slide §- Priority queue -Pop() returns the highest priority item (largest or smallest). -Normally implemented by a heap, which is discussed later in the class. -The push() and pop() operations have running time O(log 2 n)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.