Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120...

81
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sel l 100 IBM $122 Sel l 300 IBM $120 Buy 500 IBM $119 Buy 400 IBM $118

Transcript of Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120...

Priority Queues

Briana B. Morrison

Adapted from Alan Eugenio

Sell 100 IBM $122

Sell 300 IBM $120

Buy 500 IBM $119

Buy 400 IBM $118

Priority Queues 2

Topics API Application Implementation

Priority Queues 3

Priority Queue

J o b # 3C lerk

J o b # 4Su p erv is o r

J o b # 2P res id en t

J o b # 1M an ager

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.

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.

Priority Queues 5

FOR EXAMPLE, SUPPOSE A HOSPITAL

EMERGENCY ROOM HAS THE

FOLLOWING FOUR PATIENTS, WITH

NAME, PRIORITY, AND INJURY:

Priority Queues 6

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 7

THERE ARE MANY APPLICATIONS

OF PRIORITY QUEUES, IN AREAS AS

DIVERSE AS SCHEDULING, DATA

COMPRESSION, AND JPEG (Joint

Photographic Experts Group) ENCODING.

Priority Queues 8

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 9

T:THE TEMPLATE PARAMETER

CORRESPONDING TO THE TYPE OF

THE ITEMS

Priority Queues 10

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 11

THE DEFAULT CONTAINER CLASS IS

vector<T>.

Priority Queues 12

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 13

THE priority_queue CLASS INCLUDES

THE FOLLOWING

typedef Container::value_type value_type;

AND ALL OF THE SEQUENTIAL-

CONTAINER CLASSES HAVE

typedef T value_type;

SO T AND value_type ARE SYNONYMS.

Priority Queues 14

THERE ARE ONLY SEVEN METHODS

IN THE priority_queue CLASS. HERE

ARE THE METHOD INTERFACES:

Priority Queues 15

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 16

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 17

EXAMPLE SUPPOSE my_set IS A set

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 18

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 19

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.const value_type& top ( ) const;

Priority Queues 20

THE FOLLOWING PROGRAM SEGMENT MANAGES

A PRIORITY QUEUE OF doubleS:

priority_queue<double> weights; // largest weight on topweights.push (95.3);weights.push (47.6);weights.push (98.2);weights.push (88.7);while (!weights.empty()){ cout << weights.top() << “ “; weights.pop();} // while

THE OUTPUT WILL BE:98.2 95.3 88.7 47.6

Priority Queues 21

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 22

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 23

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 24

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

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

Applications

Applications: Standby flyers Auctions Stock market

Sorting Huffman Coding

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 SOutput sequence S sorted in increasing order according to CP priority queue with

comparator Cwhile !S.empty ()

e S.remove (S. first ())

P.push(e)while !P.empty()

e P.top()P.pop()

S.insertLast(e)

Priority Queues 29

APPLICATION OF

PRIORITY QUEUES:

HUFFMAN CODES

Priority Queues 30

PROBLEM:

HOW TO COMPRESS A FILE

WITHOUT LOSING INFORMATION?

COMPRESSION SAVES SPACE AND,

MORE IMPORTANTLY, SAVES TIME.

Priority Queues 31

EXAMPLE:

LET M BE A MESSAGE OF SIZE

100,000 CONSISTING OF THE

LETTERS ‘a’, b’, ‘c’, ‘d’, ‘e’.

ENCODE M INTO E, A SEQUENCE OF

BITS.

Priority Queues 32

IN ASCII (American Standard Code for

Information Interchange), EACH

CHARACTER TAKES UP 8 BITS.

IF WE USE THAT ENCODING,

|E|, the size of E, is 800,000

Priority Queues 33

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 34

IF EACH CHARACTER IS REPRESEN-

TED WITH THE SAME NUMBER OF

BITS, 3 BITS PER CHARACTER ARE

NEEDED TO ENCODE 5 CHARACTERS

UNIQUELY.

Priority Queues 35

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 36

HERE IS A POSSIBLE ENCODING:

‘a’ 000‘b’ 001‘c’ 010‘d’ 011‘e’ 100

Priority Queues 37

|E| = 100000 * 3 = 300000

Priority Queues 38

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’ 0‘b’ 1‘c’ 00‘d’ 01‘e’ 10

Priority Queues 39

|E| << 300,000

BUT

Priority Queues 40

THERE IS AMBIGUITY:

001010 COULD BE THE ENCODING OF

adda OR cee OR …

Priority Queues 41

THERE IS AMBIGUITY BECAUSE

SOME OF THE CHARACTER

ENCODINGS ARE PREFIXES OF

OTHER CHARACTER ENCODINGS.

Priority Queues 42

A PREFIX-FREE ENCODING WILL BE

UNAMBIGUOUS!

Priority Queues 43

TO GET A PREFIX-FREE ENCODING,

CREATE A BINARY TREE:

LEFT BRANCH FOR A 0 BIT

RIGHT BRANCH FOR A 1 BIT

EACH CHARACTER WILL BE A LEAF.

Priority Queues 44

0 1

0 1 0 1 c d b 0 1

a e

Priority Queues 45

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’ 010‘b’ 11‘c’ 00‘d’ 10‘e’ 011

Priority Queues 46

HERE IS ANOTHER BINARY TREE

