csc311: data structures 1 chapter 8: priority queues objectives: priority queue adt comparator...

39
CSC311: Data Structures CSC311: Data Structures 1 Chapter 8: Priority Chapter 8: Priority Queues Queues Objectives: Objectives: Priority Queue ADT Priority Queue ADT Comparator design pattern Comparator design pattern Heaps Heaps Priority Queue Implementation Priority Queue Implementation with List and Heap with List and Heap Adaptable Priority Queues Adaptable Priority Queues Sorting: Sorting: Priority Queue-sort Priority Queue-sort Selection-sort Selection-sort Insert-sort Insert-sort Heap-ort Heap-ort

Post on 20-Dec-2015

244 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

CSC311: Data StructuresCSC311: Data Structures 11

Chapter 8: Priority QueuesChapter 8: Priority Queues

Objectives:Objectives: Priority Queue ADTPriority Queue ADT Comparator design patternComparator design pattern HeapsHeaps Priority Queue Implementation with List Priority Queue Implementation with List

and Heapand Heap Adaptable Priority QueuesAdaptable Priority Queues Sorting:Sorting:

– Priority Queue-sortPriority Queue-sort– Selection-sort Selection-sort – Insert-sort Insert-sort – Heap-ortHeap-ort

Page 2: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 22

Priority Queue ADTPriority Queue ADTA priority queue stores a A priority queue stores a collection of entriescollection of entriesEach Each entryentry is a pair is a pair(key, value)(key, value)Main methods of the Main methods of the Priority Queue ADTPriority Queue ADT– insertinsert(k, x)(k, x)

inserts an entry with key k inserts an entry with key k and value xand value x

– removeMinremoveMin()()removes and returns the removes and returns the entry with smallest keyentry with smallest key

Additional methodsAdditional methods– minmin()()

returns, but does not returns, but does not remove, an entry with remove, an entry with smallest keysmallest key

– sizesize(), (), isEmptyisEmpty()()

Applications:Applications:– Standby flyersStandby flyers– AuctionsAuctions– Stock marketStock market

Page 3: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 33

Total Order RelationsTotal Order RelationsKeys in a priority Keys in a priority queue can be queue can be arbitrary objects arbitrary objects on which an on which an order is definedorder is definedTwo distinct Two distinct entries in a entries in a priority queue priority queue can have the can have the same keysame key

Mathematical concept Mathematical concept of total order relation of total order relation – Reflexive property:Reflexive property:

x x x x– Antisymmetric Antisymmetric

property:property:x x y y y y x x x x == y y

– Transitive property:Transitive property: x x y y y y z z x x z z

Page 4: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 44

Entry ADTEntry ADTAn An entryentry in a priority in a priority queue is simply a key-queue is simply a key-value pairvalue pairPriority queues store Priority queues store entries to allow for entries to allow for efficient insertion and efficient insertion and removal based on removal based on keyskeysMethods:Methods:– keykey(): returns the key (): returns the key

for this entryfor this entry– valuevalue(): returns the (): returns the

value associated with value associated with this entrythis entry

As a Java interface:As a Java interface:/** /** * Interface for a key-value* Interface for a key-value * pair entry * pair entry **/**/public interface public interface Entry {Entry { public public Object key();Object key(); public public Object value();Object value();}}

Page 5: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 55

Comparator ADTComparator ADTA comparator A comparator encapsulates the action encapsulates the action of comparing two of comparing two objects according to a objects according to a given total order relationgiven total order relationA generic priority queue A generic priority queue uses an auxiliary uses an auxiliary comparatorcomparatorThe comparator is The comparator is external to the keys external to the keys being comparedbeing comparedWhen the priority queue When the priority queue needs to compare two needs to compare two keys, it uses its keys, it uses its comparatorcomparator

The primary method of The primary method of the Comparator ADT:the Comparator ADT:– comparecompare(x, y): Returns an (x, y): Returns an

integer integer i i such that such that i < i < 0 if 0 if a < ba < b, , i i = 0 if = 0 if a a = = bb, and , and i i > > 0 if 0 if a > ba > b; an error ; an error occurs if occurs if a a and and b b cannot cannot be compared.be compared.

Page 6: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 66

Example ComparatorExample ComparatorLexicographic comparison of 2-D Lexicographic comparison of 2-D points:points:

/** Comparator for 2D points under the /** Comparator for 2D points under the standard lexicographic order. */standard lexicographic order. */

