priority queues - ed. 2. and 3.: chapter 7 – - ed. 4.: chapter 8 -

61
Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Upload: selina-urch

Post on 15-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Priority Queues

- Ed. 2. and 3.: Chapter 7 –- Ed. 4.: Chapter 8 -

Page 2: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Priority Queues(Chapter 7)

• Priority Queue ADT- Keys, Priorities, and Total order Relations

- Sorting with a Priority Queue• Priority Queue implementation

- Implementation with an unsorted sequence - Implementation with a sorted sequence

Page 3: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

The Priority Queue Abstract Data Type

Suppose that you have a few assignments from different courses. Which assignment will you want to work on first? Course Priority Due day Database Systems 2 October 3 UNIX 4 October 10 Data Structure & Algorithm 1 September 29 Structured Systems Analysis 3 October 7 You set your priority based on due days. Due days are called keys.

Page 4: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

When you want to determine the priority for your assignments, you need a value for each assignment, that you can compare with each other. key: An object that is assigned to an element as a specific attribute for that element, which can be used to identify, rank, or weight that element.

Page 5: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Example: Student records Student Name Student Number Final Score Bill Scott 110102 65 Bob Jones 110140 76 Alan Smith 110243 86 Susan Kane 110176 80 Any of the attributes, Student Name, Student Number, or Final Score can be used as keys. Note: Keys may not be unique (Final Score).

Page 6: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Example: Brand Price Warranty (km) Motormaster $61.49 110,000 Goodyear $98.99 220,000 Michelin $101.99 150,000 Suppose we are looking for tires for a passenger car. How can we weight the tires so we can select the tires? The key may consist of not only one attribute such as price. In fact, we want to consider factors such as brands and warranty as well. So the key may be more complex than just one value.

Page 7: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

We want a comparison rule that will never contradict itself.This requires that the rule define a total order relation. total order relation:

•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. Examples: Integers, real numbers, lexicographic order of character sequence.

Page 8: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Not everything can have a total order relation. Non-example: For 2-D vectors v1 = (x1, x2) and v2 = (x3, x4), define the following ordering rule:

v1 v2 if x2 - x1 = = x4 - x3 Then we have 4 - 1 = 7 - 4

Therefore, (1, 4) (4, 7) and (4, 7) (1, 4).

But (1,4) (7, 4), namely, the relation does not satisfy theantisymmetric property.

Page 9: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

If a comparison rule defines a total order relation, it will neverlead to a comparison contradiction.  the smallest key: If we have a finite number of elements witha total order relation, then the smallest key, denoted by kmin, iswell-defined: kmin is the key that satisfies kmin k for any otherkey k. Being able to find the smallest key is very important because inmany cases, we want to have the element with the smallest key.

Page 10: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

priority queue: A container of elements, each having an associated key that is provided at the time the element is inserted. The two fundamental methods of a priority queue P: insertItem(k,e): Insert an element e with key k into P.

removeMin(): Return and remove from P an element with the smallest key.

Page 11: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Example 7.1 The airline company keeps a priority queue of standby passengers waiting to get a seat. Fare Paid Frequent-Flyer

Status (0 - 6 stars) Days of Standby

passenger 1 $216 2 15 passenger 2 $198 6 20 passenger 3 $315 4 7 passenger 4 $267 5 10 The keys consist of three attributes: Fare paid, frequent-flyer status, and days of standby.

Page 12: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Sorting with a Priority Queue

Recall that a priority queue has the method removeMin() to remove and return the element with the smallest key. This fact makes priority queues useful for sorting. Assume we have a sequence S of n elements The application has two steps:

1.Put the elements of S into an initially empty priority queue P by calling the method insertItem() n times.

2.Extract elements from P by calling the method removeMin() n times, and put them back into S in order.

Page 13: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Algorithm PriorityQueueSort( S, P ) for each element in S get an element from S and assign it to variable e call P.insertItem( e, e ) while P is not empty call P.removeMin() to get an element from P insert the element to S Note that the keys are the elements themselves.

Page 14: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

E x a m p l e : W e h a v e a s e q u e n c e S w i t h 6 i n t e g e r s ( e l e m e n t s ) . A l s o w e h a v e a n e m p t y p r i o r i t y q u e u e P .

4 0 7 8 2 1

S

P

Page 15: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

A f t e r t h e f i r s t s t e p , a l l t h e e l e m e n t s a r e n o w i n t h e p r i o r i t y q u e u e , w i t h t h e m s e l v e s a s k e y s .

