2014-t2 lecture 29 school of engineering and computer science, victoria university of wellington ...

18
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae and Thomas Kuehne, VUW COMP 103 Marcus Frean Priority Queues and Heaps

Upload: elaine-houston

Post on 17-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

3 Queues and Priority Queues  Queues: Oldest out first  Priority Queues: Best out first  Emergency room, 111 calls, job scheduling in factory, etc...  Operating system process scheduling  Graph/network algorithms: route planning, find shortest/cheapest path BobJackJimIan Sometimes a lower number means higher priority Ian 3 Bob 5 Jack 1 Jim 2 Ian 5 Jack

TRANSCRIPT

Page 1: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria

University of Wellington

Marcus Frean, Lindsay Groves, Peter Andreae and Thomas Kuehne, VUW

COM

P 10

3

Marcus Frean

Priority Queuesand Heaps

Page 2: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

2RECAP-TODAY

RECAP Linked Structures

linked list idea nice way to implement Stacks, or Queues

Tree Structures (in particular Binary Search Trees (BST)) BSTs idea nice way to implement a Set, Bag, or Map in-order traversals TreeSort

TODAY Priority Queue = variation on Queue – “best” first Heap idea

nice way to implement Priority Queue HeapSort

Reading: Chapter 17 (section 17.4 - 17.5)

Page 3: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

3Queues and Priority Queues Queues: Oldest out first

Priority Queues: Best out first

Emergency room, 111 calls, job scheduling in factory, etc... Operating system process scheduling Graph/network algorithms: route planning, find

shortest/cheapest path

Bob Jack Jim Ian

Sometimes a lower number means higher

priority

Ian

3Bob

5Jack

1Jim

2Ian

5Jack

Page 4: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

4Implementing Priority Queues Unsorted list (array or linked list):

Fast to enqueue (“offer”): O(1) Slow to dequeue (“poll”): O(n)

need to search for highest priority item

Sorted list (array or linked list): Fast to dequeue (“poll”): O(1) Slow to enqueue (“offer”): O(n)

have to search for insertion point

Binary Search Tree Fast to enqueue & dequeue ( O(log n)) But, not cheap to keep balanced

[if balanced]

Page 5: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

5

Implementing Priority Queues: Ideas?

A BST is always fully sorted supports ascending order through in-order

traversal

Do we need this for Priority Queues? what if we enqueue 1000 elements

but only need the first three? can we exploit that we are always just

interested in a maximum element? is there a “lazy” sorting strategy?

Page 6: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

6

Implementing Priority Queues: POT idea

Abandon idea of total order effort may never pay off (almost) no need to worry about lower

structure as long as we keep the priority item at the top

Only aim for partial order Partially Ordered Tree

Fast to enqueue (“offer”): O(log n) Fast to dequeue (“poll”): O(log n) Easy to keep balanced Fast to construct from unordered list!

Page 7: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

7Partially Ordered Trees

Binary Search Tree Binary tree All in left subtree < parent,

All in right subtree ≥ parent

Partially Ordered Tree

Binary tree children ≤ parent, Order of children not

importantCat19

Eel26

Gnu13

Fox3

Dog14

Bee35

Hen23

Ant9

Bee35

Eel26

Cat19

Dog14

Fox3

Ant9

Hen23

Gnu13

Jay2

Jay2

Keep highest priority element

at the root

Page 8: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

8Partially Ordered Tree: Adding

Easier to add and remove because the order is not complete

Add (draft) start at top and navigate

downwards to the rightlevel

“bubble up” to correctposition by swapping

Bee35

Eel26

Cat19

Dog14

Fox3

Ant9

Hen23

Gnu13

Jay2

Fly1

Page 9: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

9Partially Ordered Tree: Adding

Easier to add and remove because the order is not complete

Add insert next to

bottom-rightmost “bubble up” to correct

position by swapping

Bee35

Eel26

Cat19

Dog14

