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

Slides:



Advertisements
Similar presentations
2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Advertisements

Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
CS 171: Introduction to Computer Science II
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Chapter 10 Heaps Anshuman Razdan Div of Computing Studies
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
COMP 103 Hashing 2013-T2 Lecture 28 Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay.
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
data ordered along paths from root to leaf
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Heaps & Priority Queues
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
2015-T2 Lecture 30 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
CS 367 Introduction to Data Structures Lecture 8.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
Heaps (8.3) CSE 2011 Winter May 2018.
Source: Muangsin / Weiss
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Binary Search Trees (I)
More complexity analysis & Binary Search
COMP 103 Binary Search Trees.
Heaps.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
Hassan Khosravi / Geoffrey Tien
Computer Science 2 Heaps.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Priority Queues & Heaps
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Interesting Algorithms for Real World Problems
Presentation transcript:

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

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 )

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 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 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 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 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 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 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 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 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 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 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 invariant repairers!

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

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  Gasp! There are no gaps! Bee 35 Eel 26 Kea 19 Dog 14 Fox 7 Ant 9 Hen 23 Gnu 13 Jay 2 Cat Bee 35 Eel 26 Kea 19 Dog 14 Fox 7 Ant 9 Hen 23 Gnu 13 Jay 2 Cat 4

16 Heap: 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 Bee 35 Eel 26 Kea 19 Dog 14 Fox 7 Ant 9 Hen 23 Gnu 13 Jay 2 Cat Pig 21 11

17 Heap: remove  Remove item at 0:  Move last item to 0  Find largest childc 1 = 2  0+1 = 1, c 2 = 2  0+2 = 2  if largest child is larger  swap  Find largest childc 1 = 2  2+1 = 5, c 2 = 2  2+2 = 6  if largest child is larger  swap  Find largest childc 1 = 2  5+1 = 11 : No such child Bee 35 Eel 26 Pig 21 Dog 14 Kea 19 Ant 9 Hen 23 Gnu 13 Jay 2 Cat Fox 7 11

18 HeapQueue: 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!