priority queue ADT heaps 1

Similar documents
Searching Linked Lists. Perfect Skip List. Building a Skip List. Skip List Analysis (1) Assume the list is sorted, but is stored in a linked list.

Final Exam Solutions

Aim To manage files and directories using Linux commands. 1. file Examines the type of the given file or directory

Homework #3. 1 x. dx. It therefore follows that a sum of the

Abstract Interpretation: concrete and abstract semantics

1 Minimum Cut Problem

Search sequence databases 3 10/25/2016

Examples and applications on SSSP and MST

Propositional Logic. Combinatorial Problem Solving (CPS) Albert Oliveras Enric Rodríguez-Carbonell. May 17, 2018

Strongly Connected Components

Basic Polyhedral theory

Chapter 8: Electron Configurations and Periodicity

10. EXTENDING TRACTABILITY

From Elimination to Belief Propagation

Fourier Transforms and the Wave Equation. Key Mathematics: More Fourier transform theory, especially as applied to solving the wave equation.

Linked-List Implementation. Linked-lists for two sets. Multiple Operations. UNION Implementation. An Application of Disjoint-Set 1/9/2014

Chapter 6 Folding. Folding

Final Exam : Solutions

Worksheet: Taylor Series, Lagrange Error Bound ilearnmath.net

NEW APPLICATIONS OF THE ABEL-LIOUVILLE FORMULA

Basic Logic Review. Rules. Lecture Roadmap Combinational Logic. Textbook References. Basic Logic Gates (2-input versions)

Hydrogen Atom and One Electron Ions

CPSC 665 : An Algorithmist s Toolkit Lecture 4 : 21 Jan Linear Programming

Minimum Spanning Trees

Solution of Assignment #2

Random Access Techniques: ALOHA (cont.)

EXST Regression Techniques Page 1

Roadmap. XML Indexing. DataGuide example. DataGuides. Strong DataGuides. Multiple DataGuides for same data. CPS Topics in Database Systems

Function Spaces. a x 3. (Letting x = 1 =)) a(0) + b + c (1) = 0. Row reducing the matrix. b 1. e 4 3. e 9. >: (x = 1 =)) a(0) + b + c (1) = 0

1 Isoparametric Concept

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 259] B-Trees

Higher-Order Discrete Calculus Methods

Computing and Communications -- Network Coding

Minimum Spanning Trees

Priority queues implemented via heaps

A=P=E M-A=N Alpha particle Beta Particle. Periodic table

Title: Vibrational structure of electronic transition

Approximate Maximum Flow in Undirected Networks by Christiano, Kelner, Madry, Spielmann, Teng (STOC 2011)

COMPUTER GENERATED HOLOGRAMS Optical Sciences 627 W.J. Dallas (Monday, April 04, 2005, 8:35 AM) PART I: CHAPTER TWO COMB MATH.

5.62 Physical Chemistry II Spring 2008

UNTYPED LAMBDA CALCULUS (II)

EECE 301 Signals & Systems Prof. Mark Fowler

Unit 6: Solving Exponential Equations and More

COUNTING TAMELY RAMIFIED EXTENSIONS OF LOCAL FIELDS UP TO ISOMORPHISM

First order differential equation Linear equation; Method of integrating factors

Einstein Equations for Tetrad Fields

Sundials and Linear Algebra

Middle East Technical University Department of Mechanical Engineering ME 413 Introduction to Finite Element Analysis

Chapter Finding Small Vertex Covers. Extending the Limits of Tractability. Coping With NP-Completeness. Vertex Cover

MAE4700/5700 Finite Element Analysis for Mechanical and Aerospace Design

u r du = ur+1 r + 1 du = ln u + C u sin u du = cos u + C cos u du = sin u + C sec u tan u du = sec u + C e u du = e u + C

The second condition says that a node α of the tree has exactly n children if the arity of its label is n.

Two Products Manufacturer s Production Decisions with Carbon Constraint

Quasi-Classical States of the Simple Harmonic Oscillator

Junction Tree Algorithm 1. David Barber

Exponential Functions

Recounting the Rationals

a g f 8 e 11 Also: Minimum degree, maximum degree, vertex of degree d 1 adjacent to vertex of degree d 2,...