public class public class Lexicographic Lexicographic implements implements Comparator {Comparator {

int int xa, ya, xb, yb;xa, ya, xb, yb; public int public int compare(Object a, Object compare(Object a, Object

b) b) throws throws ClassCastException {ClassCastException { xa = ((Point2D) a).getX();xa = ((Point2D) a).getX(); ya = ((Point2D) a).getY();ya = ((Point2D) a).getY(); xb = ((Point2D) b).getX();xb = ((Point2D) b).getX(); yb = ((Point2D) b).getY();yb = ((Point2D) b).getY(); if if (xa != xb)(xa != xb)

return return (xb - xa);(xb - xa); elseelse

return return (yb - ya);(yb - ya); }}}}

Point objects:Point objects:

/** Class representing a point in /** Class representing a point in the plane with integer the plane with integer coordinates */coordinates */

public class public class Point2DPoint2D {{ protected int protected int xc, yc; // xc, yc; //

coordinatescoordinates public public Point2D(Point2D(int int x, x, int int y) y)

{{ xc = x;xc = x; yc = y;yc = y; }} public int public int getX() { getX() {

return return xc;xc; }} public int public int getY() { getY() {

return return yc;yc; }}}}

Page 7: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 77

Priority Queue SortingPriority Queue SortingWe can use a priority We can use a priority queue to sort a set of queue to sort a set of comparable elementscomparable elements1.1. Insert the elements Insert the elements

one by one with a one by one with a series of series of insertinsert operationsoperations

2.2. Remove the elements Remove the elements in sorted order with a in sorted order with a series of series of removeMinremoveMin operationsoperations

The running time of this The running time of this sorting method depends sorting method depends on the priority queue on the priority queue implementationimplementation

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.isEmpty ()

e S.removeFirst ()P.insert (e, 0)

while P.isEmpty()e

P.removeMin().key()S.insertLast(e)

Page 8: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 88

Sequence-based Priority QueueSequence-based Priority Queue

Implementation with Implementation with an unsorted listan unsorted list

Performance:Performance:– insertinsert takes takes OO(1)(1) time time

since we can insert since we can insert the item at the the item at the beginning or end of beginning or end of the sequencethe sequence

– removeMin removeMin and and minmin take take OO((nn)) time since time since we have to traverse we have to traverse the entire sequence the entire sequence to find the smallest to find the smallest key key

Implementation with Implementation with a sorted lista sorted list

Performance:Performance:– insertinsert takes takes OO((nn)) time time

since we have to find since we have to find the place where to the place where to insert the iteminsert the item

– removeMinremoveMin and and minmin take take OO(1)(1) time, since time, since the smallest key is at the smallest key is at the beginningthe beginning

4 5 2 3 1 1 2 3 4 5

Page 9: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 99

Selection-SortSelection-Sort

Selection-sort is the variation of PQ-sort Selection-sort is the variation of PQ-sort where the priority queue is implemented with where the priority queue is implemented with an unsorted sequencean unsorted sequence

Running time of Selection-sort:Running time of Selection-sort:1.1. Inserting the elements into the priority queue with Inserting the elements into the priority queue with

nn insertinsert operations takes operations takes OO((nn) ) timetime

2.2. Removing the elements in sorted order from the Removing the elements in sorted order from the priority queue with priority queue with nn removeMinremoveMin operations takes operations takes time proportional totime proportional to

1 1 2 2 ……nn

Selection-sort runs in Selection-sort runs in OO((nn22) ) time time

Page 10: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1010

Selection-Sort ExampleSelection-Sort Example Sequence Sequence SS Priority Queue Priority Queue PPInput:Input: (7(7,,44,,88,,22,,55,,33,,9)9) ()()

Phase 1Phase 1(a)(a) (4(4,,88,,22,,55,,33,,9)9) (7)(7)(b)(b) (8(8,,22,,55,,33,,9)9) (7(7,,4)4).... .... ...... .. ..(g)(g) ()() (7(7,,44,,88,,22,,55,,33,,9)9)

Phase 2Phase 2(a)(a) (2)(2) (7(7,,44,,88,,55,,33,,9)9)(b)(b) (2(2,,3)3) (7(7,,44,,88,,55,,9)9)(c)(c) (2(2,,33,,4)4) (7(7,,88,,55,,9)9)(d)(d) (2(2,,33,,44,,5)5) (7(7,,88,,9)9)(e)(e) (2(2,,33,,44,,55,,7)7) (8(8,,9)9)(f)(f) (2(2,,33,,44,,55,,77,,8)8) (9)(9)(g)(g) (2(2,,33,,44,,55,,77,,88,,9)9) ()()

