Priority queues and heaps Professors Clark F. Olson and Carol Zander

Similar documents
The AVL Balance Condition. CSE 326: Data Structures. AVL Trees. The AVL Tree Data Structure. Is this an AVL Tree? Height of an AVL Tree

Design and Analysis of Algorithms

CE 221 Data Structures and Algorithms

CS221: Algorithms and Data Structures. Priority Queues and Heaps. Alan J. Hu (Borrowing slides from Steve Wolfman)

Section 2.3: Calculating Limits using the Limit Laws

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

Programming in Fortran 90 : 2017/2018

Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Sorting. Sorting. Why Sort? Consistent Ordering

CSE 326: Data Structures Quicksort Comparison Sorting Bound

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

Sorting: The Big Picture. The steps of QuickSort. QuickSort Example. QuickSort Example. QuickSort Example. Recursive Quicksort

ELEC 377 Operating Systems. Week 6 Class 3

Machine Learning. K-means Algorithm

Problem Set 3 Solutions

Sorting and Algorithm Analysis

Esc101 Lecture 1 st April, 2008 Generating Permutation

Load Balancing for Hex-Cell Interconnection Network

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

Bounding Tree Cover Number and Positive Semidefinite Zero Forcing Number

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Priority Queues (Heaps)

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

each node in the tree, the difference in height of its two subtrees is at the most p. AVL tree is a BST that is height-balanced-1-tree.

CS 234. Module 6. October 16, CS 234 Module 6 ADT Dictionary 1 / 33

Haar Transform CS 430 Denbigh Starkey

Insertion Sort. Divide and Conquer Sorting. Divide and Conquer. Mergesort. Mergesort Example. Auxiliary Array

Notes on Organizing Java Code: Packages, Visibility, and Scope

Lecture #5.3 Mirrors

A Polylog Time Wait-Free Construction for Closed Objects

An Optimal Algorithm for Prufer Codes *

CSE 332: Data Structures & Parallelism Lecture 8: AVL Trees. Ruth Anderson Winter 2019

Announcements. Lilian s office hours rescheduled: Fri 2-4pm HW2 out tomorrow, due Thursday, 7/7. CSE373: Data Structures & Algorithms

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

19.2 Surface Area of Prisms and Cylinders

CS240: Programming in C. Lecture 12: Polymorphic Sorting

4.1 Tangent Lines. y 2 y 1 = y 2 y 1

CS 234. Module 6. October 25, CS 234 Module 6 ADT Dictionary 1 / 22

CHAPTER 10: ALGORITHM DESIGN TECHNIQUES

Wrap up Amortized Analysis; AVL Trees. Riley Porter Winter CSE373: Data Structures & Algorithms

More on Sorting: Quick Sort and Heap Sort

Searching & Sorting. Definitions of Search and Sort. Linear Search in C++ Linear Search. Week 11. index to the item, or -1 if not found.

Sorting. Sorted Original. index. index

4.2 The Derivative. f(x + h) f(x) lim

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

Heaps, Heap Sort, and Priority Queues.

CMPS 10 Introduction to Computer Science Lecture Notes

Quicksort. Part 1: Understanding Quicksort

Investigations of Topology and Shape of Multi-material Optimum Design of Structures

An Analytical Tool to Assess Readiness of Existing Networks for Deploying IP Telephony

15-122: Principles of Imperative Computation, Summer 2011 Assignment 6: Trees and Secret Codes

Introduction to Programming. Lecture 13: Container data structures. Container data structures. Topics for this lecture. A basic issue with containers

AVL Trees Outline and Required Reading: AVL Trees ( 11.2) CSE 2011, Winter 2017 Instructor: N. Vlajic

Mode-seeking by Medoidshifts

Design Patterns for Data Structures. Chapter 10. Balanced Trees

Greedy Technique - Definition

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

CS1100 Introduction to Programming

1 Dynamic Connectivity

6.854 Advanced Algorithms Petar Maymounkov Problem Set 11 (November 23, 2005) With: Benjamin Rossman, Oren Weimann, and Pouya Kheradpour

Design Patterns for Data Structures. Chapter 10. Balanced Trees

An Application of the Dulmage-Mendelsohn Decomposition to Sparse Null Space Bases of Full Row Rank Matrices

