1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences,...

37
1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues, ..) Heaps… use of binary trees to efficiently implement priority queue L-21-22-23-24 L-21 [Portions © Goodrich & Tomassia] mputer Science Department ~ University College Dublin ~ www.cs.ucd.ie/staff/nick ~ © Nicholas Kushmerick 2001
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    0

Transcript of 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences,...

Page 1: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

1

Priority Queues & Heaps

• Priority queues… return to ‘linear’ data structures (sequences, queues, ..)

• Heaps… use of binary trees to efficiently implement priority queue

L-21-22-23-24

L-21

[Portions © Goodrich & Tomassia]

COMP-2001 ~ Computer Science Department ~ University College Dublin ~ www.cs.ucd.ie/staff/nick ~ © Nicholas Kushmerick 2001

Page 2: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

2

Example 1• Busy airport, lots of planes arriving, …

Which airplane should be allowed to land next?• Naïve idea: use a Queue: schedule landings by order in

which plains request permission

• But what about…distance from runway, amount of fuel, emergency situations, scheduled arrival time, freight/people, private/commercial, Air Force One, …

• Better idea: combine combine all such details into one common “priority”, and then schedule landings by priority

Page 3: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

3

Example 2 -- Stock Trading

• Investors place orders consisting of two items(action, price)

where:action is either buy or sell, andprice is the worst price you are willing to pay

for the purchase or get from your sale

• At equilibrium, all willing buyers & sellers have conducted transactions, so all remaining buy orders have prices lower than the sell orders

• But… how do you quickly match buyers and sellers to achieve this equilibrium?

Page 4: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

4

More Stock Trading•The “level 1” trading algorithm:

• when a new BUY order comes in at price B

• Let S be the highest-priced stored SELL order

• If B>S then perform the trade else store the buy order for subsequent matching

•When a new SELL order comes in at price S

• Let B be the lowest-priced stored BUY order

• If B<S then perform the trade else store the sell order for subsequent matching

• When trade occurs, then we need to remove relevant orders from stored lists

A

B

D

F

C

E

Page 5: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

5Priority Queue: Data Structures for the Stock Market• For each security, maintain two data structures: one for the

buy orders (bids), and the other for the sell orders (asks)• Operations that need to be supported:

purpose BUY structure SELL structureplace order insert(price) insert(price)is trade possible? min() max()perform trade removeMin()

removeMax()

•Such data structures are called priority queues.

•All operations must be fast! (The NASDAQ priority queues support an average daily trading volume of 1B shares)

A

B

D

FC

E

Page 6: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

6Keys and Total Order Relations• A Priority Queue ranks its elements by an associated

key that can be ordered by some total order relation “”• Keys: Every element has its own “key”

Key encodes whatever information is relevantto its element’s priority

Keys are not necessarily uniqueeg - key = price key = (fuel, distance, arrive-time, emergency)

• Keys are ordered by a Total Order relation “” Reflexive: k k , for every key k

Antisymetric: if k1 k2 and k2 k1, then k1 = k2

Transitive: if k1 k2 and k2 k3, then k1 k3If these properties don’t hold, then contradictions may arise:

eg, “flight FR#1764 should land first” and “flight EI#774 should land first” -- ooops…

Page 7: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

7

A key & relation for airline landings• priority(flight) =

10000 * (if emergency then 1 else 0) +10 / (amount of fuel left) +4 / (distance to airport) +3 * (minutes after scheduled arrival time)

• Larger values --> more urgent• Now compare priority(flight1) and priority(flight2) using

the ordinary numerical comparison operator

• Note: This example demonstrates that “” above is just a convention indicating “some total order relation” -- in this case ‘greater than’ is actually what we want rather than ‘less than’

• Usually… keys = numbers; lower value = higher priority

Page 8: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

8

The Priority Queue ADT• A priority queue P supports the following methods:-size() Return the number of elements in P-isEmpty() Test whether P is empty-insertItem(k,e) Insert a new element e with key k into P-minElement() Return (but don’t remove) an element of P

with smallest key; error occurs if P empty.-minKey() Return the smallest key in P; error

occurs ifP is empty

-removeMin() Remove from P and return an elementwith the smallest key; an error occursif P is empty.

No “Position” objects in this ADT since (like stack & queue)Priority Queues permit only very restricted access to elements