September 23, Honors Chem Atomic structure.notebook. Atomic Structure

Pipe flow friction, small vs. big pipes

MA 262, Spring 2018, Final exam Version 01 (Green)

λ = 2L n Electronic structure of metals = 3 = 2a Free electron model Many metals have an unpaired s-electron that is largely free

u x v x dx u x v x v x u x dx d u x v x u x v x dx u x v x dx Integration by Parts Formula

On spanning trees and cycles of multicolored point sets with few intersections

COMP 250. Lecture 24. heaps 2. Nov. 3, 2017

Derangements and Applications

Week 3: Connected Subgraphs

orbiting electron turns out to be wrong even though it Unfortunately, the classical visualization of the

First derivative analysis

5.80 Small-Molecule Spectroscopy and Dynamics

Brief Introduction to Statistical Mechanics

Gradebook & Midterm & Office Hours

CPS 616 W2017 MIDTERM SOLUTIONS 1

Exam 1. It is important that you clearly show your work and mark the final answer clearly, closed book, closed notes, no calculator.

Elements of Statistical Thermodynamics

Nuclear reactions The chain reaction

APPENDIX: STATISTICAL TOOLS

MCE503: Modeling and Simulation of Mechatronic Systems Discussion on Bond Graph Sign Conventions for Electrical Systems

cycle that does not cross any edges (including its own), then it has at least

3-2-1 ANN Architecture

Introduction to Arithmetic Geometry Fall 2013 Lecture #20 11/14/2013

8-node quadrilateral element. Numerical integration

Construction of asymmetric orthogonal arrays of strength three via a replacement method

Classical Magnetic Dipole

Prod.C [A] t. rate = = =

Give the letter that represents an atom (6) (b) Atoms of A and D combine to form a compound containing covalent bonds.

Deift/Zhou Steepest descent, Part I

MCB137: Physical Biology of the Cell Spring 2017 Homework 6: Ligand binding and the MWC model of allostery (Due 3/23/17)

Addition of angular momentum

Need to understand interaction of macroscopic measures

Statistical Thermodynamics: Sublimation of Solid Iodine

(1) Then we could wave our hands over this and it would become:

CHAPTER 16 HW: CONJUGATED SYSTEMS

4.5 Minimum Spanning Tree. Chapter 4. Greedy Algorithms. Minimum Spanning Tree. Applications

Random Process Part 1

Outline. Thanks to Ian Blockland and Randy Sobie for these slides Lifetimes of Decaying Particles Scattering Cross Sections Fermi s Golden Rule

READING ASSIGNMENTS. Signal Processing First. Problem Solving Skills LECTURE OBJECTIVES. x(t) = cos(αt 2 ) Fourier Series ANALYSIS.

SCHUR S THEOREM REU SUMMER 2005

6.1 Integration by Parts and Present Value. Copyright Cengage Learning. All rights reserved.

Learning Spherical Convolution for Fast Features from 360 Imagery

Transcription:

COMP 250 Lctur 23 priority quu ADT haps 1 Nov. 1/2, 2017 1

Priority Quu Li a quu, but now w hav a mor gnral dinition o which lmnt to rmov nxt, namly th on with highst priority..g. hospital mrgncy room Assum a st o comparabl lmnts or ys. 2

Priority Quu ADT add(lmnt) rmovmin() highst priority = numbr 1 priority p() contains(lmnt) rmov(lmnt) 3

How to implmnt a Priority Quu? sortd list? binary sarch tr (last lctur)? balancd binary sarch tr (COMP 251)? hap (nxt 3 lcturs) Not th sam hap you har about in COMP 206. 4

Complt Binary Tr (dinition) c m a g j d j d Binary tr o hight h such that vry lvl lss than h is ull, and all nods at lvl h ar as ar to th lt as possibl 5

min Hap (dinition) a b l u m Complt binary tr with uniqu comparabl lmnts, such that ach nod s lmnt is lss than its childrn s lmnt. 6

Hap.add(lmnt).g. add( c ) a b l u m 7

Hap.add(lmnt).g. add( c ) a b l u m? 8

Hap.add(lmnt).g. add( c ) a b l u m c Problm : adding at th nxt availabl slot typically will dstroy th hap proprty. 9