2.8 The derivative as a function

Data Structures and Algorithms

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

ON SOME ENTERTAINING APPLICATIONS OF THE CONCEPT OF SET IN COMPUTER SCIENCE COURSE

More on Functions and Their Graphs

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

Path Planning for Formation Control of Autonomous

On Some Entertaining Applications of the Concept of Set in Computer Science Course

Binary Search Tree - Best Time. AVL Trees. Binary Search Tree - Worst Time. Balanced and unbalanced BST

Priority Queues Heaps Heapsort

The ray density estimation of a CT system by a supervised learning algorithm

Priority Queues (Heaps)

CSCI2100B Data Structures Heaps

The Codesign Challenge

Binary heaps (chapters ) Leftist heaps

CS350: Data Structures Heaps and Priority Queues

EECS 730 Introduction to Bioinformatics Sequence Alignment. Luke Huan Electrical Engineering and Computer Science

Brave New World Pseudocode Reference

3.6 Directional Derivatives and the Gradient Vector

Outline. Self-Organizing Maps (SOM) US Hebbian Learning, Cntd. The learning rule is Hebbian like:

Areas of Triangles and Parallelograms. Bases of a parallelogram. Height of a parallelogram THEOREM 11.3: AREA OF A TRIANGLE. a and its corresponding.

When a BST becomes badly unbalanced, the search behavior can degenerate to that of a sorted linked list, O(N).

Cache Performance 3/28/17. Agenda. Cache Abstraction and Metrics. Direct-Mapped Cache: Placement and Access

Active Contours/Snakes

Pass by Reference vs. Pass by Value

A Distributed First and Last Consistent Global Checkpoint Algorithm

13.5 DIRECTIONAL DERIVATIVES and the GRADIENT VECTOR

R s s f. m y s. SPH3UW Unit 7.3 Spherical Concave Mirrors Page 1 of 12. Notes

Data Structures and Programming Spring 2014, Midterm Exam.

Data Structures Lesson 9

Transcription:

Prorty queues and eaps Professors Clark F. Olson and Carol Zander Prorty queues A common abstract data type (ADT) n computer scence s te prorty queue. As you mgt expect from te name, eac tem n te prorty queue as a prorty tat determnes te order n wc te tems leave te queue (not frst n frst out, as n te queues we ave seen prevously). Tere are many applcatons of prorty queues. Scedulng prnt jobs and system processes are two smple examples. Ts s also te abstract data type tat we need to mplement Huffman codng. Te prmary operatons tat are necessary for a prorty queue are: nsert (wt prorty) fndmn (or fndmax) deletemn (or deletemax) Of course, we want to be able to mplement tese tree operatons effcently n terms of n (te number of tems n te queue). Ts could be mplemented n many ways, ncludng several tat we ave already seen. Data structure nsert deletemn Unsorted collecton O(1) O(n) Sorted array O(n) O(1) Sorted lnked lst O(n) O(1) Bnary searc tree (balanced) O(log n) O(log n) Only te balanced bnary searc tree s effcent (n te worst case) unless te number of nsertons s very small compared to te number of deletemn operatons. Tere are two types of data structure tat can guarantee O(log n) tme for nsert and deletemn n te worst case. One s te balanced bnary searc tree; te oter (smpler) data structure s a eap. (Note tat ts s not related to te usage of eap to descrbe free memory.) Heaps Bnary eaps are complete bnary trees tat satsfy te eap order property, wc states tat eac node (except te root) must ave a key (or prorty) tat s no less tan te key of ts parent. (Ts s a mneap. A maxeap would be te reverse.) Complete bnary tree means tat all levels are completely full except possbly te last level, wc s flled from left to rgt. Wt ts defnton, eac subtree wtn te eap s also a eap and te mnmum key must be at te root of te tree. Here s an example eap: 17 8 19 6 1 1 7 41 15 Note tat tere s no restrcton on te orderng of te cldren of a node; no tree traversal wll output te tems n sorted order. We can also ave smaller keys at lower levels of te tree tan larger keys,.e., tere s no relatonsp between left and rgt subtrees oter tan te parent s smaller. Page 1 of 7