Page 9: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

9Comparators• The most general and reusable form of a priority queue makes use

of comparator objects.• When create a PriorityQueue implementation class, supply the

comparator object to be used. When the priority queue needs to compare two keys, it uses the comparator it was given to do the comparison.

• Comparator objects are external to the keys: might want different comparator for exact same keys -- example,

key = point in 2 dimensionsshould we compare two points by x coordinate,y coordinate, distance from origin, angle of linefrom point to origin, …

• Comparators encapsulate the specific comparison from the key itself. Thus a priority queue can be general enough to store any object and use any kind of comparison

• The comparator ADT includes:-isLessThanOrEqualTo(a,b)

Page 10: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

10

PQ implementation #1: Implementation with an Unsorted Sequence

• Let’s try to implement a priority queue with an unsorted sequence S.

• The elements of S are a composition of two elements, k, the key, and e, the element.

• We can implement insertItem() by using insertLast() on the sequence. This takes O(1) time.

Page 11: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

11Implementation w/ unsorted seq, continued

• A few more details… see Practical 9 code

• “Item” class = (key,element) pairThese are the objects that are stored in the sequence. (Note that as specified by the ADT, these are never exported outside the Priority queue.)

• For simplicity, we’ll use java.util.Vector rather than Sequence

• IntegerComparator provides “” for Integers

Page 12: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

12Unsorted Sequence Implementaton - code detailsclass UnsortedVectorPriorityQueue