S

4 , 40 , 0 7 , 7 8 , 82 , 21 , 1

P

Page 16: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

T h e f i r s t t h r e e e l e m e n t s h a v e b e e n e x t r a c t e d f r o m P a n d i n s e r t e d i n t o S i n o r d e r . T h e e l e m e n t w i t h t h e s m a l l e s t k e y i n P n o w i s 4 , 4 , w h i c h w i l l b e e x t r a c t e d n e x t t i m e .

0 21

S

4 , 4 7 , 7 8 , 8

P

Page 17: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

A f t e r t h e s e c o n d s t e p : N o w t h e e l e m e n t s a r e s o r t e d i n S .

40 7 821

S

P

Page 18: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Methods of a Priority Queue

The priority queue abstract data type supports the following methos:

size(): Return the number of elements in P. Input: None; Output: Integer

isEmpty(): Test whether P is empty. Input: None; Output: Boolean

inserItem(k,e): Insert a new element e with key k into P. Input: Objects k (key) and e (element); Output: None

minElement(): Return (but do not remove) an element of P with the smallest key; an error condition occurs if the priority queue is empty. Input: None; Output: Object (element)

Page 19: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

minKey(): Return a smallest key in P; an error condition occurs if the priority queue is empty. Input: None; Output: Object (key)

removeMin(): Remove from P and return an element with the smallest key; an error condition occurs if the priority queue is empty. Input: None; Output: Object (element)

Page 20: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Example 7.2: The following table shows a series of operations and their effects on an initially empty priority queue P. Operation Output Priority Queue insertItem(5,A) - {(5,A)} insertItem(9,C) - {(5,A),(9,C)} insertItem(3,B) - {(3,B),(5,A),(9,C)} insertItem(7,D) - {(3,B),(5,A),(7,D),(9,C)} minElement() B {(3,B),(5,A),(7,D),(9,C)} minKey() 3 {(3,B),(5,A),(7,D),(9,C)} removeMin() B {(5,A),(7,D),(9,C)} size() 3 {(5,A),(7,D),(9,C)} removeMin() A {(7,D),(9,C)} removeMin() D {(9,C)} removeMin() C {} removeMin() “error” {} isEmpty() true {}

Page 21: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Items in a Priority Queue

Recall that when we insert an element into a priority queue, we need to assign a key to the element. Later, when we need to extract the element from the priority queue, we need to suppy the key. Therefore, items stored in the priority queue are pairs.

Page 22: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Here is a Java implementation of the pairs as class Item. public class Item { private Object key, elem; protected Item( Object k, Object e ) { key = k; elem = e; } public Object key() { return key; } public Object element() { return elem; } public void setKey( Object k ) { key = k; } public void setElement( Object e ) { elem = e; } }

Page 23: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

The Comparator Abstract Data Type

Recall that the priority queue method removeMin() returns the element with the smallest key. To find the element with the smallest key, keys of elements in a priority queue have to be compared. This can be handled by objects called comparators. The comparator abstract data type supports the following methods:

Page 24: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

isLessThan(a,b) True if and only if a is less than b. Input: Pair of objects; Output: Boolean

isLessThanOrEqualTo(a,b): True if and only if a is less than or equal to b. Input: Pair of objects; Output: Boolean

isEqualTo(a,b): True if and only if a and b are equal. Input: Pair of objects; Output: Boolean

isGreaterThan(a,b): True if and only if a is greater than b. Input: Pair of objects; Output: Boolean

isGreaterThanOrEqualTo(a,b): True if and only if a is greater than or equal to b. Input: Pair of objects; Output: Boolean

isComparable(a): True if and only if a can be compared. Input: Object; Output: Boolean

Page 25: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Class Lexicographic

