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

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

Heaps Goodrich, Tamassia. Heaps 1

Heaps. 2/13/2006 Heaps 1

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Minimum Spanning Trees

Minimum Spanning Trees. Application: Connecting a Network

Priority Queues. Binary Heaps

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

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Minimum Spanning Trees

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

CIS 121. Introduction to Trees

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

Stores a collection of elements each with an associated key value

CH 8. HEAPS AND PRIORITY QUEUES

Priority Queues and Heaps. Heaps and Priority Queues 1

CH. 8 PRIORITY QUEUES AND HEAPS

Lecture 5. Counting Sort / Radix Sort

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

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

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

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

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

Data Structures Lecture 7

Heaps and Priority Queues

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

6.854J / J Advanced Algorithms Fall 2008

quality/quantity peak time/ratio

1/27/12. Vectors: Outline and Reading. Chapter 6: Vectors, Lists and Sequences. The Vector ADT. Applications of Vectors. Array based Vector: Insertion

Analysis of Algorithms

Data Structures Week #5. Trees (Ağaçlar)

Data Structures Week #9. Sorting

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

CSE 417: Algorithms and Computational Complexity

Elementary Data Structures. The Stack ADT ( 2.1.1) Applications of Stacks. Stacks, Queues, Vectors, Lists & Sequences Trees

Data Structures and Algorithms " Priority Queues!

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

Data Structures and Algorithms Part 1.4

Fundamental Algorithms

Data Structures and Algorithms. Analysis of Algorithms

BST Sequence of Operations

Priority Queues & Heaps. Chapter 9

Algorithm Design Techniques. Divide and conquer Problem

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

Graph Terminology and Representations

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

Analysis of Algorithms

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

Graphs ORD SFO LAX DFW

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Algorithm Efficiency

Lower Bounds for Sorting

The Magma Database file formats

1 Graph Sparsfication

Homework 1 Solutions MA 522 Fall 2017

6.851: Advanced Data Structures Spring Lecture 17 April 24

Priority Queues. Reading: 7.1, 7.2

Chapter 2: Basic Data Structures

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Big-O Analysis. Asymptotics

Examples and Applications of Binary Search

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Applications of Stacks

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

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

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Lecture 1: Introduction and Strassen s Algorithm

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

Thompson s Group F (p + 1) is not Minimally Almost Convex

On Sorting an Intransitive Total Ordered Set Using Semi-Heap

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

CMPT 125 Assignment 2 Solutions

Design and Analysis of Algorithms Notes

Linked Lists 11/16/18. Preliminaries. Java References. Objects and references. Self references. Linking self-referential nodes

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

Programming II (CS300)

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

EE 459/500 HDL Based Digital Design with Programmable Logic. Lecture 13 Control and Sequencing: Hardwired and Microprogrammed Control

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

Algorithm. Counting Sort Analysis of Algorithms

The isoperimetric problem on the hypercube

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

Computational Geometry

Combination Labelings Of Graphs

A New Morphological 3D Shape Decomposition: Grayscale Interframe Interpolation Method

Ones Assignment Method for Solving Traveling Salesman Problem

Big-O Analysis. Asymptotics

3. b. Present a combinatorial argument that for all positive integers n : : 2 n

A Generalized Set Theoretic Approach for Time and Space Complexity Analysis of Algorithms and Functions

Transcription:

Presetatio for use with the textbook Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 201 Heaps 201 Goodrich ad Tamassia xkcd. http://xkcd.com/83/. Tree. Used with permissio uder Creative Commos 2. Licese. Heaps 1

Recall Priority Queue Operatios A priority ueue stores a collectio of etries Each etry is a pair (key, value) Mai methods of the Priority Queue ADT isert(k, v) iserts a etry with key k ad value v Additioal methods mi() returs, but does ot remove, a etry with smallest key size(), isempty() Applicatios: Stadby flyers Auctios removemi() removes ad returs the etry with smallest key Stock market eigies 201 Goodrich ad Tamassia Heaps 2

Recall PQ Sortig We use a priority ueue Isert the elemets with a series of isert operatios Remove the elemets i sorted order with a series of removemi operatios The ruig time depeds o the priority ueue implemetatio: Usorted seuece gives selectio-sort: O( 2 ) time Sorted seuece gives isertio-sort: O( 2 ) time Ca we do better? 201 Goodrich ad Tamassia Heaps 3

Heaps A heap is a biary tree storig keys at its odes ad satisfyig the followig properties: Heap-Order: for every iteral ode v other tha the root, key(v) key(paret(v)) The last ode of a heap is the rightmost ode of maximum depth 2 Complete Biary Tree: let h be the height of the heap for i = 0,, h - 1, there are 2 i odes of depth i at depth h - 1, the iteral odes are to the left of the exteral odes last ode 201 Goodrich ad Tamassia Heaps 4

Height of a Heap Theorem: A heap storig keys has height O(log ) Proof: (we apply the complete biary tree property) Let h be the height of a heap storig keys Sice there are 2 i keys at depth i = 0,, h - 1 ad at least oe key at depth h, we have 1 + 2 + 4 + + 2 h-1 + 1 Thus, 2 h, i.e., h log depth 0 1 h-1 h keys 1 2 2 h-1 1 201 Goodrich ad Tamassia Heaps

