CSE 373 Priority queue implementation; Intro to heaps

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
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.
Building Java Programs Priority Queues, Huffman Encoding.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
CSE 143 Lecture 22 Priority Queues; Huffman Encoding slides created by Marty Stepp, Hélène Martin, and Daniel Otero
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.
Priority Queue What is that? Implementation with linked list with O(n) behaviour The Heap (O(log(n)) An implementation using an array Your mission …
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CSE 143 Lecture 24 Priority Queues; Huffman Encoding slides created by Marty Stepp and Daniel Otero
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.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
CS 367 Introduction to Data Structures Lecture 8.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues A priority queue is an ADT where:
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
Chapter 11 Heap.
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps Suggested reading from Weiss book: Ch. 21
CSE 143 Lecture 24 Priority Queues; Huffman Encoding
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
October 30th – Priority QUeues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heaps.
ADT Heap data structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Prioritization problems
7/23/2009 Many thanks to David Sun for some of the included slides!
Chapter 8 – Binary Search Tree
Priority Queues.
CSE 373: Data Structures and Algorithms
CMSC 341 Lecture 14 Priority Queues & Heaps
B-Tree Insertions, Intro to Heaps
Binary Tree Application Operations in Heaps
Lecture 24: Priority Queues and Huffman Encoding
Priority Queues.
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
CSE 373: Data Structures and Algorithms
Hassan Khosravi / Geoffrey Tien
Computer Science 2 Heaps.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
CSE 12 – Basic Data Structures
CSC 380: Design and Analysis of Algorithms
Priority Queues CSE 373 Data Structures.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queues & Heaps
Heaps & Multi-way Search Trees
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

CSE 373 Priority queue implementation; Intro to heaps read: Weiss Ch. 6 slides created by Marty Stepp http://www.cs.washington.edu/373/ © University of Washington, all rights reserved.

Prioritization problems print jobs: CSE lab printers constantly accept and complete jobs from all over the building. We want to print faculty jobs before staff before student jobs, and grad students before undergrad, etc. ER scheduling: Scheduling patients for treatment in the ER. A gunshot victim should be treated sooner than a guy with a cold, regardless of arrival time. How do we always choose the most urgent case when new patients continue to arrive? key operations we want: add an element (print job, patient, etc.) get/remove the most "important" or "urgent" element

Priority Queue ADT priority queue: A collection of ordered elements that provides fast access to the minimum (or maximum) element. add adds in order peek returns minimum or "highest priority" value remove removes/returns minimum value isEmpty, clear, size, iterator O(1) pq.add("if"); pq.add("from"); ... "the" "of" "from" "to" "she" "you" "him" "why" "in" "down" "by" "if" pq.remove() "by" priority queue

Unfilled array? Consider using an unfilled array to implement a priority queue. add: Store it in the next available index, as in a list. peek: Loop over elements to find minimum element. remove: Loop over elements to find min. Shift to remove. queue.add(9); queue.add(23); queue.add(8); queue.add(-3); queue.add(49); queue.add(12); queue.remove(); How efficient is add? peek? remove? O(1), O(N), O(N) (peek must loop over the array; remove must shift elements) index 1 2 3 4 5 6 7 8 9 value 23 -3 49 12 size

Sorted array? Consider using a sorted array to implement a priority queue. add: Store it in the proper index to maintain sorted order. peek: Minimum element is in index [0]. remove: Shift elements to remove min from index [0]. queue.add(9); queue.add(23); queue.add(8); queue.add(-3); queue.add(49); queue.add(12); queue.remove(); How efficient is add? peek? remove? O(N), O(1), O(N) (add and remove must shift elements) index 1 2 3 4 5 6 7 8 9 value -3 12 23 49 size

Linked list? Consider using a doubly linked list to implement a priority queue. add: Store it at the end of the linked list. peek: Loop over elements to find minimum element. remove: Loop over elements to find min. Unlink to remove. queue.add(9); queue.add(23); queue.add(8); queue.add(-3); queue.add(49); queue.add(12); queue.remove(); How efficient is add? peek? remove? O(1), O(N), O(N) (peek and remove must loop over the linked list) 9 23 8 -3 49 12 front back

Sorted linked list? Consider using a sorted linked list to implement a priority queue. add: Store it in the proper place to maintain sorted order. peek: Minimum element is at the front. remove: Unlink front element to remove. queue.add(9); queue.add(23); queue.add(8); queue.add(-3); queue.add(49); queue.add(12); queue.remove(); How efficient is add? peek? remove? O(N), O(1), O(1) (add must loop over the linked list to find the proper insertion point) -3 8 9 12 23 49 front back

Binary search tree? Consider using a binary search tree to implement a PQ. add: Store it in the proper BST L/R - ordered spot. peek: Minimum element is at the far left edge of the tree. remove: Unlink far left element to remove. queue.add(9); queue.add(23); queue.add(8); queue.add(-3); queue.add(49); queue.add(12); queue.remove(); How efficient is add? peek? remove? O(log N), O(log N), O(log N)...? (good in theory, but the tree tends to become unbalanced to the right) 9 8 23 -3 12 49

Unbalanced binary tree queue.add(9); queue.add(23); queue.add(8); queue.add(-3); queue.add(49); queue.add(12); queue.remove(); queue.add(16); queue.add(34); queue.remove(); queue.remove(); queue.add(42); queue.add(45); queue.remove(); Simulate these operations. What is the tree's shape? A tree that is unbalanced has a height close to N rather than log N, which breaks the expected runtime of many operations. 49 23 12 16 34 42 45

Heaps heap: A complete binary tree with vertical ordering. complete tree: Every level is full except possibly the lowest level, which must be filled from left to right (i.e., a node may not have any children until all possible siblings exist)

Heap ordering heap ordering: If P ≤ X for every element X with parent P. Parents' values are always smaller than those of their children. Implies that minimum element is always the root (a "min-heap"). variation: "max-heap" stores largest element at root, reverses ordering Is a heap a BST? How are they related?

Which are min-heaps? no 10 20 10 20 80 10 80 20 80 40 60 85 99 40 60 85 99 30 15 50 700 50 700 10 10 20 80 10 40 60 85 99 20 80 20 80 50 700 40 60 99 40 60

Which are max-heaps? no 30 10 20 48 80 21 10 25 30 14 24 9 18 28 11 22 35 30 50 33 30 10 40 10 17 7 3 13

Heap height and runtime The height of a complete tree is always log N. How do we know this for sure? Because of this, if we implement a priority queue using a heap, we can provide the following runtime guarantees: add: O(log N) peek: O(1) remove: O(log N) n-node complete tree of height h: 2h  n  2h+1 – 1 h = log n

The add operation When an element is added to a heap, where should it go? Must insert a new node while maintaining heap properties. queue.add(15); new node 10 15 20 80 40 60 85 99 50 700 65

The add operation When an element is added to a heap, it should be initially placed as the rightmost leaf (to maintain the completeness property). But the heap ordering property becomes broken! 10 10 20 80 20 80 40 60 85 99 40 60 85 99 50 700 65 50 700 65 15

"Bubbling up" a node bubble up: To restore heap ordering, the newly added element is shifted ("bubbled") up the tree until it reaches its proper place. Weiss: "percolate up" by swapping with its parent How many bubble-ups are necessary, at most? 10 10 20 80 15 80 40 60 85 99 40 20 85 99 50 700 65 15 50 700 65 60

Bubble-up exercise Draw the tree state of a min-heap after adding these elements: 6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2 104 42 25 6 19 2 76 50 11 55 88 20

The peek operation A peek on a min-heap is trivial to perform. because of heap properties, minimum element is always the root O(1) runtime Peek on a max-heap would be O(1) as well (return max, not min) 10 20 80 40 60 85 99 50 76 65

The remove operation When an element is removed from a heap, what should we do? The root is the node to remove. How do we alter the tree? queue.remove(); 10 20 80 40 60 85 99 50 700 65

The remove operation When the root is removed from a heap, it should be initially replaced by the rightmost leaf (to maintain completeness). But the heap ordering property becomes broken! 10 65 20 80 20 80 40 60 85 99 40 60 85 99 700 50 65 700 50 65

"Bubbling down" a node bubble down: To restore heap ordering, the new improper root is shifted ("bubbled") down the tree until it reaches its proper place. Weiss: "percolate down" by swapping with its smaller child (why?) How many bubble-down are necessary, at most? 65 20 20 80 40 80 40 60 85 99 50 60 85 99 74 50 74 65

Bubble-down exercise Suppose we have the min-heap shown below. Show the state of the heap tree after remove has been called 3 times, and which elements are returned by the removal. 104 42 25 6 19 2 76 50 11 55 88 20