Usually, tese are mplemented wt an array, snce tey are always complete trees. 0 1 4 5 6 7 8 9 10 11 1 14 15 16 17-17 8 19 6 1 1 7 41 15 Note tat te frst node s often ntalzed to a value known to be smaller tan all values n te eap n order to smplfy te nserton operaton. Te array may be larger tan te eap to allow nserton or because tems ave been deleted from te eap. Wen mplemented, a eap keeps track of bot ow many tems are n te eap and ow muc space as been allocated to te array. Even better s to use a vector, wc keeps track of te allocaton for us, so te declaraton mgt look lke ts. class Heap { publc: // Constructor, copy constructor, destructor, as usual vod nsert(comparable *c); const Comparable * fndmn() const; bool deletemn(); prvate: nt sze; vector<comparable *> tems; ; We can perform a fndmn operaton n O(1) tme on a mneap, snce te mnmum value s always at te root (*tems[1]). Heap nserton To nsert n a eap, you place te value n te next avalable spot and ten percolate t upwards untl t s no longer smaller tan ts parent s. Let s nsert a 5 nto our eap above. Wen we start te structure looks lke ts: 17 8 19 6 1 1 7 41 15 5 Often te tem s not actually n te end array poston yet. Ts way as we percolate te value up, t s one assgnment (one operaton) nstead of one swap (tree operatons). Page of 7

Ts sn t a eap, snce te 5 s smaller tan ts parent. We swap t wt te to fx ts. Snce t stll sn t smaller tan ts parent s, we ten swap t wt te 8 to get te followng eap: 17 5 19 6 8 1 1 7 41 15 Try nsertng,, and 19 (n tat order) to get a new eap. In practce, te nserton works on te array lke ts: vod Heap<Comparable>::nsert(Comparable *c) { // add tem n poston 0 (dummy poston) to prevent percolatng up from root tems[0] = c; // Ensure we ave enoug space sze++; f (tems.sze() == sze) tems.pus_back(null); // Percolate up nt poston = sze; wle (*tems[0] < *tems[poston / ]) { tems[poston] = tems[poston / ]; poston = poston / ; tems[poston] = tems[0]; Te code above uses a couple of optmzatons. One s not to nsert te tem untl te end. In ts case, we just move te necessary elements untl we fnd te fnal restng spot for te new tem. Also, te value at poston zero s ntalzed to te new tem n ts case. Ts s also a useful tecnque wen usng templates, snce we can t create a dummy object for ndex 0 tat we know as a key less tan every object tat wll be placed n te eap. (Note tat vector transparently ncreases te sze of te nternal dynamc array by more tan one at a tme.) Ts operaton s O(log n) n te worst case, but O(1) n te average case (assumng tat te numbers are nserted n random order). Te average number of levels tat a new tem needs to be percolated s only 1.6. Arguably, ts s O(n) n te worst case, f we need to expand te sze of te vector or array, but clever trcks can be used to nsure tat we do no more tan O(log n), f necessary. Page of 7

Te deletemn operaton Wen we want to do a deletemn, we perform a smlar operaton n reverse. So tat tere are no oles n te array, we move te last element to te root and ten percolate t down. Here s te state wen we move te last element to te root: 17 5 19 6 8 1 1 7 41 15 Now we need to reeapfy te tree by percolatng te down. We always replace t wt te smaller of te two cldren (unless tey are bot larger tan t s) and, ten, repeat te process, yeldng: 5 17 8 19 6 1 1 7 41 15 Te code looks lke: bool Heap<Comparable>::deleteMn() { f (sze == 0) return false; delete tems[1]; tems[1] = tems[sze]; sze--; percolatedown(1); return true; vod Heap<Comparable>::percolateDown(nt poston) { nt cld = poston * ; f (cld > sze) return; f (cld!= sze && *tems[cld] > *tems[cld + 1]) cld++; f (*tems[cld] < *tems[poston]) { swap(tems[cld], tems[poston]); percolatedown(cld); Page 4 of 7

Ts operaton requres O(log n) tme n te average case and n te worst case, snce te last tem s usually relatvely large and often needs to be percolated down almost to te bottom. Lnear tme eap buldng We can buld a eap n lnear expected tme by usng nsert repeatedly, but ts as O(n log n) worst case effcency. It turns out tat we can buld a eap n lnear tme n te worst case f we ave all of te elements wen we start. Te basc dea s stragtforward. We call percolatedown for eac non leaf node startng wt te last node and endng wt te root. // te routne assumes tat te objects and sze ave already // been loaded nto te eap data vod Heap<Comparable>::eapfy() { for (nt = sze / ; > 0; --) percolatedown(); How do we know tat sze/ s te rgt place to start? We can t ave more tan sze/ non leaf nodes. Eac non leaf node as two cldren (wt one possble excepton te last non leaf node). If tere are m non leaves, ten tere are eter m 1 or m cldren. Snce m 1 of tese cldren are oter non leaves (one non leaf s te root), tat leaves m or m+1 leaf nodes and tere must be at least as many leaves as tere are non leaves. Does ts result n a eap wen we re done? Let s try an example: 8 4 46 44 41 1 19 10 45 8 Te 41 percolates down one spot, swappng wt te 8. Te 44 swaps wt te 10. Te 46 swaps wt te 19. Essentally we turn te bottom level of trees nto eaps. Ts yelds: Page 5 of 7

8 4 19 10 8 1 46 44 45 41 At te next level, te 4 swaps wt te. Te swaps wt te 10. Now tese subtrees are eaps. Fnally, at te root, te 8 swaps wt te 10, and ten te 19, and ten te 1, yeldng: 10 19 1 8 4 8 46 44 45 41 Two questons: Do we know tat ts always results n a eap? Does ts really take lnear tme n te worst case? For te frst queston: f we start wt two eaps, jon tem togeter wt any root and, ten, percolate te root down, we end up wt a eap. Inducton usng ts dea proves tat we get a eap n te end wt eapfy. For te second queston: wat s te worst tat can appen? At every non leaf node, we could ave to percolate t down to a leaf. Ts means tat te maxmum number of swaps s te same as te sum of te egts of eac of te nodes n te tree. Provng te followng teorem, proves eapfy s O(N) were N = (+1) 1. N s te number of nodes n a complete bnary tree. (Assume =0 for a tree wt just a root.) Teorem: For a complete bnary tree of egt contanng (+1) 1 nodes, te sum of te egts s (+1) 1 (+1). (Note tat snce N = (+1) 1, (+1) 1 (+1) s O(N).) Page 6 of 7

Proof: Sum te egt of te tree. Here we use te defnton tat says an empty tree as egt = 1; a tree wt just a root s egt zero. For example, wen s, te sum of te egts s: 1() + () + 4(1) + 8(0) Now generalze and wrte as a formula: 1() + () + 4(1) + 8(0) = 0 (-0) + 1 (-1) + (-) + (-) = 0 (-0) + 1 (-1) + (-) + (-) // \ / \ / \ // \ / \ // \ // \ / \ / \ // \ /\ /\ /\ // \ /\ // \ /\ //\\//\\ //\\//\\ 1 branc, brances, 4 brances, of lengt of lengt of lengt 1 Te double lnes sow te egt count. So te sum, S, s as follows: S = = 0 0 1 ( ) = + ( 1) + ( ) +... + + 1 1+ 0 We can use a neat trck and compute S = S S as follows: S= 1 () + (-1) + (-) +... + (-) () + (-1) () + -S= - 0 () - 1 (-1) - (-) - (-) -... - (-) () - (-1) (1) - 0 ------------------------------------------------------------------------- S= - + 1 + * + * +... + (-) (-) + (-1) + - * 1 - * \ / \ / = =... Subtractng term by term, te terms wt '' drop out and a power of s left. Ten subtract and add one, so our summaton starts at zero: S S = S = + + + +... + = + = 1 = 1+ = 0 Now, usng a well known formula: = 0 = + 1 1 proves te teorem: S = 1+ + 1 1 = O( N) Heap sort A fnal note tat s wort mentonng s tat we can easly sort usng a eap, by buldng te eap and repeatedly deletng te mnmum value. We use te delete operaton, but store te deleted value at te end of te array. Interestng, ts yelds an algortm tat s O(n log n) n te worst case (unlke qucksort/oaresort) and tat doesn t requre extra memory (unlke mergesort). Qucksort s stll used most often n practce. Page 7 of 7