Presentation is loading. Please wait.

Presentation is loading. Please wait.

2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.

Similar presentations


Presentation on theme: "2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas."— Presentation transcript:

1 2015-T2 Lecture 28 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

2 2 RECAP-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)

3 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

4 4 Implementing 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]

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

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

7 7 Partially 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 important Cat 19 Cat 19 Eel 26 Eel 26 Gnu 13 Gnu 13 Ant 9 Ant 9 Bee 35 Bee 35 Eel 26 Eel 26 Cat 19 Cat 19 Dog 14 Dog 14 Fox 3 Fox 3 Ant 9 Ant 9 Hen 23 Hen 23 Gnu 13 Gnu 13 Jay 2 Jay 2 Jay 2 Jay 2 Keep highest priority element at the root

8 8 Partially Ordered Tree: Adding  Easier to add and remove because the order is not complete  Add (draft)  start at top and navigate downwards to the right level  “bubble up” to correct position by swapping Bee 35 Bee 35 Eel 26 Eel 26 Cat 19 Cat 19 Dog 14 Dog 14 Fox 3 Fox 3 Ant 9 Ant 9 Hen 23 Hen 23 Gnu 13 Gnu 13 Jay 2 Jay 2 Fly 1 Fly 1

9 9 Partially 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 Bee 35 Bee 35 Eel 26 Eel 26 Cat 19 Cat 19 Dog 14 Dog 14 Fox 3 Fox 3 Ant 9 Ant 9 Hen 23 Hen 23 Gnu 13 Gnu 13 Jay 2 Jay 2 Kea 24 Kea 24

10 10 Partially 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 become unbalanced! Bee 35 Bee 35 Eel 26 Eel 26 Cat 19 Cat 19 Dog 14 Dog 14 Fox 3 Fox 3 Ant 9 Ant 9 Hen 23 Hen 23 Gnu 13 Gnu 13 Jay 2 Jay 2

11 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! Eel 26 Eel 26 Kea 24 Kea 24 Dog 14 Dog 14 Cat 19 Cat 19 Ant 9 Ant 9 Hen 25 Hen 25 Gnu 13 Gnu 13 Jay 2 Jay 2 Fox 3 Fox 3 Bee 35 Bee 35

12 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! Hen 23 Hen 23 Kea 24 Kea 24 Dog 14 Dog 14 Cat 19 Cat 19 Ant 9 Ant 9 Fox 3 Fox 3 Gnu 13 Gnu 13 Jay 2 Jay 2 Eel 26 Eel 26

13 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] Hen 23 Hen 23 Kea 24 Kea 24 Dog 14 Dog 14 Cat 19 Cat 19 Ant 9 Ant 9 Fox 3 Fox 3 Gnu 13 Gnu 13 Jay 2 Jay 2 Eel 26 Eel 26

14 14 Heap  A complete, partially ordered, binary tree complete = every level full, except bottom, where nodes are to the left  Implemented in an array using breadth-first order Bee 35 Eel 26 Kea 19 Dog 14 Fox 7 Ant 9 Hen 23 Gnu 13 Jay 2 Cat 4 Bee 35 Eel 26 Kea 19 Dog 14 Fox 7 Ant 9 Hen 23 Gnu 13 Jay 2 Cat 4 0137894265

15 15 Heap  Bottom right node is last element used  We can compute the index of parent and children of a node:  the children of nodei are at(2i+1) and (2i+2)  the parent of nodei is at (i-1)/2  wow: no gaps! Bee 35 Eel 26 Kea 19 Dog 14 Fox 7 Ant 9 Hen 23 Gnu 13 Jay 2 Cat 4 0137894265 Bee 35 Eel 26 Kea 19 Dog 14 Fox 7 Ant 9 Hen 23 Gnu 13 Jay 2 Cat 4


Download ppt "2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas."

Similar presentations


Ads by Google