Page 11: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1111

Insertion-SortInsertion-Sort

Insertion-sort is the variation of PQ-sort Insertion-sort is the variation of PQ-sort where the priority queue is implemented where the priority queue is implemented with a sorted sequencewith a sorted sequence

Running time of Insertion-sort:Running time of Insertion-sort:1.1. Inserting the elements into the priority queue with Inserting the elements into the priority queue with

nn insertinsert operations takes time proportional to operations takes time proportional to 1 1 2 2 ……nn

2.2. Removing the elements in sorted order from the Removing the elements in sorted order from the priority queue with a series of priority queue with a series of nn removeMinremoveMin operations takes operations takes OO((nn) ) timetime

Insertion-sort runs in Insertion-sort runs in OO((nn22) ) time time

Page 12: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1212

Insertion-Sort ExampleInsertion-Sort ExampleSequence Sequence SS Priority queue Priority queue PP

Input:Input: (7(7,,44,,88,,22,,55,,33,,9)9) ()()

Phase 1Phase 1 (a)(a) (4(4,,88,,22,,55,,33,,9)9) (7)(7)

(b)(b) (8(8,,22,,55,,33,,9)9) (4(4,,7)7)(c)(c) (2(2,,55,,33,,9)9) (4(4,,77,,8)8)(d)(d) (5(5,,33,,9)9) (2(2,,44,,77,,8)8)(e)(e) (3(3,,9)9) (2(2,,44,,55,,77,,8)8)(f)(f) (9)(9) (2(2,,33,,44,,55,,77,,8)8)(g)(g) ()() (2(2,,33,,44,,55,,77,,88,,9)9)

Phase 2Phase 2(a)(a) (2)(2) (3(3,,44,,55,,77,,88,,9)9)(b)(b) (2(2,,3)3) (4(4,,55,,77,,88,,9)9).... .... ...... .. ..(g)(g) (2(2,,33,,44,,55,,77,,88,,9)9) ()()

Page 13: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1313

In-place Insertion-sortIn-place Insertion-sortInstead of using an Instead of using an external data structure, external data structure, we can implement we can implement selection-sort and selection-sort and insertion-sort in-placeinsertion-sort in-placeA portion of the input A portion of the input sequence itself serves as sequence itself serves as the priority queuethe priority queueFor in-place insertion-sortFor in-place insertion-sort– We keep sorted the We keep sorted the

initial portion of the initial portion of the sequencesequence

– We can use We can use swapsswaps instead of modifying the instead of modifying the sequencesequence

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

Page 14: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1414

HeapsHeapsA heap is a binary tree A heap is a binary tree storing keys at its nodes storing keys at its nodes and satisfying the and satisfying the following properties:following properties:– Heap-Order:Heap-Order: for every for every

internal node v other than internal node v other than the root,the root,keykey((vv)) keykey((parentparent((vv))))

– Complete Binary Tree:Complete Binary Tree: let let hh be the height of the heapbe the height of the heap

for for i i 0, … , 0, … , h h 1,1, there there are are 22ii nodes of depth nodes of depth iiat depth at depth hh 1 1, the internal , the internal nodes are to the left of the nodes are to the left of the external nodesexternal nodes

2

65

79

The last node of a The last node of a heap is the rightmost heap is the rightmost node of depth node of depth hh

last node

Page 15: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1515

Height of a HeapHeight of a HeapTheorem:Theorem: A heap storing A heap storing nn keys has height keys has height OO(log (log nn))

Proof: (we apply the complete binary tree property)Proof: (we apply the complete binary tree property)– Let Let hh be the height of a heap storing be the height of a heap storing n n keyskeys– Since there are Since there are 22ii keys at depth keys at depth ii 0, … , 0, … , h h 1 1 and at least and at least

one key at depth one key at depth hh, we have , we have nn 1 1 2 2 4 4 … … 2 2hh1 1 11

– Thus, Thus, nn 22hh , i.e., , i.e., hh log log nn

1

2

2h1

1

keys

0

1

h1

h

depth

Page 16: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1616

Heaps and Priority QueuesHeaps and Priority QueuesWe can use a heap to implement a priority queueWe can use a heap to implement a priority queue