Heaps ad Priority Queues We ca use a heap to implemet a priority ueue We store a (key, elemet) item at each iteral ode We keep track of the positio of the last ode (2, Sue) (, Pat) (, Mark) (, Jeff) (, Aa) 201 Goodrich ad Tamassia Heaps

Array-based Heap Implemetatio We ca represet a heap with keys by meas of a array of legth For the ode at rak i the left child is at rak 2i the right child is at rak 2i + 1 2 Liks betwee odes are ot explicitly stored Operatio add correspods to isertig at rak + 1 Operatio remove_mi correspods to removig at rak Yields i-place heap-sort 2 1 2 3 4 201 Goodrich ad Tamassia Heaps

Isertio ito a Heap Method isertitem of the priority ueue ADT correspods to the isertio of a key k to the heap The isertio algorithm cosists of three steps Fid the isertio ode z (the ew last ode) Store k at z Restore the heap-order property (discussed ext) 2 2 1 z isertio ode z 201 Goodrich ad Tamassia Heaps 8

Upheap After the isertio of a ew key k, the heap-order property may be violated Algorithm upheap restores the heap-order property by swappig k alog a upward path from the isertio ode Upheap termiates whe the key k reaches the root or a ode whose paret has a key smaller tha or eual to k Sice a heap has height O(log ), upheap rus i O(log ) time 2 1 z 1 z 2 201 Goodrich ad Tamassia Heaps

Isertio Pseudo-Code Assumes a array-based heap implemetatio. 201 Goodrich ad Tamassia Heaps 10

Removal from a Heap Method removemi of the priority ueue ADT correspods to the removal of the root key from the heap The removal algorithm cosists of three steps Replace the root key with the key of the last ode w Remove w Restore the heap-order property (discussed ext) w w 2 ew last ode last ode 201 Goodrich ad Tamassia Heaps 11

Dowheap After replacig the root key with the key k of the last ode, the heap-order property may be violated Algorithm dowheap restores the heap-order property by swappig key k alog a dowward path from the root Upheap termiates whe key k reaches a leaf or a ode whose childre have keys greater tha or eual to k Sice a heap has height O(log ), dowheap rus i O(log ) time w w 201 Goodrich ad Tamassia Heaps 12

RemoveMi Pseudo-code Assumes heap is implemeted with a array. 201 Goodrich ad Tamassia Heaps 13

Performace of a Heap A heap has the followig performace for the priority ueue operatios. The above aalysis is based o the followig facts: The height of heap T is O(log ), sice T is complete. I the worst case, up-heap ad dow-heap bubblig take time proportioal to the height of T. Fidig the isertio positio i the executio of isert ad updatig the last ode positio i the executio of removemi takes costat time. The heap T has iteral odes, each storig a referece to a key ad a referece to a elemet. 201 Goodrich ad Tamassia Heaps 14

Heap-Sort Cosider a priority ueue with items implemeted by meas of a heap the space used is O() methods isert ad removemi take O(log ) time methods size, isempty, ad mi take time O(1) time Usig a heap-based priority ueue, we ca sort a seuece of elemets i O( log ) time The resultig algorithm is called heap-sort Heap-sort is much faster tha uadratic sortig algorithms, such as isertio-sort ad selectio-sort 201 Goodrich ad Tamassia Heaps 1

Mergig Two Heaps We are give two two heaps ad a key k 8 3 4 2 We create a ew heap with the root ode storig k ad with the two heaps as subtrees We perform dowheap to restore the heaporder property 8 3 2 4 2 3 4 8 201 Goodrich ad Tamassia Heaps 1

Bottom-up Heap Costructio We ca costruct a heap storig give keys i usig a bottom-up costructio with log phases I phase i, pairs of heaps with 2 i -1 keys are merged ito heaps with 2 i+1-1 keys 2 i -1 2 i -1 2 i+1-1 201 Goodrich ad Tamassia Heaps 1

Example 1 1 4 12 23 20 2 11 2 1 1 4 12 23 20 201 Goodrich ad Tamassia Heaps 18

Example (cotd.) 2 11 2 1 1 4 12 23 20 1 4 23 1 2 12 11 2 20 201 Goodrich ad Tamassia Heaps 1

Example (cotd.) 8 1 4 23 1 2 12 11 2 20 4 1 8 23 1 2 12 11 2 20 201 Goodrich ad Tamassia Heaps 20

Example (ed) 10 4 1 8 23 1 2 12 11 2 20 4 1 8 23 1 2 10 12 11 2 20 201 Goodrich ad Tamassia Heaps 21

Aalysis We visualize the worst-case time of a dowheap with a proxy path that goes first right ad the repeatedly goes left util the bottom of the heap (this path may differ from the actual dowheap path) Sice each ode is traversed by at most two proxy paths, the total umber of odes of the proxy paths is O() Thus, bottom-up heap costructio rus i O() time Bottom-up heap costructio is faster tha successive isertios ad speeds up the first phase of heap-sort, which takes O( log ) time i its secod phase. 201 Goodrich ad Tamassia Heaps 22