W swap c with its parnt. Q: Can this crat a problm with c s ormr sibling, who is now c s child? a b c l u m 10

W swap c with its parnt. Q: Can this crat a problm with c s ormr sibling, who is now c s child? A: No. Bcaus c < and < m. Thus, c < m. a b c l u m 11

Q: Ar w don? A: Not ncssarily. What about c s parnt? a b c l u m 12

W swap c with its (nw) parnt. Now w ar don bcaus c is gratr than its parnt a a c b l u m 13

Hap.add(lmnt) add( lmnt ){ cur = nw nod at nxt availabl la position cur.lmnt = lmnt whil (cur!= root) and (cur.lmnt < cur.parnt.lmnt){ swapelmnt(cur, parnt) cur = cur.parnt } } 14

Hap.add(lmnt) add( lmnt ){ cur = nw nod at nxt availabl la position cur.lmnt = lmnt whil (cur!= root) and (cur.lmnt < cur.parnt.lmnt){ swapelmnt(cur, parnt) cur = cur.parnt } } 15

Hap.add(lmnt) add( lmnt ){ cur = nw nod at nxt availabl la position cur.lmnt = lmnt whil (cur!= root) and (cur.lmnt < cur.parnt.lmnt){ swapelmnt(cur, parnt) // argumnts ar nods cur = cur.parnt } } 16

How to build a hap? add( ) add( ) add( ) add( a ) add( g ) 17

How to build a hap? add( ) add( ) add( ) add( a ) 18

How to build a hap? add( ) add( ) add( ) add( a ) add( g ) 19

How to build a hap? add( ) add( ) add( ) add( a ) a add( g ) 20

How to build a hap? add( ) add( ) add( ) add( a ) add( g ) g a 21

This mthod o building a hap is slow. I will show you a astr mthod two lcturs rom now. 22

Hap.rmovMin() rturns root lmnt a c b l u m 23

rmovmin() a c b l u m 24

rmovmin() Claim: i th root has two childrn, thn th nw root will b gratr than at last on o its childrn. Why? c b a How to solv this problm? m l u 25

Swap lmnts with smallr child. rmovmin() b a c l u Kp swapping with smallr child, i ncssary. m 26

Lt s do it again. rmovmin() b a c l u m 27

Lt s do it again. rmovmin() b b c l u m 28

rmovmin() Now swap with smallr child, i ncssary, to prsrv hap proprty. m b c l u 29

rmovmin() c b Kp swapping with smallr child, i ncssary. m l u 30

rmovmin() c b m l u 31

rmovmin(){ tmp = root.lmnt rmov last la nod and put its lmnt into th root cur = root whil ((cur has at last on child) and ( (cur.lmnt > cur.lt.lmnt) or (cur has right child and cur.lmnt > cur.right.lmnt)) ) { minchild = child with th smallr lmnt swapelmnt(cur, minchild) cur = minchild } rturn tmp } 32

rmovmin(){ tmp = root.lmnt rmov last la nod and put its lmnt into th root cur = root whil ( (cur has a lt child) and ( (cur.lmnt > cur.lt.lmnt) or (cur has right child and cur.lmnt > cur.right.lmnt)) ) { minchild = child with th smallr lmnt swapelmnt(cur, minchild) cur = minchild } rturn tmp } 33

rmovmin(){ tmp = root.lmnt rmov last la nod and put its lmnt into th root cur = root whil ( (cur has a lt child) and ( (cur.lmnt > cur.lt.lmnt) or (cur has right child and cur.lmnt > cur.right.lmnt)) ) { minchild = child with th smallr lmnt swapelmnt(cur, minchild) cur = minchild } rturn tmp } 34

add(lmnt) rmovmin() uphap downhap 35

Q: What about rmov(lmnt)? 36

Q: What about rmov(lmnt)? A: Worst cas Θ(n) Bst cas (not discussd) 37

Hap (array implmntation) 1 c 2 3 m 4 5 6 7 a g j Not usd d j d d d d 8 9 10 11 12 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 38

1 a 2 3 b 4 5 6 7 l u Not usd m g n q w z 8 9 10 11 12 13 a b l u m g n q w z 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 39

Nxt two lcturs writ add(lmnt) and rmovmin() using array indics bst and worst cas astr algorithm or building a hap 40