We store a (key, element) item at each internal We store a (key, element) item at each internal nodenode

We keep track of the position of the last nodeWe keep track of the position of the last node

For simplicity, we show only the keys in the For simplicity, we show only the keys in the picturespictures

(2, Sue)

(6, Mark)(5, Pat)

(9, Jeff) (7, Anna)

Page 17: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1717

Insertion into a HeapInsertion into a HeapMethod insertItem of Method insertItem of the priority queue ADT the priority queue ADT corresponds to the corresponds to the insertion of a key insertion of a key kk to to the heapthe heap

The insertion algorithm The insertion algorithm consists of three stepsconsists of three steps– Find the insertion node Find the insertion node zz

(the new last node)(the new last node)– Store Store kk at at zz– Restore the heap-order Restore the heap-order

property (discussed property (discussed next)next)

2

65

79

insertion node

2

65

79 1

z

z

Page 18: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1818

UpheapUpheapAfter the insertion of a new key After the insertion of a new key kk, the heap-order property , the heap-order property may be violatedmay be violated

Algorithm upheap restores the heap-order property by Algorithm upheap restores the heap-order property by swapping swapping kk along an upward path from the insertion node along an upward path from the insertion node

Upheap terminates when the key Upheap terminates when the key kk reaches the root or a reaches the root or a node whose parent has a key smaller than or equal to node whose parent has a key smaller than or equal to kk

Since a heap has height Since a heap has height OO(log (log nn)), upheap runs in , upheap runs in OO(log (log nn)) timetime

2

15

79 6z

1

25

79 6z

Page 19: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 1919

Removal from a HeapRemoval from a HeapMethod removeMin of Method removeMin of the priority queue ADT the priority queue ADT corresponds to the corresponds to the removal of the root key removal of the root key from the heapfrom the heapThe removal algorithm The removal algorithm consists of three stepsconsists of three steps– Replace the root key Replace the root key

with the key of the last with the key of the last node node ww

– Remove Remove ww – Restore the heap-order Restore the heap-order

property (discussed property (discussed next)next)

2

65

79

last node

w

7

65

9w

new last node

Page 20: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2020

DownheapDownheapAfter replacing the root key with the key After replacing the root key with the key kk of the last node, the of the last node, the heap-order property may be violatedheap-order property may be violated

Algorithm downheap restores the heap-order property by Algorithm downheap restores the heap-order property by swapping key swapping key kk along a downward path from the root along a downward path from the root

Upheap terminates when key Upheap terminates when key kk reaches a leaf or a node whose reaches a leaf or a node whose children have keys greater than or equal to children have keys greater than or equal to kk

Since a heap has height Since a heap has height OO(log (log nn)), downheap runs in , downheap runs in OO(log (log nn)) time time

7

65

9w

5

67

9w

Page 21: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2121

Updating the Last NodeUpdating the Last NodeThe insertion node can be found by traversing a path of The insertion node can be found by traversing a path of OO(log (log nn) ) nodesnodes– Go up until a left child or the root is reachedGo up until a left child or the root is reached– If a left child is reached, go to the right childIf a left child is reached, go to the right child– Go down left until a leaf is reachedGo down left until a leaf is reached

Similar algorithm for updating the last node after a Similar algorithm for updating the last node after a removalremoval

Page 22: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2222

Heap-Sort Heap-Sort

Consider a priority Consider a priority queue with queue with nn items items implemented by implemented by means of a heapmeans of a heap– the space used is the space used is OO((nn))

– methods methods insertinsert and and removeMinremoveMin take take OO(log (log nn) ) timetime

– methods methods sizesize, , isEmptyisEmpty, and , and minmin take take time time OO(1) (1) timetime

Using a heap-based Using a heap-based priority queue, we can priority queue, we can sort a sequence of sort a sequence of nn elements in elements in OO((nn log log nn) ) timetimeThe resulting The resulting algorithm is called algorithm is called heap-sortheap-sortHeap-sort is much Heap-sort is much faster than quadratic faster than quadratic sorting algorithms, sorting algorithms, such as insertion-sort such as insertion-sort and selection-sortand selection-sort

Page 23: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2323

Vector-based Heap Vector-based Heap ImplementationImplementation

We can represent a heap with We can represent a heap with nn keys by means of a vector keys by means of a vector of length of length n n 1 1For the node at rank For the node at rank ii– the left child is at rank the left child is at rank 22ii– the right child is at rank the right child is at rank 22i i 1 1