Fox3

Ant9

Hen23

Gnu13

Jay2

Kea24

Page 10: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

10Partially Ordered Tree: Removing

Easier to add and remove because the order is not complete

Remove (draft) “pull up” largest child

and recurse But:

tree may becomeunbalanced!

Bee35

Eel26

Cat19

Dog14

Fox3

Ant9

Hen23

Gnu13

Jay2

Page 11: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

11

Partially Ordered Tree: Removing II

Easier to add and remove because the order is not complete

Remove (better) replace root by

bottom-rightmost node “sink down” to

correct position by swapping

keeps tree balanced – and complete!

Eel26

Kea24

Dog14

Cat19

Ant9

Hen25

Gnu13

Jay2

Fox3

Bee35

Page 12: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

12

Partially Ordered Tree: Removing II

Easier to add and remove because the order is not complete

Remove (better) replace root by

bottom-rightmost node “sink down” to

correct position by swapping

keeps tree balanced – and complete!

Hen23

Kea24

Dog14

Cat19

Ant9

Fox3

Gnu13

Jay2

Eel26

Page 13: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

13

Partially Ordered Tree: Removing II

Easier to add and remove because the order is not complete

Support required for locating bottom-rightmost node

[add & remove (II)] child parent

[“bubble up” for add] parent child

[“sink down” for remove]

Hen23

Kea24

Dog14

Cat19

Ant9

Fox3

Gnu13

Jay2

Eel26

invariant repairers!

Page 14: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

14Heap

A complete, partially ordered, binary treecomplete = every level full, except bottom, where nodes are to

the left Implemented in an array using breadth-first

order

Bee35

Eel26

Kea19

Dog14

Fox7

Ant9

Hen23

Gnu13

Jay2

Cat4

Bee35

Eel26

Kea19

Dog14

Fox7

Ant9

Hen23

Gnu13

Jay2

Cat4

0 1 3 7 8 942 65

Page 15: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

15Heap

Bottom right node is last element used

We can compute the index of parent and children of a node: the children of node i are at (2i+1)

and (2i+2) the parent of node i is at (i-1)/2

Gasp! There are no gaps!

Bee35

Eel26

Kea19

Dog14

Fox7

Ant9

Hen23

Gnu13

Jay2

Cat4

0 1 3 7 8 942 65Bee35

Eel26

Kea19

Dog14

Fox7

Ant9

Hen23

Gnu13

Jay2

Cat4

Page 16: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

16Heap: add

Insert at bottom of tree and bubble up: Put new item at end: 10 Compare with parent: p = (10-1)/2 = 4 ⇒ Fox/7

If larger than parent swap Compare with parent: p = (4-1)/2 = 1⇒ Kea/19

If larger than parent swap Compare with parent: p = (1-1)/2 = 0⇒ Bee/35

Bee35

Eel26

Kea19

Dog14

Fox7

Ant9

Hen23

Gnu13

Jay2

Cat4

0 1 3 7 8 942 65 10 11 12

10

Pig21

11

Page 17: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

17Heap: remove

Remove item at 0: Move last item to 0 Find largest child c1 = 2 0+1 = 1, c2 = 2 0+2

= 2 if largest child is larger swap

Find largest child c1 = 2 2+1 = 5, c2 = 2 2+2 = 6 if largest child is larger swap

Find largest child c1 = 2 5+1 = 11 : No such child

Bee35

Eel26

Pig21

Dog14

Kea19

Ant9

Hen23

Gnu13

Jay2

Cat4

0 1 3 7 8 942 65 10 11 12

10

Fox7

11

Page 18: 2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas

18HeapQueue: Analysis

Cost of offer:= cost of “bubble up”= O(log(n))

log(n) comparisons, 2 log(n) assignments

Cost of poll: = cost of “sink down”= O(log(n))

2 log(n) comparisons, 2 log(n) assignments

Conclusion: HeapQueue is always fast!