Priority Queues and Heaps Suggested reading from Weiss book: Ch. 21

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

PRIORITY QUEUES AND HEAPS Lecture 19 CS2110 Spring
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
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
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
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
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. 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.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
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.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
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.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
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)
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Heaps And Priority Queues
Priority Queues and Heaps
October 30th – Priority QUeues
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Heaps What is a Binary Heap?
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.
Heaps & Priority Queues
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Priority Queue & Heap CSCI 3110 Nan Chen.
Part-D1 Priority Queues
Data Structures & Algorithms Priority Queues & HeapSort
CSE 373: Data Structures and Algorithms
CMSC 341 Lecture 14 Priority Queues & Heaps
CSE 373 Data Structures and Algorithms
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
CSE 373: Data Structures and Algorithms
Hassan Khosravi / Geoffrey Tien
"Teachers open the door, but you must enter by yourself. "
CE 221 Data Structures and Algorithms
Priority Queues & Heaps
CSE 373: Data Structures and Algorithms
Heaps and Priority Queues
CSE 373 Data Structures and Algorithms
CSE 373 Priority queue implementation; Intro to heaps
CSC 380: Design and Analysis of Algorithms
Priority Queues (Heaps)
Priority Queues.
Priority Queues CSE 373 Data Structures.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
CSE 373: Data Structures and Algorithms
Heaps & Multi-way Search Trees
Heaps.
CSE 373: Data Structures and Algorithms
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Priority Queues and Heaps Suggested reading from Weiss book: Ch. 21 TCSS 342 Lecture Notes Priority Queues and Heaps Suggested reading from Weiss book: Ch. 21 These lecture notes are copyright (C) Marty Stepp, 2006. They may not be rehosted, sold, or modified without expressed permission from the author. All rights reserved.

Lecture outline using priority queues examples the priority queue abstract data type (ADT) Java's PriorityQueue class implementing a priority queue using a heap heap properties and examples add peek remove heap sort

Using priority queues

Motivating examples we are a bank and we want to manage a collection of our customers' information we'll be performing many adds and removes we'd like to be able to ask the collection to remove or get the information about the customer with the minimum bank account balance another example: A shared Unix server has a list of print jobs to print. It wants to print them in chronological order, but each print job also has a priority, and higher-priority jobs always print before lower-priority jobs another example: We are writing a ghost AI algorithm for Pac-Man. It needs to search for the best path to find Pac-Man; it will enqueue all possible paths with priorities (based on guesses about which one will succeed), and try them in order

Some bad implementations list: store all customers/jobs in an unordered list, remove min/max one by searching for it problem: expensive to search sorted list: store all in a sorted list, then search it in O(log n) time with binary search problem: expensive to add/remove binary search tree: store in a BST, search it in O(log n) time for the min (leftmost) element problem: tree could be unbalanced on nonrandom input balanced BST problem: in practice, if the program has many adds/removes, it performs slowly on AVL trees and other balanced BSTs problem: removal always occurs from the left side, which

Priority queue ADT priority queue: an collection of ordered elements that provides fast access to the minimum (or maximum) element a mix between a queue and a BST usually implemented using a structure called a heap priority queue operations: add O(1) average, O(log n) worst-case peek - returns the minimum element O(1) remove - removes/returns minimum element O(log n) worst-case isEmpty, clear, size, iterator

Java's PriorityQueue class public class PriorityQueue<E> implements Queue<E> public PriorityQueue<E>() public PriorityQueue<E>(int capacity, Comparator<E> comp) Constructs a priority queue that uses the given comparator to order its elements. public void add(E o) public void clear() public Iterator<E> iterator() public E peek() public E remove()

Implementing a priority queue using a heap

Heap properties heap: a tree with the following two properties: 1. completeness complete tree: every level is full except possibly the lowest level, which must be filled from left to right with no leaves to the right of a missing node (i.e., a node may not have any children until all of its possible siblings exist) Heap shape:

Heap properties 2 2. heap ordering a tree has heap ordering if P < X for every element X with parent P in other words, in heaps, parents' element values are always smaller than those of their children implies that minimum element is always the root is every a heap a BST? Are any heaps BSTs? Are any BSTs heaps? Answer: no, it is not a BST.

Which are min-heaps? wrong! 10 20 10 20 80 10 80 20 80 40 60 85 99 40 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? wrong! 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

Heap height and runtime height of a complete tree is always log n, because it is always balanced this suggests that searches, adds, and removes will have O(log n) worst-case runtime because of this, if we implement a priority queue using a heap, we can provide the O(log n) runtime required for the add and remove operations n-node complete tree of height h: h = log n 2h  n  2h+1 - 1

Adding to a heap when an element is added to a heap, it should be initially placed as the rightmost leaf (to maintain the completeness property) 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

Adding to a heap, cont'd. to restore heap ordering property, the newly added element must be shifted upward ("bubbled up") until it reaches its proper place bubble up (book: "percolate up") by swapping with parent how many bubble-ups could be 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

Adding to a max-heap same operations, but must bubble up larger values to top 16 5 11 3 18 16 18 11 3 5 18 16 11 3 5

Heap practice problem Draw the state of the min-heap tree after adding the following elements to it: 6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2

The peek operation peek on a min-heap is trivial; because of the heap properties, the minimum element is always the root peek is O(1) peek on a max-heap would be O(1) as well, but would return you the maximum element and not the minimum one 10 20 80 40 60 85 99 50 700 65