Links between nodes are not Links between nodes are not explicitly storedexplicitly storedThe cell of at rank The cell of at rank 00 is not is not usedusedOperation insert corresponds Operation insert corresponds to inserting at rank to inserting at rank n n 1 1Operation removeMin Operation removeMin corresponds to removing at corresponds to removing at rank 1rank 1Yields in-place heap-sortYields in-place heap-sort

2

65

79

2 5 6 9 7

1 2 3 4 50

Page 24: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2424

Merging Two HeapsMerging Two Heaps

We are given two We are given two heaps and a key heaps and a key kk

We create a new We create a new heap with the root heap with the root node storing node storing kk and and with the two heaps with the two heaps as subtreesas subtrees

We perform We perform downheap to downheap to restore the heap-restore the heap-order property order property

7

3

58

2

64

3

58

2

64

2

3

58

4

67

Page 25: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2525

We can construct a We can construct a heap storing heap storing nn given given keys in using a bottom-keys in using a bottom-up construction with up construction with log log nn phases phases

In phase In phase ii, pairs of , pairs of heaps with heaps with 22i i 11 keys are keys are merged into heaps with merged into heaps with 22ii1111 keys keys

Bottom-up Heap ConstructionBottom-up Heap Construction

2i 1 2i 1

2i11

Page 26: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2626

ExampleExample

1516 124 76 2023

25

1516

5

124

11

76

27

2023

Page 27: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2727

Example (contd.)Example (contd.)

25

1516

5

124

11

96

27

2023

15

2516

4

125

6

911

23

2027

Page 28: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2828

Example (contd.)Example (contd.)

7

15

2516

4

125

8

6

911

23

2027

4

15

2516

5

127

6

8

911

23

2027

Page 29: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 2929

Example (end)Example (end)

4

15

2516

5

127

10

6

8

911

23

2027

5

15

2516

7

1210

4

6

8

911

23

2027

Page 30: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 3030

AnalysisAnalysisWe visualize the worst-case time of a downheap with a We visualize the worst-case time of a downheap with a proxy path that goes first right and then repeatedly goes proxy path that goes first right and then repeatedly goes left until the bottom of the heap (this path may differ from left until the bottom of the heap (this path may differ from the actual downheap path)the actual downheap path)

Since each node is traversed by at most two proxy paths, Since each node is traversed by at most two proxy paths, the total number of nodes of the proxy paths is the total number of nodes of the proxy paths is OO((nn))

Thus, bottom-up heap construction runs in Thus, bottom-up heap construction runs in OO((nn) ) time time

Bottom-up heap construction is faster than Bottom-up heap construction is faster than nn successive successive insertions and speeds up the first phase of heap-sortinsertions and speeds up the first phase of heap-sort

Page 31: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

CSC311: Data StructuresCSC311: Data Structures 3131

Adaptable Adaptable Priority QueuesPriority Queues

3 a

5 g 4 e

Page 32: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 3232

Motivating ExampleMotivating ExampleSuppose we have an online trading system where Suppose we have an online trading system where orders to purchase and sell a given stock are orders to purchase and sell a given stock are stored in two priority queues (one for sell orders stored in two priority queues (one for sell orders and one for buy orders) as (p,s) entries:and one for buy orders) as (p,s) entries:– The key, p, of an order is the priceThe key, p, of an order is the price– The value, s, for an entry is the number of sharesThe value, s, for an entry is the number of shares– A buy order (p, s) is executed when a sell order (p’, s’) A buy order (p, s) is executed when a sell order (p’, s’)

with price p’with price p’<<p is added (the execution is complete if p is added (the execution is complete if s’s’>>s)s)

– A sell order (p, s) is executed when a buy order (p’, s’) A sell order (p, s) is executed when a buy order (p’, s’) with price p’with price p’>>p is added (the execution is complete if p is added (the execution is complete if s’s’>>s)s)

What if someone wishes to cancel their order What if someone wishes to cancel their order before it executes?before it executes?What if someone wishes to update the price or What if someone wishes to update the price or number of shares for their order?number of shares for their order?

Page 33: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 3333

Methods of the Adaptable Methods of the Adaptable Priority Queue ADTPriority Queue ADT

