Heaps Goodrich, Tamassia. Heaps 1

Similar documents
Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Heaps. 2/13/2006 Heaps 1

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Heaps and Priority Queues

Priority Queues and Heaps. Heaps and Priority Queues 1

Priority Queue ADT ( 7.1) Heaps and Priority Queues 2. Comparator ADT ( 7.1.4) Total Order Relation. Using Comparators in C++

Data Structures and Algorithms " Priority Queues!

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT

Stores a collection of elements each with an associated key value

Data Structures Lecture 7

CH 8. HEAPS AND PRIORITY QUEUES

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort

CSED233: Data Structures (2017F) Lecture9: Priority Queues and Heaps

CH. 8 PRIORITY QUEUES AND HEAPS

Priority Queues. Reading: 7.1, 7.2

Priority Queues & Heaps. Chapter 9

Height of a Heap. Heaps. 1. Insertion into a Heap. Heaps and Priority Queues. The insertion algorithm consists of three steps

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

Priority Queues and Heaps. More Data Structures. Priority Queue ADT ( 2.4.1) Total Order Relation. Sorting with a Priority Queue ( 2.4.

Chapter 2: Basic Data Structures

Priority queues. Priority queues. Priority queue operations

Elementary Data Structures 2

Algorithms and Data Structures

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

Data Structures and Algorithms

Priority Queues & Heaps. CS16: Introduction to Data Structures & Algorithms Spring 2019

Priority queues. Priority queues. Priority queue operations

1/27/2005 2:03 AM Priority Queues 1. Outline and Reading

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Programming II (CS300)

DataStruct 8. Heaps and Priority Queues

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

Data Structures and Algorithms

Priority Queues Goodrich, Tamassia. Priority Queues 1

csci 210: Data Structures Priority Queues and Heaps

Programming II (CS300)

Priority Queues 3/19/14

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

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

CS350: Data Structures Heaps and Priority Queues

Sorting and Searching

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

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Sorting and Searching

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 20, March 24, 2016

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

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

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Module 2: Priority Queues

Priority Queues and Binary Heaps

Priority Queues Heaps Heapsort

Priority Queues. Priority Queue. Keys and Total Order Relations. The Priority Queue ADT

Topics on the Midterm

Binary Heaps in Dynamic Arrays

Heaps and Priority Queues

Module 2: Priority Queues

Module 2: Priority Queues

Dictionaries. Priority Queues

9. Heap : Priority Queue

Heaps and Priority Queues

Priority Queues Heaps Heapsort

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Data Structures and Algorithms for Engineers

Properties of a heap (represented by an array A)

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

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

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

COMP250: Priority queue ADT, Heaps. Lecture 23 Jérôme Waldispühl School of Computer Science McGill University

Partha Sarathi Manal

CS711008Z Algorithm Design and Analysis

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

CS711008Z Algorithm Design and Analysis

CE 221 Data Structures and Algorithms

Binary Heaps. CSE 373 Data Structures Lecture 11

Administration CSE 326: Data Structures

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

Search Trees - 2. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore. M.S Ramaiah School of Advanced Studies - Bangalore

Topic: Heaps and priority queues

CSCI2100B Data Structures Heaps

Priority Queues, Binary Heaps, and Heapsort

Dictionaries. 2/17/2006 Dictionaries 1

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

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

CS2 Algorithms and Data Structures Note 6

Priority queue ADT Heaps. Lecture 21

CS165: Priority Queues, Heaps

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

Priority Queues and Heaps. Heaps of fun, for everyone!

Entry and Priority Queue ADTs

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Chapter 6 Heapsort 1

Algorithms and Data Structures

Binary heaps (chapters ) Leftist heaps

Data Structures Lesson 9

Heaps. A heap is a certain kind of complete binary tree.

CS/COE 1501

Transcription:

Heaps Heaps 1

Recall Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue ADT insert(k, x) inserts an entry with key k and value x Additional methods min() returns, but does not remove, an entry with smallest key size(), isempty() Applications: Standby flyers Auctions removemin() removes and returns the entry with smallest key Stock market Heaps

Recall PQ Sorting We use a priority queue Insert the elements with a series of insert operations Remove the elements in sorted order with a series of removemin operations The running time depends on the priority queue implementation: Unsorted sequence gives selection-sort: O(n ) time Sorted sequence gives insertion-sort: O(n ) time Can we do better? Algorithm PQ-Sort(S, C) Input sequence S, comparator C for the elements of S Output sequence S sorted in increasing order according to C P priority queue with comparator C while S.isEmpty () e S.remove (S. first ()) P.insertItem(e, e) while P.isEmpty() e P.removeMin().getKey() S.addLast(e) Heaps 3

Heaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: Heap-Order: for every internal node v other than the root, key(v) key(parent(v)) The last node of a heap is the rightmost node of maximum depth Complete Binary Tree: let h be the height of the heap for i = 0,, h - 1, there are i nodes of depth i at depth h - 1, the internal nodes are to the left of the external nodes last node Heaps 4

Height of a Heap Theorem: A heap storing n keys has height O(log n) Proof: (we apply the complete binary tree property) Let h be the height of a heap storing n keys Since there are i keys at depth i = 0,, h - 1 and at least one key at depth h, we have n 1 + + 4 + + h-1 + 1 Thus, n h, i.e., h log n depth 0 1 h-1 h keys 1 h-1 1 Heaps

Heaps and Priority Queues We can use a heap to implement a priority queue We store a (key, element) item at each internal node We keep track of the position of the last node (, Sue) (, Pat) (, Mark) (, Jeff) (, Anna) Heaps

Insertion into a Heap Method insertitem of the priority queue ADT corresponds to the insertion of a key k to the heap The insertion algorithm consists of three steps Find the insertion node z (the new last node) Store k at z Restore the heap-order property (discussed next) 1 z insertion node z Heaps

Upheap After the insertion of a new key k, the heap-order property may be violated Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k Since a heap has height O(log n), upheap runs in O(log n) time 1 z 1 z Heaps 8

Removal from a Heap (.3.3) Method removemin of the priority queue ADT corresponds to the removal of the root key from the heap The removal algorithm consists of three steps w last node Replace the root key with the key of the last node w Remove w w Restore the heap-order property (discussed next) new last node Heaps

Downheap After replacing the root key with the key k of the last node, the heap-order property may be violated Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k Since a heap has height O(log n), downheap runs in O(log n) time w w Heaps 10

Updating the Last Node The insertion node can be found by traversing a path of O(log n) nodes Go up until a left child or the root is reached If a left child is reached, go to the right child Go down left until a leaf is reached Similar algorithm for updating the last node after a removal Heaps 11

Heap-Sort Consider a priority queue with n items implemented by means of a heap the space used is O(n) methods insert and removemin take O(log n) time methods size, isempty, and min take time O(1) time Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time The resulting algorithm is called heap-sort Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selection-sort Heaps 1

Vector-based Heap Implementation We can represent a heap with n keys by means of a vector of length n + 1 For the node at rank i the left child is at rank i the right child is at rank i + 1 Links between nodes are not explicitly stored The cell of at rank 0 is not used Operation insert corresponds to inserting at rank n + 1 Operation removemin corresponds to removing at rank n 0 1 3 4 Yields in-place heap-sort Heaps 13

Merging Two Heaps We are given two two heaps and a key k 8 3 4 We create a new heap with the root node storing k and with the two heaps as subtrees We perform downheap to restore the heaporder property 8 3 3 4 4 8 Heaps 14

Bottom-up Heap Construction We can construct a heap storing n given keys in using a bottom-up construction with log n phases In phase i, pairs of heaps with i -1 keys are merged into heaps with i+1-1 keys i -1 i -1 i+1-1 Heaps 1

Example 1 1 4 1 3 0 11 1 1 4 1 3 0 Heaps 1

Example (contd.) 11 1 1 4 1 3 0 1 4 3 1 1 11 0 Heaps 1

Example (contd.) 8 1 4 3 1 1 11 0 4 1 8 3 1 1 11 0 Heaps 18

Example (end) 10 4 1 8 3 1 1 11 0 4 1 8 3 1 10 1 11 0 Heaps 1

Analysis We visualize the worst-case time of a downheap with a proxy path that goes first right and then repeatedly goes left until the bottom of the heap (this path may differ from the actual downheap path) Since each node is traversed by at most two proxy paths, the total number of nodes of the proxy paths is O(n) Thus, bottom-up heap construction runs in O(n) time Bottom-up heap construction is faster than n successive insertions and speeds up the first phase of heap-sort Heaps 0