THAT PRODUCES ANOTHER

UNAMBIGUOUS ENCODING:

0 1

0 1 b c 0 1

a 0 1

e d

Priority Queues 47

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 48

TO OBTAIN A MINIMAL PREFIX-

FREE ENCODING,CREATE A BINARY

TREE, CALLED A HUFFMAN TREE,

THAT IS ACTUALLY BASED ONTHE FREQUENCIES OF THE

CHARACTERS IN M.

Priority Queues 49

AT THE LEVEL FARTHEST FROM THE

ROOT, PUT THE LEAST-FREQUENTLY

OCCURRING CHARACTERS. THEIR

ENCODINGS WILL HAVE THE MOST

BITS.

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.

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=103Combine b and c: bc=35Combine d and bc: d(bc)=67Combine a and d(bc): a(d(bc))=131Combine e and a(d(bc)): e(a(d(bc)))=234 ... done

Priority Queues 52

Huffman Tree Example

e=103

a=64

d=32

b=13 c=22

0

0

0

0 1

1

1

1

0

10

110

1110 1111

Priority Queues 53

SUPPOSE THE FREQUENCIES ARE:

‘a’ 5,000‘b’ 20,000‘c’ 8,000‘d’ 40,000‘e’ 27,000

LEFT-BRANCH FOR LEAST

RIGHT-BRANCH FOR NEXT-TO-LEAST

Priority Queues 54

0 1

a c

Priority Queues 55

0 1

a c

NOW WHAT? THE SUM OF THESE FREQUENCIES

IS 13,000. THAT SUM IS LESS THAN b’s FREQUENCY

OF 20,000, SO WE’LL 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 56

0 1 b 0 1

a c

Priority Queues 57

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 58

0 1

e 0 1 b 0 1

a c

Priority Queues 59

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 60

0 1

d 0 1

e 0 1

0 1 b

a c

Priority Queues 61

‘a’ 1100‘b’ 111‘c’ 1101‘d’ 0‘e’ 10

Priority Queues 62

“acceded” 110011011101100100

Priority Queues 63

110011011101100100 ?

0 1

d 0 1

e 0 1

0 1 b

a c

Priority Queues 64

|E| = THE SUM, OVER ALL

CHARACTERS, OF THE NUMBER OF

BITS FOR THE CHARACTER TIMES

THE FREQUENCY OF THE

CHARACTER.

Priority Queues 65

|E| = 4 * 5000 +3 * 20000 +4 * 8000 +1 * 40000 +2 * 27000

= 206,000

Priority Queues 66

WE WILL USE A PRIORITY QUEUE TO

HOLD THE FREQUENCIES OF

CHARACTERS AND SUBTREES.

push: INSERT THE CHARACTER’S 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 67

EACH ITEM IN THE PRIORITY QUEUE

CONSISTS OF A CHARACTER-

FREQUENCY PAIR (PLUS left, right,

parent,...). FOR EXAMPLE, SUPPOSE THE

FREQUENCIES ARE: (a: 34000) (b: 20000) (c: 31000) (d: 10000) (e: 5000)

Priority Queues 68

THE ORDER OF THE ITEMS IN THE

PRIORITY QUEUE IS:

e 5000d 10000b 20000c 31000a 34000

ALL WE REALLY KNOW IS THAT top

RETURNS THE HIGHEST-PRIORITY

(THAT IS, LOWEST-FREQUENCY) ITEM.

Priority Queues 69

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 1

e d

Priority Queues 70

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 1

13000 b

0 1

e d

Priority Queues 71

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 1

15000 b 65000

0 1 0 1

e d c a

Priority Queues 72

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 73

100000 0 1

35000 0 1

15000 b 65000

0 1 0 1

e d c a

Priority Queues 74

EXERCISE: CREATE A MINIMAL, PREFIX-FREE

ENCODING FOR THE FOLLOWING CHARACTER-

FREQUENCY PAIRS:

‘a’ 20,000‘b’ 4,000‘c’ 1,000‘d’ 17,000‘e’ 25,000‘f’ 2,000‘g’ 3,000‘h’ 28,000

NOTE: YOU WILL NEED TO MAINTAIN THE

PRIORITY QUEUE, ORDERED BY FREQUENCIES.

Priority Queues 75

EXERCISE: GIVEN THE FOLLOWING

OUTPUT FILE, RE-CONSTRUCT THE

HUFFMAN TREE, AND THEN DECODE

THE ENCODED MESSAGE:

0100 carriage-return 110 blank space. 0101a 0110e 00h 0111l 111 lower-case Ls 10**1001110011010001111111011010000110110100111001111111001010100

Priority Queues 76

PQ Implementation

How would you implement a priority queue?

Several possibilities exist….

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

4 5 2 3 1 1 2 3 4 5

Implementation 1 Implementation 2

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(n2) time

4 5 2 3 1

Implementation 1 – like sorting a hand of cards (find smallest, next smallest)

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(n2) time

1 2 3 4 5

Implementation 2 – like sorting a hand of cards (put first in order, 2nd in order)

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

5 4 2 3 1

5 4 2 3 1

4 5 2 3 1

2 4 5 3 1

2 3 4 5 1

1 2 3 4 5

1 2 3 4 5

Priority Queues 81 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(log2n)