removeremove((ee): Remove from ): Remove from P P and and return entry return entry ee..replaceKeyreplaceKey((e,ke,k): Replace with ): Replace with k k and and return the key of entry return the key of entry e e of of P;P; an an error condition occurs if error condition occurs if k k is invalid is invalid (that is, (that is, k k cannot be compared with cannot be compared with other keys).other keys).replaceValuereplaceValue((e,xe,x): Replace with ): Replace with x x and return the value of entry and return the value of entry e e of of PP..

Page 34: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 3434

ExampleExampleOperationOperation OutputOutput PPinsertinsert((55,,AA)) ee11 ((55,,AA))insertinsert((33,,BB)) ee22 ((33,,BB)),,((55,,AA))insertinsert((77,,CC)) ee33 ((33,,BB)),,((55,,AA)),,((77,,CC))minmin()() ee22 ((33,,BB)),,((55,,AA)),,((77,,CC))keykey((ee22)) 33 ((33,,BB)),,((55,,AA)),,((77,,CC))removeremove((ee11)) ee11 ((33,,BB)),,((77,,CC))replaceKeyreplaceKey((ee22,,99)) 33 ((77,,CC)),,((99,,BB))replaceValuereplaceValue((ee33,,DD)) CC ((77,,DD)),,((99,,BB))removeremove((ee22)) ee22 ((77,,DD))

Page 35: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 3535

Locating EntriesLocating Entries

In order to implement the operations In order to implement the operations remove(k), replaceKey(e),remove(k), replaceKey(e), and and replaceValue(k),replaceValue(k), we need fast ways we need fast ways of locating an entry of locating an entry ee in a priority in a priority queue.queue.

We can always just search the entire We can always just search the entire data structure to find an entry data structure to find an entry ee, but , but there are better ways for locating there are better ways for locating entries.entries.

Page 36: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 3636

Location-Aware EntriesLocation-Aware EntriesA locator-aware entry identifies and A locator-aware entry identifies and tracks the location of its (key, value) tracks the location of its (key, value) object within a data structureobject within a data structureIntuitive notion:Intuitive notion:– Coat claim checkCoat claim check– Valet claim ticketValet claim ticket– Reservation numberReservation numberMain idea:Main idea:– Since entries are created and returned from Since entries are created and returned from

the data structure itself, it can return the data structure itself, it can return location-aware entries, thereby making location-aware entries, thereby making future updates easierfuture updates easier

Page 37: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 3737

List ImplementationList ImplementationA location-aware list entry is an object storingA location-aware list entry is an object storing– keykey– valuevalue– position (or rank) of the item in the listposition (or rank) of the item in the list

In turn, the position (or array cell) stores the entryIn turn, the position (or array cell) stores the entryBack pointers (or ranks) are updated during swapsBack pointers (or ranks) are updated during swaps

trailerheader nodes/positions

entries

2 c 4 c 5 c 8 c

Page 38: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 3838

Heap ImplementationHeap ImplementationA location-aware A location-aware heap entry is an heap entry is an object storingobject storing– keykey– valuevalue– position of the position of the

entry in the entry in the underlying heapunderlying heap

In turn, each In turn, each heap position heap position stores an entrystores an entryBack pointers are Back pointers are updated during updated during entry swapsentry swaps

4 a

2 d

6 b

8 g 5 e 9 c

Page 39: CSC311: Data Structures 1 Chapter 8: Priority Queues Objectives: Priority Queue ADT Comparator design pattern Heaps Priority Queue Implementation with

Priority QueuesPriority Queues CSC311: Data StructuresCSC311: Data Structures 3939

PerformancePerformanceUsing location-aware entries we can achieve Using location-aware entries we can achieve the following running times (times better the following running times (times better than those achievable without location-than those achievable without location-aware entries are highlighted in aware entries are highlighted in redred):):

MethodMethod Unsorted ListUnsorted List Sorted ListSorted List HeapHeapsize, isEmptysize, isEmpty OO(1)(1) OO(1)(1) OO(1)(1)insertinsert OO(1)(1) OO((nn)) OO(log (log nn))minmin OO((nn)) OO(1)(1) OO(1)(1)removeMinremoveMin OO((nn)) OO(1)(1) OO(log (log nn))removeremove OO(1)(1) OO(1)(1) OO(log (log nn))replaceKeyreplaceKey OO(1)(1) OO((nn)) OO(log (log nn))replaceValuereplaceValue OO(1)(1) OO(1)(1) OO(1)(1)