Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

Similar documents
PRIORITY QUEUES AND HEAPS

Priority queues. Priority queues. Priority queue operations

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Heapsort. Why study Heapsort?

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

CS165: Priority Queues, Heaps

9. Heap : Priority Queue

Algorithms and Data Structures

CSCI2100B Data Structures Heaps

Heaps. Heaps Priority Queue Revisit HeapSort

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Priority Queues and Binary Heaps

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CS350: Data Structures Heaps and Priority Queues

Unit #2: Priority Queues

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

AVL Trees Heaps And Complexity

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

Data Structures and Algorithms

Algorithms and Data Structures

Heaps. Heaps. A heap is a complete binary tree.

CMSC 341 Lecture 14: Priority Queues, Heaps

Heap Model. specialized queue required heap (priority queue) provides at least

CS 361 Data Structures & Algs Lecture 9. Prof. Tom Hayes University of New Mexico

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Priority Queues Heaps Heapsort

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Priority Queues Heaps Heapsort

CSE 373: Data Structures and Algorithms

Heaps. 2/13/2006 Heaps 1

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

The Heap Data Structure

Heaps, Heap Sort, and Priority Queues.

Programming II (CS300)

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

BM267 - Introduction to Data Structures

Heapsort. Heap data structure

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

CSE 2123: Collections: Priority Queues. Jeremy Morris

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Data Structures Lesson 9

Priority Queues. T. M. Murali. January 29, 2009

COSC160: Data Structures Heaps. Jeremy Bolton, PhD Assistant Teaching Professor

Heaps and Priority Queues

CS 240 Data Structures and Data Management. Module 2: Priority Queues

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

Chapter 6 Heapsort 1

Module 2: Priority Queues

Sorting and Searching

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Data Structures and Algorithms for Engineers

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Properties of a heap (represented by an array A)

Module 2: Priority Queues

Programming II (CS300)

Priority Queues. T. M. Murali. January 26, T. M. Murali January 26, 2016 Priority Queues

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

Sorting and Searching

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Priority Queues (Heaps)

CE 221 Data Structures and Algorithms

Topic: Heaps and priority queues

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012

CSE373: Data Structures & Algorithms Priority Queues

PRIORITY QUEUES AND HEAPS

Heap sort. Carlos Moreno uwaterloo.ca EIT

Heaps. (our first advanced data structure)

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

CSC263 Week 2. Larry Zhang

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

The priority is indicated by a number, the lower the number - the higher the priority.

Heaps. A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Transform & Conquer. Presorting

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

Data Structures Question Bank Multiple Choice

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Stores a collection of elements each with an associated key value

Binary heaps (chapters ) Leftist heaps

Priority Queues. Binary Heaps

Transcription:

Operations Priority Queues & Heaps : insert item with a priority : remove the item with highest priority, and break ties with FIFO ordering For simplicity: we use an int as the object and the priority In practice: need <int priority, Object elt> pair Niave Solution: Prio-Q As Array : add item to end of array : search for highest priority item Prio-Q as Array : O(1) assuming no array doubling : O(n) O(n) to find element, O(1) to patch hole 1 5 3 4 3 1 5 3 4? 1 5 3 4 Prio-Q as Sorted Array Maintain array in sorted order, largest first : add item to correct place in array : take from head of array 5 4 3 3 1 : O(n) Prio-Q as Sorted Array O(n) to find correct position + O(n) to shift remaining elements out of the way Can we use binary search? : O(n) Need to slide all elements to the left 1

Prio-Q as Sorted Circular Buffer Maintain circular buffer in sorted order, largest first : add item to correct place in buffer : take from head of buffer head head 5 4 3 3 1 : O(n) Prio-Q as Sorted Circular Buffer O(n) to find correct position + O(n) to shift remaining elements out of the way Can we use binary search? : O(1) head : O(n) Prio-Q as Sorted Linked List O(n) to find item (linear search) + O(1) to splice it out : O(1) Special Case: at most p priorities Many applications only use a fixed number of priorities: 8-Puzzle with # tiles correct as heuristic: p in 0..8 Java task priorities: usually 1..10 Email priority: very low, low, normal, high, urgent Cool implementation: Use array of p (FIFO) queues, one for each priority value : O(1) : O(P) P = # priorities p = 0 1 2 3 4 5 6 7 8 1 3 3 4 5 General Solution: Heap A heap is a tree in which 1. an integer is stored at each node 2. integer stored at node is >= integer stored at any of its children the heap property Heaps in Reality Ages of people in a family tree Parent older than children But your uncle may be younger than you Salaries boss makes more than underlings but temp worker in one division can make more than a supervisor in a different division For now, we only care about binary trees 2

Running Example: Crime Family Nodes contain name and ruthlessness (= # crimes committed, an integer) Boss is always more ruthless than a subordinate Prio-Q as Heap : must return element with largest priority : insert somewhere in the heap and maintain/restore the heap property heapify = restore the heap property Highest priority always at root Remove root, then fill hole Solution #1: promote the highest-priority child recurse until you promote a leaf element delete the now-empty leaf Heapify #1: Restore the heap property Insert new element into a new leaf node, anywhere in the tree Then bubble up the new element until heap property is restored compare new element against parent if needed, swap then repeat in the new position worst case: bubble up all the way to the root la Familia Bubble up 3

Prio-Q as Heap : O(n) worst case, but O(log n) if tree is nice : O(n) worst case, but O(log n) if tree is nice Keeping the Tree Fat & Short A complete binary tree is a tree in which: 1. Nodes are numbered in a bfs fashion (the position ) 2. Node at position n is filled only if all nodes in positions less than n are filled 12 Very Nice Not Nice Nice Heap As Complete Binary Tree Will keep tree short and fat, and us happy Get: will ruin the complete binary tree property, since the bottom-right-most node might not be the one to promoted Put: need to create new node in the first empty position easy to name (next empty position = current size + 1) how to find the position once we have the number? Quick Diversion: Binary Numbers Finding empty position 13 given root (position 1) 13 = 8 + 4 + 0 + 1 = 1 2 3 + 1 2 2 + 0 2 1 + 1 2 0 = 1101 (binary) So we can find any position in log n steps If current size = n, insert at position n = n+1 Convert n to binary substitute R for 1, and L for 0 follow links to the position Insert new element Bubble up, as before is still broken 4

Fixing (2) (1) steps: 1. Extract root 2. Promote last element directly into root Fixing Prio-Q as Heap Use a heap that is a complete binary tree Keep track of size : O(log n) remove root, promote last leaf node, bubble down : O(log n) insert into last+1 leaf node, bubble up Neat Trick for complete binary trees Don t store as Tree Cells Instead, store in an array, indexed by position # children(i) = 2i and 2i+1 parent(i) = i/2 position 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 22 14 12 8 7 11 4 7 4 3 2 Heap Construction build-heap: Given n elements in an array, rearrange to build a heap Obvious solution: Start with empty array Do heap insert for each element 1 + n * log n O(n log n) There is a faster way 5