Beruflich Dokumente
Kultur Dokumente
Sell Sell Buy Buy 100 300 500 400 IBM IBM IBM IBM $122 $120 $119 $118
Topics
Priority Queues
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.
Job # 1 M anager Job # 2 President Job # 3 Clerk
Job # 4 Supervisor
Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3.
Priority Queues
3
Priority Queues
FOR EXAMPLE, SUPPOSE A HOSPITAL EMERGENCY ROOM HAS THE FOLLOWING FOUR PATIENTS, WITH NAME, PRIORITY, AND INJURY:
Priority Queues
Matt 20 sprained ankle Andrew 45 broken leg Samira 20 active labor Kerem 83 heart attack IN WHAT ORDER SHOULD THE PATIENTS BE TREATED?
Priority Queues
6
THERE ARE MANY APPLICATIONS OF PRIORITY QUEUES, IN AREAS AS DIVERSE AS SCHEDULING, DATA COMPRESSION, AND JPEG (Joint Photographic Experts Group) ENCODING.
Priority Queues
THE priority_queue CLASS IS A CONTAINER ADAPTOR, JUST LIKE THE queue AND stack CLASSES:
template<class T, class Container = vector<T>, class Compare = less<Container::value_type> > class priority_queue {
Priority Queues
Priority Queues
Container: THE TEMPLATE PARAMETER CORRESPONDING TO THE CONTAINER CLASS THAT WILL HOLD THE ITEMS. THE CONTAINER CLASS MUST SUPPORT RANDOM-ACCESS ITERATORS, AND METHODS front, push_back, AND pop_back.
Priority Queues
10
Priority Queues
11
Compare: THE TEMPLATE PARAMETER CORRESPONDING TO THE FUNCTION CLASS THAT WILL COMPARE THE ITEMS. THE DEFAULT FUNCTION CLASS IS less; THE LARGEST ITEM WILL THEN HAVE HIGHEST PRIORITY AND BE AT THE TOP OF THE PRIORITY QUEUE.
Priority Queues
12
THERE ARE ONLY SEVEN METHODS IN THE priority_queue CLASS. HERE ARE THE METHOD INTERFACES:
Priority Queues
14
1. // Postcondition: this priority queue has been initialized // with x and a copy of a Container. explicit priority_queue (const Compare& x = Compare(), const Container& = Container( ));
EXAMPLE
priority_queue<int> old_pq; priority_queue<int> pq (old_pq);
Priority Queues
15
2. // Postcondition: this priority_queue has been initialized to // a copy of the items at positions from first // (inclusive) to last (exclusive), with // Compare class x and underlying // container class y. template<class InputIterator> priority_queue (InputIterator first, InputIterator last, const Compare& x = Compare( ), const Container& y = Container( ));
Priority Queues
16
EXAMPLE
CONTAINER OF doubleS; THE ORDERING OF ITEMS IS IRREVELANT. TO CONSTRUCT A priority_queue CONTAINER pq THAT HAS A COPY OF THE ITEMS IN my_set:
priority_queue<double> pq (my_set);
THE LARGEST ITEM IN my_set WILL BE AT THE FRONT OF pq BECAUSE less<double> IS THE DEFAULT COMPARATOR.
Priority Queues
17
3. // Postcondition: The number of items in this // priority_queue has been returned. size_type size( ) const; 4. // Postcondition: if this priority_queue has no items in // it, true has been returned. Otherwise, // false has been returned. bool empty( ) const;
Priority Queues
18
5. // Postcondition: x has been inserted into this // priority_queue. The averageTime(n) is // constant, and worstTime(n) is O(n). void push (const value_type& x); 6. // Precondition: this priority_queue is not empty. // Postcondition: the item on this priority_queue that had, // before this message was sent, the // highest priority has been deleted. The // worstTime (n) is O(log n). void pop( ); 7. // Precondition: this priority_queue is not empty. // Postcondition: a constant reference to the item with // highest priority in this priority_queue has // been returned. 19 Queues ( ) const ; const value_type& top Priority
BECAUSE THE priority_queue CLASS IS A CONTAINER ADAPTOR, THE FIELDS ARE DEFINED AS PART OF THE STANDARD TEMPLATE LIBRARY:
protected: Container c; Compare comp;
Priority Queues
21
THE DEFINITIONS OF THREE OF THE METHODS, empty, size, AND top ARE DEFINED BY THE LIBRARY. FOR EXAMPLE:
const value_type& top( ) const { return c.front( ); }
Priority Queues
22
CLASS priority_queue
Constructor
<queue>
priority_queue(); Create an empty priority queue. Type T must implement the operator <.
CLASS priority_queue Operations <queue>
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
Priority Queues
23
CLASS priority_queue
Operations
<queue>
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().
Priority Queues
24
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
Priority Queues
25
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.x > b.x) return +1 else if (a.y < b.y) 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 less than q; else if (lexCompare(p, q) == 0) cout << p equals q; else if (lexCompare(p, q) > 0) cout << p greater than q;
Priority Queues
26
Applications
Applications:
Priority Queues
27
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)
28
Priority Queues
PROBLEM:
IN ASCII (American Standard Code for Information Interchange), EACH CHARACTER TAKES UP 8 BITS.
SUPPOSE WE USE A FIXED NUMBER OF BITS FOR EACH CHARACTER. WHAT IS THE MINIMUM NUMBER OF BITS NEEDED TO ENCODE 5 CHARACTERS UNIQUELY?
Priority Queues
33
IF EACH CHARACTER IS REPRESENTED WITH THE SAME NUMBER OF BITS, 3 BITS PER CHARACTER ARE NEEDED TO ENCODE 5 CHARACTERS UNIQUELY.
Priority Queues
34
IN GENERAL, THE MINIMUM NUMBER OF BITS NEEDED TO ENCODE n CHARACTERS UNIQUELY IS THE NUMBER OF BITS IN THE BINARY REPRESENTATION OF n: ceil(log2 n) ceil(x) RETURNS THE SMALLEST INTEGER GREATER THAN OR EQUAL TO x.
Priority Queues
35
Priority Queues
36
Priority Queues
37
CAN WE DO BETTER? WE WOULD LIKE AN ENCODING OF n CHARACTERS THAT DOES NOT REQUIRE ceil(log2n) BITS PER CHARACTER. HOW ABOUT THIS: a b c d e 0 1 00 01 10
Priority Queues
38
BUT
Priority Queues
39
THERE IS AMBIGUITY:
Priority Queues
40
THERE IS AMBIGUITY BECAUSE SOME OF THE CHARACTER ENCODINGS ARE PREFIXES OF OTHER CHARACTER ENCODINGS.
Priority Queues
41
Priority Queues
42
LEFT BRANCH FOR A 0 BIT RIGHT BRANCH FOR A 1 BIT EACH CHARACTER WILL BE A LEAF.
Priority Queues
43
0 c 0 a
1 1 e
0 d
1 b
Priority Queues
44
SINCE EACH CHARACTER IS A LEAF, NO TWO CHARACTERS CAN BE ON THE SAME PATH FROM THE ROOT, SO THIS GIVES A PREFIX-FREE AND UNAMBIGUOUS ENCODING:
a b c d e 010 11 00 10 011
Priority Queues
45
0 c 0 a
1 1
0 e
1
Priority Queues
46
GIVEN AN UNAMBIGUOUS ENCODING INTO E, TO DETERMINE |E|, WE NEED TO KNOW THE FREQUENCY OF EACH CHARACTER IN THE ORIGINAL MESSAGE M.
Priority Queues
47
TO OBTAIN A MINIMAL PREFIXFREE ENCODING,CREATE A BINARY TREE, CALLED A HUFFMAN TREE, THAT IS ACTUALLY CHARACTERS IN M.
BASED ON
Priority Queues
48
AT THE LEVEL FARTHEST FROM THE ROOT, PUT THE LEAST-FREQUENTLY OCCURRING CHARACTERS. THEIR ENCODINGS WILL HAVE THE MOST BITS.
Priority Queues
49
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.
Priority Queues
50
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
Priority Queues
51
10
a=64
0
d=32
110
0 b=13
1 c=22 1111
1110
Priority Queues
52
0 a
1 c
Priority Queues
54
0 a
1 c
NOW WHAT? THE SUM OF THESE FREQUENCIES IS 13,000. THAT SUM IS LESS THAN bs FREQUENCY OF 20,000, SO WELL CREATE 2 MORE BRANCHES: THIS SUBTREE WILL BE AT THE END OF THE LEFT BRANCH, AND b WILL BE THE LEAF OF THE RIGHT BRANCH:
Priority Queues
55
0 0 a 1 c
1 b
Priority Queues
56
THE SUM OF THE FREQUENCIES OF THIS SUBTREE IS 33000, WHICH IS GREATER THAN 27000, SO THIS SUBTREE SHOULD BE A RIGHT BRANCH, WITH e FREQUENCY OF 27000 AS THE LEAF OF THE LEFT BRANCH.
Priority Queues
57
e 0 a
0 1 c
1 b
Priority Queues
58
FINALLY, THE SUM OF THE FREQUENCIES OF THIS SUBTREE IS 60000, WHICH IS GREATER THAN 40000, SO THIS SUBTREE SHOULD BE A RIGHT BRANCH, AND d WITH A FREQUENCY OF 40000 SHOULD BE THE LEAF OF THE LEFT BRANCH.
Priority Queues
59
d 0 1
e 0
0 1
1 b
a
Priority Queues
c
60
a b c d e
Priority Queues
61
acceded
110011011101100100
Priority Queues
62
110011011101100100
d 0 1
e 0
0 1
1 b
a Priority Queues
63
|E| = THE SUM, OVER ALL CHARACTERS, OF THE NUMBER OF BITS FOR THE CHARACTER TIMES THE FREQUENCY OF THE CHARACTER.
Priority Queues
64
Priority Queues
65
WE WILL USE A PRIORITY QUEUE TO HOLD THE FREQUENCIES OF CHARACTERS AND SUBTREES.
push: INSERT THE CHARACTERS FREQUENCY INTO THE PRIORITY QUEUE pop: DELETE THE ITEM WITH LOWEST FREQUENCY FROM THE PRIORITY QUEUE. THE FUNCTION CLASS greater WILL BE USED TO COMPARE ITEMS.
Priority Queues
66
EACH ITEM IN THE PRIORITY QUEUE CONSISTS OF A CHARACTERFREQUENCY PAIR (PLUS left, right, parent,...). FOR EXAMPLE, SUPPOSE THE FREQUENCIES ARE: (a: 34000) (b: 20000) (c: 31000) (d: 10000) (e: 5000)
Priority Queues
67
THE ORDER OF THE ITEMS IN THE PRIORITY QUEUE IS: e 5000 d 10000 b 20000 c 31000 a 34000 ALL WE REALLY KNOW IS THAT top RETURNS THE HIGHEST-PRIORITY (THAT IS, LOWEST-FREQUENCY) ITEM.
Priority Queues
68
pop IS CALLED TWICE. THE FIRST ITEM REMOVED BECOMES A LEFT BRANCH, THE SECOND ITEM REMOVED BECOMES A RIGHT BRANCH, AND THE SUM OF THOSE FREQUENCIES IS pushED ONTO THE PRIORITY QUEUE: ( : 15000) (b: 20000) (c: 31000) (a: 34000) THE HUFFMAN TREE IS NOW: 15000 0 e
Priority Queues
1 d
69
AGAIN, pop IS CALLED TWICE, AND THE SUM OF THOSE FREQUENCIES IS pushED ONTO THE PRIORITY QUEUE: (c: 31000) (a: 34000) ( : 35000) THE HUFFMAN TREE IS NOW 35000
0 13000 0 e
Priority Queues
1 b
1 d
70
AGAIN, pop IS CALLED TWICE, THE ITEMS BECOME LEAVES IN A NEW HUFFMAN TREE, AND THE SUM OF THOSE FREQUENCIES IS pushED ONTO THE PRIORITY QUEUE: ( : 35000) ( : 65000) THE HUFFMAN TREES ARE NOW 35000
0 15000 0 e d 1
1 b 0
Priority Queues
65000 1 a 71
FINALLY WHEN ( : 35000) AND ( :65000) ARE REMOVED FROM THE PRIORITY QUEUE AND THEIR SUM IS INSERTED IN THE PRIORITY QUEUE, THE PRIORITY QUEUE IS: ( : 100000). THE FINAL HUFFMAN TREE IS:
Priority Queues
72
100000 0 35000 1
0 15000 0 e 1 d
1 b 0 c
Priority Queues
65000 1 a
73
EXERCISE: CREATE A MINIMAL, PREFIX-FREE ENCODING FOR THE FOLLOWING CHARACTERFREQUENCY PAIRS: a b c d e f g h 20,000 4,000 1,000 17,000 25,000 2,000 3,000 28,000
EXERCISE: GIVEN THE FOLLOWING OUTPUT FILE, RE-CONSTRUCT THE HUFFMAN TREE, AND THEN DECODE THE ENCODED MESSAGE:
0100 carriage-return 110 blank space . 0101 a 0110 e 00 h 0111 l 111 lower-case L s 10 ** 1001110011010001111111011010000110110100111001111111001 75 Priority Queues 010100
PQ Implementation
Priority Queues
76
Implementation 2
Performance:
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
Priority Queues
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
77
Selection-Sort
Implementation 1 like sorting a hand of cards (find smallest, next smallest)
Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequence 4 5 2 3 1 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(n2) time
Priority Queues
78
Insertion-Sort
Implementation 2 like sorting a hand of cards (put first in order, 2nd in order)
Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted sequence 1 2 3 4 5
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(n2) time
Priority Queues
79
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
5
4 2 2 1 1
4
5 4 3 2 2
2
2 5 4 3 3
3
3 3 5 4 4
1
1 1 1 5 5
80
Priority Queues
- Priority queue
Summary Slide
- 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(log2n)
Priority Queues
81
81