extends AbstractVectorPriorityQueue {

public void insert(Object key, Object element) {v.addElement(new Item(key,element));

}

protected Item minItem() {if (isEmpty()) return null;Iterator it = v.iterator();Item result = (Item) it.next();while (it.hasNext()) { Item item = (Item) it.next(); if (comp.isLessThanOrEqualTo(item.key,result.key)) {

result = item; }}return result;

}

Page 13: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

13

Implementation with an Unsorted Sequence (contd.)• Because we always insert at the end, regardless of the key value, our sequence is not

ordered.• Thus, for methods such as minElement(), minKey(), and removeMin(), we need to look

at all the elements of S. The worst case time complexity for these methods is O(n).

•Performance summary

Ouch!

Page 14: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

14

L22

• This week– start Chapter 7

– Finish practical 9

– Start practical 10

• Next week– Finish Chapter 7

– Finish practical 10

Page 15: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

15

PQ implementation #2: Sorted Sequence• Another implementation uses a sequence S, and we do some

extra work to ensure it is sorted by increasing keys• minElement(), minKey(), and removeMin() take O(1) time

• However, to implement insertItem(), we must now scan through the entire sequence in the worst case. Thus insertItem() runs in O(n) time

Ouch!

Page 16: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

16Code fragment - Sorted Sequence Impl. class SortedVectorPriorityQueue extends AbstractVectorPriorityQueue { public void insert(Object key, Object element) {

Item newitem = new Item(key, element);if (isEmpty()) { // special case -- empty PQ v.addElement(newitem);} else if (comp.isLessThanOrEqualTo(((Item)v.lastElement()).key, key)) { // special case -- new key has lower priority than current keys v.insertElementAt(newitem, v.size());} else { // normal case... for (int i=0; i<v.size(); i++) {

Item item = (Item) v.elementAt(i);if (comp.isLessThanOrEqualTo(key,item.key)) { v.insertElementAt(newitem, i); return;}

} // we'll never get here -- handled in second special case above throw new RuntimeException("SortedVectorPriorityQueue.insert: Yikes!");}

} protected Item minItem() {

return isEmpty() ? null : (Item) v.elementAt(0); }

Page 17: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

17

Heaps• Review…• So far… priority queues, keys, comparators,

terrible implementation based on unsorted Sequence

better implementation based on sorted Sequence

• Final 3 days of COMP-2001… a great implementation based on heaps (a certain kind of binary tree)

• Practical 9: compare all three implementations

• This week - read Chapter 7- complete P8- start P9

• Next week - finish Chapter 7- complete P9

Page 18: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

18

HeapsA heap is a binary tree that stores keys (or key-element pairs) at its internal nodes and that satisfies two additional properties:

•Order Property: key(parent) key(child)•Structural “Completeness” Property: all levels are full, except the bottom, which is “left-filled”

4

6

207

811

5

9

1214

15

2516

Rule 1. Level i has 2i nodes,for 0i<h

Rule 2. At level h-1, all leafsare to right of all internalnodes

level 0

level 1

level h-1

level h

Page 19: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

19

Examples that are NOT heaps

Page 20: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

20

A useful fact• A heap storing N keys has height

h = log2(N+1) x = smallest integer greater than

x

4

6

207

811

5

9

1214

15

2516

h = log2(13+1) = 3.80775... = 4

Page 21: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

21

Using a heap to implement a PQ

heap

4

6

207

811

5

9

1214

15

2516

an instance of the HeapPriorityQueue class

BinaryTree objectlast

comp

For simplicity… only keys are shown (elements not displayed)keys are integers ordered by the ordinary “” relation

(note: we don’t actuallyput anything in leaves!)

Page 22: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

22

Heap “insert” operation (#1)

• insert(6,e)

last

Page 23: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

23Insert #2

to restore “order” property

to ensure “completeness” property

Page 24: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

24Upheap - [insert #3]• Swap parent-child keys out of order

Page 25: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

25Upheap Continues [insert #4]

Page 26: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

26End of Upheap [insert #5]

Upheap terminates when new key is greater than the key of its parent, or the top of the heap is reached.Note that after Upheap the heap is again valid!

Complexity Analysis:cost(insert) = (total#swaps) (h-1) = log2(N+1) = O(log n)

not quite as good as UnsortedSeqPQ [O(1)]but much better than SortedSeqPQ [O(n)]

Page 27: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

27

Adjusting “last” [insert #6]• One more detail -- need to update “last” reference

• From old ‘last’, find sibling to the left, then a cousin to the left, then a second cousin to the left, …

• In the worst case, need to traverse tree all the way up from level h-1 to root and then all the way back down to level h-1

• Therefore… maintaining “last” consumes O(log n) additional operations, so insert is really O(log n) after all…

last

Page 28: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

28Heap “removeMin” operation [#1]

• The removal of the top key leaves a hole

• We need to fix the heap:– First, replace the hole

with the last key in the heap

– Then, begin Downheap

3

Page 29: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

29Downheap [removeMin #2]

• Downheap compares the parent with the smallest child. If the child is smaller, it switches the two.

Page 30: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

30

Downheap Continues [removeMin #3]

Page 31: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

31Downheap Continues [removeMin #4]

Page 32: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

32End of Downheap

• Downheap terminates when the key is greater than the keys of both its children, or the bottom of the heap is reached.Note that after Downheap the heap is again valid!

Complexity Analysis:cost(rMin) = (total#swaps) (h-1) = log2(N+1) = O(log n)

not quite as good as SortedSeqPQ [O(1)]but much better than UnsortedSeqPQ [O(n)]

Page 33: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

33

L23

• This week … – Finish chapter 7

– Finish practical 9

Page 34: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

34

PQ implementations - comparison

• Operation Heap SortedSeq UnsortedSeq

• Size, isEmpty O(1) O(1) O(1)• minElement, O(1) O(1) O(n)

minKey• insert O(log n) O(n) O(1)• removeMin O(log n) O(1) O(n)• ------------------------------------------------------• mixture of all O(log n) O(n) O(n)

operations

ouch

ouch

ouch

Page 35: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

35Implementation of a Heappublic class HeapPriorityQueue implements PriorityQueue {

BinaryTree T;

Position last;

Comparator comparator;

...

}

Page 36: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

36Vector Based Implementation on the Binary Tree• Remember the vector-based tree implementation (L19)….• Observation: updates to tree occur only at the root and “last” elements• A heap can be represented by a vector, where the node at rank i has -left child at rank 2i and -right child at rank 2i + 1

• Insertion into the heap correspond to insertLast on the vector• Removes from the tree corresponds to removeLast on the vector

• The leaves do no need to be explicitly stored

vector 1 2 3 4 5 6 7 8 9 10 11 12 13

last

Page 37: 1 Priority Queues & Heaps Priority queues… return to ‘linear’ data structures (sequences, queues,..) Heaps… use of binary trees to efficiently implement.

37

Vector-based implementation, cont• So… tree simplifies insertion/deletion at the ‘last’ element

• It also simplifies updating ‘last’ after these operations…

• Nothing to do: The correct “last” value of the heap is always equal to the last vector element!