As an application of the comparator ADT, here is a Java class Lexicographic for comparison of 2-D points. public class Lexicographic implements Comparator { int xa, ya, xb, yb; // Assume objects are class Point2D objects

Page 26: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

private void getXY( Object a, Object b ) { if( a == null || b == null ) throw new InvalidElementException( "Null Argument" ); try { xa = (( Point2D )a ).getX(); ya = (( Point2D )a ).getY(); xb = (( Point2D )b ).getX(); yb = (( Point2D )b ).getY(); } catch( ClassCastException e ) { throw new InvalidElementException( "Argument not a Point2D" ); } }

Page 27: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

public boolean isLessThan( Object a, Object b ) { getXY( a, b ); if( xa == xb ) return( ya < yb ); else return( xa < xb ); }

Examples: Object a Object b return value (1, 3) (1, 4) true (7, 1) (5, 2) false (4, 5) (5, 2 ) true

Page 28: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

public boolean isLessThanOrEqualTo( Object a, Object b ) { getXY( a, b ); if( xa == xb ) return( ya <= yb ); else return( xa <= xb ); }

Examples: Object a Object b return value (2, 3) (2, 3) true (7, 1) (5, 2) false (4, 5) (5, 2 ) true

Page 29: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

public boolean isEqualTo( Object a, Object b ) { getXY( a, b ); return ( xa == xb ) && ( ya == yb ); }

Examples: Object a Object b return value (2, 3) (2, 3) true (7, 1) (5, 2) false (4, 5) (5, 2 ) false

Page 30: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

public boolean isComparable( Object a ) { if( a == null ) return a; else { try { Point2D p = ( Point2D )a; } catch( ClassCastException e ) { return false; } return true; } }

The function returns false if object a is null or it is not a Poin2D type.

Page 31: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Data Structure Exercises 14.1

Page 32: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Implementing a Priority Queue with a Sequence

Suppose that we want to write a program that using a priority queue to help us select tires. How can we implement a priority queue? Brand Price Warranty (km) Motormaster $61.49 110,000 Goodyear $98.99 220,000 Michelin $101.99 150,000

Page 33: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Recall that both a sequence and a priority queue are containers of some elements. Therefore, we can use a sequence to implement a priority queue. The elements in the sequence are the pairs of keys and elements. There are two ways to implement a priority queue with a sequence:

1.Keys in the sequence are sorted. 2.Keys in the sequence are not sorted.

Page 34: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Implementation with an Unsorted Sequence Let S be a sequence. A pair of key and element is denoted as p=(k,e). With an unsorted sequence, we use the method insertLast(p) of S to implement insertItem(k,e) of the priority queue P. To perform operations including minElement, minKey, and removeMin, we have to inspect all the elements of the sequence S to find the element with the smallest key.

Page 35: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

E x a m p l e : A s s u m e w e h a v e t h e e l e m e n t s s t o r e d i n a n u n s o r t e d s e q u e n c e s h o w h e r e . T o p e r f o r m t h e r e m o v e M i n ( ) o p e r a t i o n , w e h a v e t o i n s p e c t a l l e l e m e n t s t o f i n d t h e e l e m e n t ( 0 , 0 ) t h a t h a s t h e s m a l l e s t k e y .

4 , 4 0 , 0 7 , 7 8 , 82 , 21 , 1

P

Page 36: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Implementation with an Sorted Sequence Let S be a sequence. A pair of key and element is denoted as p=(k,e). With an sorted sequence, we can easily extract the element with the smallest key with the combination of methods remove() and first() of S. However, to perform operation insertItem, we need to scan through the sequence S to find the apropriate position to insert the new element and key.

Page 37: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

E x a m p l e : T o i n s e r t t h e p a i r ( 6 , 6 ) , w e h a v e t o s c a n t h r o u g h t h e s e q u e n c e u n t i l w e f i n d t h e r i g h t p l a c e ( b e t w e e n ( 4 , 4 ) a n d ( 7 , 7 ) ) .

4 , 40 , 0 7 , 7 8 , 82 , 21 , 1

P

6 , 6

Page 38: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Comparing the Two Implementations Assume that the size of the sequence is n. Method Unsorted S Sorted S size, isEmpty fast fast insertItem fast minElement, minKey, removeMin fast

O(n)O(n)

Page 39: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Class SortedSequencePriorityQueuepublic class SortedSequencePriorityQueue implements PriorityQueue { protected Sequence S = new NodeSequence(); protected Comparator comp; protected Object key( Position pos ) { return (( Item )pos.element()).key(); } protected Object elem( Position pos ) { return (( Item )pos.element()).element(); } protected Object elem( Object kep ) { return (( Item )kep ).element() } public SortedSequencePriorityQueue( Comparator c ) { comp = c; }

public int size() { return S.size(); } public boolean isEmpty() { return S.isEmpty() }

Page 40: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

public void insertItem( Object k, Object e ) throws InvalidKeyException { if( !comp.isComparable( k )) throws new InvalidKeyException( "The key is not valid" ); else if( S.isEmpty()) S.insertFirst( new Item( k, e )); else if( comp.isGreaterThan( k,key(S.last()))) S.insertAfter( S.last(), new Item( k, e )); else { Position curr = S.first(); while( comp.isGreaterThan( k, key( curr ))) curr = S.after( curr ); S.insertBefore( curr, new Item( k, e )); } }

4 ,40 ,0 7 ,7 8 ,82 ,21 ,1

P

6 ,6

cu rr

k = 6 , e = 6

Page 41: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

public Object removeMin() throws PriorityQueueEmptyException { if( S.isEmpty()) throw new PriorityQueueEmptyException( "The priority queue is empty" ); else return elem( S.remove( S.first())); }

Page 42: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Selection Sort and Insertion Sort

Recall that we can use a priority queue to sort elements. Depending on whether a sorted sequence is used to implement the priority queue, we have two kinds of sort. If the priority queue is implemented with an unsorted sequence, it is called selection-sort.

Page 43: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

The table here shows the performance of selection sort on sequence (7, 4, 8, 2, 5, 3, 9). Sequence S Priority Queue P Input (7, 4, 8, 2, 5, 3, 9) () Phase 1 (4, 8, 2, 5, 3, 9)

(8, 2, 5, 3, 9) … ()

(7) (7, 4)

… (7, 4, 8, 2, 5, 3, 9)

Phase 2 (2) (2, 3)

(2, 3, 4) (2, 3, 4, 5)

(2, 3, 4, 5, 7) (2, 3, 4, 5, 7, 8)

(2, 3, 4, 5, 7, 8, 9)

(7, 4, 8, 5, 3, 9) (7, 4, 8, 5, 9)

(7, 8, 5, 9) (7, 8, 9)

(8, 9) (9) ()

Page 44: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

If the priority queue is implemented with a sorted sequence, the sort is called insertion sort. The table here shows the performance of insertion sort on the same sequence S = (7, 4, 8, 2, 5, 3, 9). Sequence S Priority Queue P Input (7, 4, 8, 2, 5, 3, 9) () Phase 1 (4, 8, 2, 5, 3, 9)

(8, 2, 5, 3, 9) (2, 5, 3, 9)

(5, 3, 9) (3, 9)

(9) ()

(7) (4, 7)

(4, 7, 8) (2, 4, 7, 8)

(2, 4, 5, 7, 8) (2, 3, 4, 5, 7, 8)

(2, 3, 4, 5, 7, 8, 9) Phase 2 (2)

(2, 3) …

(2, 3, 4, 5, 7, 8, 9)

(7, 4, 8, 5, 3, 9) (7, 4, 8, 5, 9)

… ()

Page 45: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

The table here is a comparison of running time for both kinds of sort. Selection Sort Insertion Sort Phase 1 Phase 2 Overall Recall that the runnint time for bobble sort is also propotional to the size of the square of n, the size of the sequence.

O(n) O(n2)O(n2)O(n2) O(n2)

O(n)

Page 46: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Data Structure Exercises 15.1

Page 47: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Implementing a Priority Queue with a Sequence

Suppose that we want to write a program that using a priority queue to help us select tires. How can we implement a priority queue? Brand Price Warranty (km) Motormaster $61.49 110,000 Goodyear $98.99 220,000 Michelin $101.99 150,000

Page 48: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Recall that both a sequence and a priority queue are containers of some elements. Therefore, we can use a sequence to implement a priority queue. The elements in the sequence are the pairs of keys and elements. There are two ways to implement a priority queue with a sequence:

1. Keys in the sequence are sorted. 2. Keys in the sequence are not sorted.

Page 49: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Implementation with an Unsorted Sequence Let S be a sequence. A pair of key and element is denoted as p=(k,e). With an unsorted sequence, we use the method insertLast(p) of S to implement insertItem(k,e) of the priority queue P. To perform operations including minElement, minKey, and removeMin, we have to inspect all the elements of the sequence S to find the element with the smallest key.

Page 50: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

E x a m p l e : A s s u m e w e h a v e t h e e l e m e n t s s t o r e d i n a n u n s o r t e d s e q u e n c e s h o w h e r e . T o p e r f o r m t h e r e m o v e M i n ( ) o p e r a t i o n , w e h a v e t o i n s p e c t a l l e l e m e n t s t o f i n d t h e e l e m e n t ( 0 , 0 ) t h a t h a s t h e s m a l l e s t k e y .

4 , 4 0 , 0 7 , 7 8 , 82 , 21 , 1

P

Page 51: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Implementation with an Sorted Sequence Let S be a sequence. A pair of key and element is denoted as p=(k,e). With an sorted sequence, we can easily extract the element with the smallest key with the combination of methods remove() and first() of S. However, to perform operation insertItem, we need to scan through the sequence S to find the apropriate position to insert the new element and key.

Page 52: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

E x a m p l e : T o i n s e r t t h e p a i r ( 6 , 6 ) , w e h a v e t o s c a n t h r o u g h t h e s e q u e n c e u n t i l w e f i n d t h e r i g h t p l a c e ( b e t w e e n ( 4 , 4 ) a n d ( 7 , 7 ) ) .

4 , 40 , 0 7 , 7 8 , 82 , 21 , 1

P

6 , 6

Page 53: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Comparing the Two Implementations Assume that the size of the sequence is n. Method Unsorted S Sorted S size, isEmpty fast fast insertItem fast minElement, minKey, removeMin fast

Page 54: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Class SortedSequencePriorityQueuepublic class SortedSequencePriorityQueue implements PriorityQueue { protected Sequence S = new NodeSequence(); protected Comparator comp; protected Object key( Position pos ) { return (( Item )pos.element()).key(); } protected Object elem( Position pos ) { return (( Item )pos.element()).element(); } protected Object elem( Object kep ) { return (( Item )kep ).element() } public SortedSequencePriorityQueue( Comparator c ) { comp = c; }

public int size() { return S.size(); } public boolean isEmpty() { return S.isEmpty() }

Page 55: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

public void insertItem( Object k, Object e ) throws InvalidKeyException { if( !comp.isComparable( k )) throws new InvalidKeyException( "The key is not valid" ); else if( S.isEmpty()) S.insertFirst( new Item( k, e )); else if( comp.isGreaterThan( k,key(S.last()))) S.insertAfter( S.last(), new Item( k, e )); else { Position curr = S.first(); while( comp.isGreaterThan( k, key( curr ))) curr = S.after( curr ); S.insertBefore( curr, new Item( k, e )); } }

4 ,40 ,0 7 ,7 8 ,82 ,21 ,1

P

6 ,6

cu rr

k = 6 , e = 6

Page 56: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

public Object removeMin() throws PriorityQueueEmptyException { if( S.isEmpty()) throw new PriorityQueueEmptyException( "The priority queue is empty" ); else return elem( S.remove( S.first())); }

Page 57: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Selection Sort and Insertion Sort

Recall that we can use a priority queue to sort elements. Depending on whether a sorted sequence is used to implement the priority queue, we have two kinds of sort. If the priority queue is implemented with an unsorted sequence, it is called selection-sort.

Page 58: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

The table here shows the performance of selection sort on sequence (7, 4, 8, 2, 5, 3, 9). Sequence S Priority Queue P Input (7, 4, 8, 2, 5, 3, 9) () Phase 1 (4, 8, 2, 5, 3, 9)

(8, 2, 5, 3, 9) … ()

(7) (7, 4)

… (7, 4, 8, 2, 5, 3, 9)

Phase 2 (2) (2, 3)

(2, 3, 4) (2, 3, 4, 5)

(2, 3, 4, 5, 7) (2, 3, 4, 5, 7, 8)

(2, 3, 4, 5, 7, 8, 9)

(7, 4, 8, 5, 3, 9) (7, 4, 8, 5, 9)

(7, 8, 5, 9) (7, 8, 9)

(8, 9) (9) ()

Page 59: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

If the priority queue is implemented with a sorted sequence, the sort is called insertion sort. The table here shows the performance of insertion sort on the same sequence S = (7, 4, 8, 2, 5, 3, 9). Sequence S Priority Queue P Input (7, 4, 8, 2, 5, 3, 9) () Phase 1 (4, 8, 2, 5, 3, 9)

(8, 2, 5, 3, 9) (2, 5, 3, 9)

(5, 3, 9) (3, 9)

(9) ()

(7) (4, 7)

(4, 7, 8) (2, 4, 7, 8)

(2, 4, 5, 7, 8) (2, 3, 4, 5, 7, 8)

(2, 3, 4, 5, 7, 8, 9) Phase 2 (2)

(2, 3) …

(2, 3, 4, 5, 7, 8, 9)

(7, 4, 8, 5, 3, 9) (7, 4, 8, 5, 9)

… ()

Page 60: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

The table here is a comparison of running time for both kinds of sort. Selection Sort Insertion Sort Phase 1 Phase 2 Overall Recall that the runnint time for bobble sort is also propotional to the size of the square of n, the size of the sequence.

Page 61: Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -

Data Structure Exercises 15.1