Removing from a min-heap min-heaps only support remove of the min element (the root) must remove the root while maintaining heap completeness and ordering properties intuitively, the last leaf must disappear to keep it a heap initially, just swap root with last leaf (we'll fix it) 10 65 20 80 20 80 40 60 85 99 40 60 85 99 700 50 65 700 50 65

Removing from heap, cont'd. must fix heap-ordering property; root is out of order shift the root downward ("bubble down") until it's in place swap it with its smaller child each time What happens if we don't always swap with the smaller child? 65 20 20 80 40 80 40 60 85 99 50 60 85 99 700 50 700 65

Heap practice problem Assuming that you have a heap with the following elements to it (from the last question): 6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2 Show the state of the heap after remove() has been executed on it 3 times, and state which elements are returned by the removal.

Turning any input into a heap we can quickly turn any complete tree of comparable elements into a heap with a buildHeap algorithm simply perform a "bubble down" operation on every node that is not a leaf, starting from the rightmost internal node and working back to the root why does this buildHeap operation work? how long does it take to finish? (big-Oh) runtime is actually O(N) 45 6 21 18 14 18 14 60 32 6 21 60 32 45

Array tree implementation corollary: a complete binary tree can be implemented using an array (the example tree shown is not a heap) Bob Mike Sam Tom Pam Joe Sue Ann Jane Mary i Item Left Child 2*i Right 1 Pam 2 3 Joe 4 5 Sue 6 7 Bob 8 9 Mike 10 -1 Sam 11 Tom 13 Ann 15 Jane 17 Mary 19 LeftChild(i) = 2*i RightChild(i) = 2*i + 1

Array binary tree - parent Bob Mike Sam Tom Pam Joe Sue Ann Jane Mary i Item Parent i / 2 1 Pam -1 2 Joe 3 Sue 4 Bob 5 Mike 6 Sam 7 Tom 8 Ann 9 Jane 10 Mary Parent(i) =  i / 2 

Implementation of a heap when implementing a complete binary tree, we actually can "cheat" and just use an array index of root = 1 (leave 0 empty for simplicity) for any node n at index i, index of n.left = 2i index of n.right = 2i + 1

Code for add method public boolean add(E element) { // grow array if needed if (this.size() >= this.elements.length - 1) { this.elements = this.resize(this.elements); } // place element into heap at bottom this.size++; this.elements[this.size] = element; this.bubbleUp(); return true;

Code for peek method public E peek() { if (this.isEmpty()) { throw new IllegalStateException(); } return this.elements[1];

Code for remove method public E remove() { E result = this.peek(); // move last element of array up to root this.elements[1] = this.elements[this.size]; this.elements[this.size] = null; this.size--; this.bubbleDown(); return result; }

The bubbleUp helper private void bubbleUp() { int index = this.size; while (hasParent(index) && elements[index].compareTo(parent(index)) < 0) { // parent/child are out of order; swap them int parentIndex = parentIndex(index); swap(elements, index, parentIndex); index = parentIndex; } // helpers private boolean hasParent(int i) { return i > 1; } private int parentIndex(int i) { return i/2; } private E parent(int i) { return elements[parentIndex(i)]; }

The bubbleDown helper private void bubbleDown() { int index = 1; while (hasLeftChild(index)) { int childIndex = leftIndex(index); if (hasRightChild(index) && right(index).compareTo(left(index)) < 0) { childIndex = rightIndex(index); } if (elements[index].compareTo(elements[childIndex]) > 0) { swap(elements, index, childIndex); // out of order index = childIndex; } else { break; // helpers private int leftIndex(int i) { return i*2; } private int rightIndex(int i) { return i*2 + 1; } private boolean hasLeftChild(int i) { return leftIndex(i) <= size(); } private boolean hasRightChild(int i) { return rightIndex(i) <= size(); }

Advantages of array heap the "implicit representation" of a heap in an array makes several operations very fast add a new node at the end (O(1)) from a node, find its parent (O(1)) swap parent and child (O(1)) a lot of dynamic memory allocation of tree nodes is avoided the algorithms shown usually have elegant solutions private void buildHeap() { for (int i = this.size() / 2; i > 0; i--) { this.bubbleDown(i); }

Heap sort

Heap sort heap sort: an algorithm to sort an array of N elements by turning the array into a heap, then doing a remove N times the elements will come out in sorted order! we can put them into a new sorted array what is the runtime?

A max-heap the heaps shown have been minimum heaps because the elements come out in ascending order a max-heap is the same thing, but with the elements in descending order

Improved heap sort the heap sort shown requires a second array we can use a max-heap to implement an improved version of heap sort that needs no extra storage O(n log n) runtime no external storage required! useful on low-memory devices elegant

Improved heap sort 1 use an array heap, but with 0 as the root index max-heap state after buildHeap operation:

Improved heap sort 2 state after one remove operation: modified remove that moves element to end

Improved heap sort 3 state after two remove operations: notice that the largest elements are at the end (becoming sorted!)

Sorting algorithms review Best case Average case ( † ) Worst case Selection sort n 2 n 2 n 2 Bubble sort n n 2 n 2 Insertion sort n n 2 n 2 Mergesort n log n n log n n log n 2 2 2 Heapsort n log n n log n n log n 2 2 2 Treesort n log n n log n n 2 2 2 Quicksort n log n n log n n 2 2 2 † According to Knuth, the average growth rate of Insertion sort is about 0.9 times that of Selection sort and about 0.4 times that of Bubble Sort. Also, the average growth rate of Quicksort is about 0.74 times that of Mergesort and about 0.5 times that of Heapsort.