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

Similar documents
CS 241 Analysis of Algorithms

BM267 - Introduction to Data Structures

Sorting and Searching

Heapsort. Heap data structure

Priority queues. Priority queues. Priority queue operations

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

Sorting and Searching

9. Heap : Priority Queue

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

Chapter 6 Heapsort 1

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

Topic: Heaps and priority queues

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

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

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

Module 2: Priority Queues

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

Module 2: Priority Queues

Properties of a heap (represented by an array A)

Priority Queues and Binary Heaps

Algorithms and Data Structures

Data Structures and Algorithms

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

Recall: Properties of B-Trees

Chapter 6 Heap and Its Application

The Heap Data Structure

CS165: Priority Queues, Heaps

Binary Heaps. CSE 373 Data Structures Lecture 11

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

Binary Heaps in Dynamic Arrays

Heaps and Priority Queues

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

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

Module 2: Priority Queues

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

CS350: Data Structures Heaps and Priority Queues

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?

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

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

Basic Data Structures and Heaps

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

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

Partha Sarathi Manal

Lecture 5: Sorting Part A

Heapsort. Why study Heapsort?

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Introduction to Algorithms 3 rd edition

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

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

Design and Analysis of Algorithms

Data Structures and Algorithms for Engineers

PRIORITY QUEUES AND HEAPS

CS301 All Current Final Term Paper Subjective 2013 Solved with refernces

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Data Structures Lesson 9

Heaps Goodrich, Tamassia. Heaps 1

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

CH 8. HEAPS AND PRIORITY QUEUES

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

Binary heaps (chapters ) Leftist heaps

CH. 8 PRIORITY QUEUES AND HEAPS

CS 10: Problem solving via Object Oriented Programming. Priori9zing 2

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

Heaps, Heapsort, Priority Queues

Algorithms and Data Structures

Lecture 7. Transform-and-Conquer

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

CSE 214 Computer Science II Heaps and Priority Queues

Questions from the material presented in this lecture

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Lecture: Analysis of Algorithms (CS )

Basic Data Structures (Version 7) Name:

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

CSCI2100B Data Structures Heaps

Stores a collection of elements each with an associated key value

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

Priority Queues. Chapter 9

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

Data Structures and Algorithms Chapter 4

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

Tables and Priority Queues

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;

Algorithms and Theory of Computation. Lecture 7: Priority Queue

1 Interlude: Is keeping the data sorted worth it? 2 Tree Heap and Priority queue

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

Data Structures and Algorithms Week 4

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

Section 4 SOLUTION: AVL Trees & B-Trees

Programming II (CS300)

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Heapsort. Algorithms.

Priority Queues Heaps Heapsort

Programming II (CS300)

Priority Queues Heaps Heapsort

Transcription:

CSE 241 Class 17 Jeremy Buhler October 28, 2015 And now for something completely different! 1 A New Abstract Data Type So far, we ve described ordered and unordered collections. Unordered collections didn t support efficient min or max Ordered collections supported both, plus total ordering operations (pred and succ) Both types support find. We ll now look at a new, partly-ordered data type: a Priority Queue. It supports: Q.insert(k) insert key k into the queue (could be record with key k, but we ll just think about key) Q.max() find maximum key in queue Q.ExtractMax() delete the maximum key from the queue What doesn t a priority queue support? No support for search, predecessor, or successor can have min/extractmin instead of max/extractmax, but not both Priority Queue Examples: You ve got a bunch of jobs x i running on a computer, each with some priority p i. Each time the CPU becomes available to run jobs, which job has the highest priority? Moreover, running the job changes its priority, so you need to repeatedly modify that job and find the new highest-priority job. Oooooor... for certain graph algorithms that we will study, you need to repeatedly find and remove or modify the largest- or smallest-weight vertex or edge in a graph quickly. Oooooor... You receive sales figures for a million books, and you want to report the top ten best-sellers (without sorting the whole set). 1

2 Heaps for Priority Queues One implementation of a priority queue is a binary heap. Abstractly, it looks like a binary tree, but it is not totally ordered like a BST. Nodes are sequentially numbered top to bottom, left to right. Max-first heaps satisfy the following heap property: For every node x in a heap, x s key is at least as large as the keys of every node in the subtree rooted at x. Tree representation of a heap is only useful for visualization. In practice, heaps are coded using arrays. For node at index i, its left and right children are at 2i and 2i + 1. For node at index i, i.parent = i/2 Root node is at index 1 to keep index arithmetic simple 2

0th element of the array is unused. (It s actually not that hard to switch to 0-based arithmetic.) Note that any heap element can be reached starting from root in at most Θ(log n) steps. IOW, tree representation is balanced. 3 Heap Max and Insert max() is trivial root always has maximal key. But how do we insert a key into the heap? Visualize on the tree. insert(k) first creates an empty node at the bottom of the heap. While k is greater than key of empty node s parent, swap empty node with parent ( bubble up ) Finally, write k into empty node. Cost is surely O(height) = O(log n). insert(15) Why didn t we write k into the empty node until it was correctly placed? In array rep, a node corresponds to an array cell. Swapping a node x with an empty node y becomes just write x s key into y. Saves us from explicitly copying k each time. 3

4 Heapify Heapify is a function used to correct violations of the heap property. We ll need it for, e.g., ExtractMax. Let i be a node for which both subtrees i.left and i.right are heaps (i.e. they satisfy the heap property). However, i might violate the heap property i.e. its key might be less than that of one or both children. After calling Heapify on i, the tree rooted at node i will be a heap. Idea: If i s key is smaller than that of some child, swap i with largest child. Heap violation, if any, now pushed down the tree one level. Recursively heapify the subtree that received node i. 4

Pseudocode for heap stored in array A, starting from ith cell. Heapify(A, i) if i is not a leaf j index of larger of i s children if A[j] > A[i] swap(a[i], A[j]) Heapify(A, j) Note: suppose you know the size n of the heap (you ll need this to implement it). How do you test if a node with index i is a leaf? If node i has no children, we have that 2i > n Hence, i (an integer) is a leaf if i > n/2 Note that Heapify does constant work per level of the heap and so requires time O(log n) for heap of size n. Note also that we can avoid some copies if we perform swaps as described for insertion. 5 ExtractMax We can use Heapify to implement ExtractMax. Suppose we delete the root node of the heap. Replace this node with the last element of the heap (shrinks array size by one) Root may now violate heap property. However, its subtrees (if any) are both heaps. To fix the heap, call Heapify starting at root. 5

ExtractMax(A) A[1] A[n] n Heapify(A, 1) Cost of ExtractMax is constant plus one call to Heapify, hence takes time O(log n). 6 Further Fun Facts About Heaps Can turn an arbitrary array into a heap in time O(n). Heapsort: turn input array into a heap, then repeatedly remove smallest element Can support increasing the key value of any node in a max-first heap. We ll need this for graph algorithms think about how! 6