Greedy Algorithms. Greedy Algorithms. Piyush Kumar

Similar documents
Algorithm Design and Analysis

Algorithm Design and Analysis

4. GREEDY ALGORITHMS I

4.1 Interval Scheduling. Chapter 4. Greedy Algorithms. Interval Scheduling: Greedy Algorithms. Interval Scheduling. Interval scheduling.

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1

CS 491G Combinatorial Optimization Lecture Notes

CSE 332. Sorting. Data Abstractions. CSE 332: Data Abstractions. QuickSort Cutoff 1. Where We Are 2. Bounding The MAXIMUM Problem 4

Chapter Interval Scheduling. Greedy Algorithms. What is a Greedy Algorithm? Interval Scheduling

Trigonometry. Trigonometry. Curriculum Ready ACMMG: 223, 224, 245.

Abhilasha Classes Class- XII Date: SOLUTION (Chap - 9,10,12) MM 50 Mob no

Trigonometry. Trigonometry. Solutions. Curriculum Ready ACMMG: 223, 224, 245.

10. AREAS BETWEEN CURVES

Chapter Newton-Raphson Method of Solving a Nonlinear Equation

Partially Observable Systems. 1 Partially Observable Markov Decision Process (POMDP) Formalism

Factorising FACTORISING.

Rank One Update And the Google Matrix by Al Bernstein Signal Science, LLC

Lecture 6: Coding theory

Project 6: Minigoals Towards Simplifying and Rewriting Expressions

Chapter Newton-Raphson Method of Solving a Nonlinear Equation

2.4 Linear Inequalities and Interval Notation

1 PYTHAGORAS THEOREM 1. Given a right angled triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides.

Improper Integrals. The First Fundamental Theorem of Calculus, as we ve discussed in class, goes as follows:

Designing Information Devices and Systems I Discussion 8B

Review of Gaussian Quadrature method

Area and Perimeter. Area and Perimeter. Solutions. Curriculum Ready.

Dennis Bricker, 2001 Dept of Industrial Engineering The University of Iowa. MDP: Taxi page 1

Welcome. Balanced search trees. Balanced Search Trees. Inge Li Gørtz

First Midterm Examination

CARLETON UNIVERSITY. 1.0 Problems and Most Solutions, Sect B, 2005

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

VECTORS VECTORS VECTORS VECTORS. 2. Vector Representation. 1. Definition. 3. Types of Vectors. 5. Vector Operations I. 4. Equal and Opposite Vectors

Lecture 7 Circuits Ch. 27

Now we must transform the original model so we can use the new parameters. = S max. Recruits

CS 2204 DIGITAL LOGIC & STATE MACHINE DESIGN SPRING 2014

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

CS 573 Automata Theory and Formal Languages

For a, b, c, d positive if a b and. ac bd. Reciprocal relations for a and b positive. If a > b then a ab > b. then

Arrow s Impossibility Theorem

22: Union Find. CS 473u - Algorithms - Spring April 14, We want to maintain a collection of sets, under the operations of:

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

Lecture 2e Orthogonal Complement (pages )

Variable time amplitude amplification and quantum algorithms for linear algebra. Andris Ambainis University of Latvia

Data Compression Techniques (Spring 2012) Model Solutions for Exercise 4

Instructions. An 8.5 x 11 Cheat Sheet may also be used as an aid for this test. MUST be original handwriting.

Preview 11/1/2017. Greedy Algorithms. Coin Change. Coin Change. Coin Change. Coin Change. Greedy algorithms. Greedy Algorithms

Numbers and indices. 1.1 Fractions. GCSE C Example 1. Handy hint. Key point

5.4, 6.1, 6.2 Handout. As we ve discussed, the integral is in some way the opposite of taking a derivative. The exact relationship

The DOACROSS statement

ME306 Dynamics, Spring HW1 Solution Key. AB, where θ is the angle between the vectors A and B, the proof

Regular expressions, Finite Automata, transition graphs are all the same!!

Continuous Random Variables Class 5, Jeremy Orloff and Jonathan Bloom

How do we solve these things, especially when they get complicated? How do we know when a system has a solution, and when is it unique?

Solutions to Problem Set #1

Linear Inequalities. Work Sheet 1

Lecture 3 ( ) (translated and slightly adapted from lecture notes by Martin Klazar)

MORE FUNCTION GRAPHING; OPTIMIZATION. (Last edited October 28, 2013 at 11:09pm.)

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

p-adic Egyptian Fractions

Logic, Set Theory and Computability [M. Coppenbarger]

Definite Integrals. The area under a curve can be approximated by adding up the areas of rectangles = 1 1 +

D. Harel, Statecharts: A visual formalism for complex systems, Science of Computer Programming 8, 1987, pp

NON-DETERMINISTIC FSA

If we have a function f(x) which is well-defined for some a x b, its integral over those two values is defined as

April 8, 2017 Math 9. Geometry. Solving vector problems. Problem. Prove that if vectors and satisfy, then.

( x) ( ) takes at the right end of each interval to approximate its value on that

Improper Integrals. Introduction. Type 1: Improper Integrals on Infinite Intervals. When we defined the definite integral.

I1 = I2 I1 = I2 + I3 I1 + I2 = I3 + I4 I 3

Nondeterminism and Nodeterministic Automata

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

Data Structures LECTURE 10. Huffman coding. Example. Coding: problem definition

Applied Statistics Qualifier Examination

10 Vector Integral Calculus

(e) if x = y + z and a divides any two of the integers x, y, or z, then a divides the remaining integer

New Algorithms: Linear, Nonlinear, and Integer Programming

Minimum Spanning Trees

IMPOSSIBLE NAVIGATION

332:221 Principles of Electrical Engineering I Fall Hourly Exam 2 November 6, 2006

DCDM BUSINESS SCHOOL NUMERICAL METHODS (COS 233-8) Solutions to Assignment 3. x f(x)

Solutions for HW9. Bipartite: put the red vertices in V 1 and the black in V 2. Not bipartite!

Properties of Integrals, Indefinite Integrals. Goals: Definition of the Definite Integral Integral Calculations using Antiderivatives

UNIVERSITY OF IOANNINA DEPARTMENT OF ECONOMICS. M.Sc. in Economics MICROECONOMIC THEORY I. Problem Set II

Non Right Angled Triangles

4 7x =250; 5 3x =500; Read section 3.3, 3.4 Announcements: Bell Ringer: Use your calculator to solve

Basic Derivative Properties

Bases for Vector Spaces

Section 1.3 Triangles

Section 2.3. Matrix Inverses

Arrow s Impossibility Theorem

Section 4: Integration ECO4112F 2011

AP CALCULUS Test #6: Unit #6 Basic Integration and Applications

u( t) + K 2 ( ) = 1 t > 0 Analyzing Damped Oscillations Problem (Meador, example 2-18, pp 44-48): Determine the equation of the following graph.

Surds and Indices. Surds and Indices. Curriculum Ready ACMNA: 233,

Homework 3 Solutions

Fault Modeling. EE5375 ADD II Prof. MacDonald

5.1 Estimating with Finite Sums Calculus

Bridging the gap: GCSE AS Level

The Schur-Cohn Algorithm

Section 6: Area, Volume, and Average Value

Transcription:

Gree s goo. Gree s rght. Gree works. Gree lrfes, uts through, n ptures the essene of the evolutonry sprt. - Goron Geko (Mhel Dougls) Anlyss of Algorthms Pyush Kumr (Leture 4: Greey Algorthms) Welome to 453 oure: K. Wyne, Greey Algorthms Optmzton prolem: Mn/Mx n oetve. Mnmze the totl length of spnnng tree. Mnmze the sze of fle usng ompresson (The mother of ll prolems) Greey Algorthm Attempt to o est t eh step wthout onserton of future onserton For some prolems, Lolly optml hoe les to glol opt. Follows Gree s goo phlosophy Requres Optml ustruture Wht exmples hve we lrey seen? Greey Algorthms For some prolems, Gree s goo works. For some, t fns goo soluton whh s not glol opt Heursts Approxmton Algorthms For some, t n o very.

Prolem of Chnge Venng mhne hs qurters, nkels, pennes n mes. Nees to return N ents hnge. Wnte: An lgorthm to return the N ents n mnmum numer of ons. Wht o we o? 4. Intervl heulng 5 Intervl heulng Intervl sheulng. Jo strts t s n fnshes t f. Two os omptle f they on't overlp. Gol: fn mxmum suset of mutully omptle os. e f g 0 3 4 5 6 7 8 9 0 h Tme

Intervl heulng: Greey Algorthms Greey templte. Conser os n some orer. Tke eh o prove t's omptle wth the ones lrey tken. [Erlest strt tme] Conser os n senng orer of strt tme s. [Erlest fnsh tme] Conser os n senng orer of fnsh tme f. [hortest ntervl] Conser os n senng orer of ntervl length f - s. [Fewest onflts] For eh o, ount the numer of onfltng os. heule n senng orer of onflts. Intervl heulng: Greey Algorthms Greey templte. Conser os n some orer. Tke eh o prove t's omptle wth the ones lrey tken. reks erlest strt tme reks shortest ntervl reks fewest onflts Intervl heulng: Greey Algorthm Greey lgorthm. Conser os n nresng orer of fnsh tme. Tke eh o prove t's omptle wth the ones lrey tken. ort os y fnsh tmes so tht f f... f n. os selete A for = to n { f (o omptle wth A) A A {} } return A Implementton. O(n log n). Rememer o * tht ws e lst to A. Jo s omptle wth A f s f *. 3

Intervl heulng: Anlyss Theorem. Greey lgorthm s optml. Pf. (y ontrton) Assume greey s not optml, n let's see wht hppens. Let,,... k enote set of os selete y greey. Let,,... m enote set of os n the optml soluton wth =, =,..., r = r for the lrgest possle vlue of r. o r+ fnshes efore r+ Greey: r r+ OPT: r r+... why not reple o r+ wth o r+? Intervl heulng: Anlyss Theorem. Greey lgorthm s optml. Pf. (y ontrton) Assume greey s not optml, n let's see wht hppens. Let,,... k enote set of os selete y greey. Let,,... m enote set of os n the optml soluton wth =, =,..., r = r for the lrgest possle vlue of r. o r+ fnshes efore r+ Greey: r r+ OPT: r r+... soluton stll fesle n optml, ut ontrts mxmlty of r. 4. Intervl Prttonng 4

Intervl Prttonng Intervl prttonng. Leture strts t s n fnshes t f. Gol: fn mnmum numer of lssrooms to sheule ll letures so tht no two our t the sme tme n the sme room. Ex: Ths sheule uses 4 lssrooms to sheule 0 letures. e g h f 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 4 4:30 Tme Intervl Prttonng Intervl prttonng. Leture strts t s n fnshes t f. Gol: fn mnmum numer of lssrooms to sheule ll letures so tht no two our t the sme tme n the sme room. Ex: Ths sheule uses only 3. f g e h 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 4 4:30 Tme Intervl Prttonng: Lower Boun on Optml oluton Def. The epth of set of open ntervls s the mxmum numer tht ontn ny gven tme. Key oservton. Numer of lssrooms neee epth. Ex: Depth of sheule elow = 3 sheule elow s optml.,, ll ontn 9:30 Q. Does there lwys exst sheule equl to epth of ntervls? f g e h 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 4 4:30 Tme 5

Intervl Prttonng: Greey Algorthm Greey lgorthm. Conser letures n nresng orer of strt tme: ssgn leture to ny omptle lssroom. ort ntervls y strtng tme so tht s s... s n. 0 numer of llote lssrooms for = to n { f (leture s omptle wth some lssroom k) sheule leture n lssroom k else llote new lssroom + sheule leture n lssroom + + } Implementton. O(n log n). For eh lssroom k, mntn the fnsh tme of the lst o e. Keep the lssrooms n prorty queue. Intervl Prttonng: Greey Anlyss Oservton. Greey lgorthm never sheules two nomptle letures n the sme lssroom. Theorem. Greey lgorthm s optml. Pf. Let = numer of lssrooms tht the greey lgorthm llotes. Clssroom s opene euse we neee to sheule o, sy, tht s nomptle wth ll - other lssrooms. ne we sorte y strt tme, ll these nomptltes re use y letures tht strt no lter thn s. Thus, we hve letures overlppng t tme s +. Key oservton ll sheules use lssrooms. 4. heulng to Mnmze Lteness 8 6

heulng to Mnmzng Lteness Mnmzng lteness prolem. ngle resoure proesses one o t tme. Jo requres t unts of proessng tme n s ue t tme. If strts t tme s, t fnshes t tme f = s + t. Lteness: = mx { 0, f - }. Gol: sheule ll os to mnmze mxmum lteness L = mx. Ex: 3 4 5 6 t 3 4 3 6 8 9 9 4 5 lteness = lteness = 0 mx lteness = 6 3 = 9 = 8 6 = 5 = 6 5 = 4 4 = 9 0 3 4 5 6 7 8 9 0 3 4 5 Mnmzng Lteness: Greey Algorthms Greey templte. Conser os n some orer. [hortest proessng tme frst] Conser os n senng orer of proessng tme t. [Erlest elne frst] Conser os n senng orer of elne. [mllest slk] Conser os n senng orer of slk - t. Mnmzng Lteness: Greey Algorthms Greey templte. Conser os n some orer. [hortest proessng tme frst] Conser os n senng orer of proessng tme t. t 0 ounterexmple 00 0 [mllest slk] Conser os n senng orer of slk - t. t 0 0 ounterexmple 7

Mnmzng Lteness: Greey Algorthm Greey lgorthm. Erlest elne frst. ort n os y elne so tht n t 0 for = to n Assgn o to ntervl [t, t + t ] s t, f t + t t t + t output ntervls [s, f ] mx lteness = = 6 = 8 3 = 9 4 = 9 5 = 4 6 = 5 4 6 8 4 0 3 5 7 9 0 3 5 Mnmzng Lteness: No Ile Tme Oservton. There exsts n optml sheule wth no le tme. = 4 = 6 0 3 4 5 6 = 7 8 9 0 = 4 = 6 = 6 7 0 3 4 5 8 9 0 Oservton. The greey sheule hs no le tme. Mnmzng Lteness: Inversons Def. An nverson n sheule s pr of os n suh tht: < ut sheule efore. nverson efore swp Oservton. Greey sheule hs no nversons. Oservton. If sheule (wth no le tme) hs n nverson, t hs one wth pr of nverte os sheule onseutvely. 8

Mnmzng Lteness: Inversons Def. An nverson n sheule s pr of os n suh tht: < ut sheule efore. nverson f efore swp fter swp f' Clm. wppng two ent, nverte os reues the numer of nversons y one n oes not nrese the mx lteness. Pf. Let e the lteness efore the swp, n let ' e t fterwrs. ' k = k for ll k, ' If o s lte: = f (efnton) = f ( fnshes t tme f ) f ( ) (efnton) Mnmzng Lteness: Anlyss of Greey Algorthm Theorem. Greey sheule s optml. Pf. Defne * to e n optml sheule tht hs the fewest numer of nversons, n let's see wht hppens. Cn ssume * hs no le tme. If * hs no nversons, then = *. If * hs n nverson, let - e n ent nverson. swppng n oes not nrese the mxmum lteness n strtly ereses the numer of nversons ths ontrts efnton of * Greey Anlyss trteges Greey lgorthm stys he. how tht fter eh step of the greey lgorthm, ts soluton s t lest s goo s ny other lgorthm's. Exhnge rgument. Grully trnsform ny soluton to the one foun y the greey lgorthm wthout hurtng ts qulty. truturl. Dsover smple "struturl" oun ssertng tht every possle soluton must hve ertn vlue. Then show tht your lgorthm lwys heves ths oun. 9

4.3 Optml Chng 8 Optml Offlne Chng Chng. Che wth pty to store k tems. equene of m tem requests,,, m. Che ht: tem lrey n he when requeste. Che mss: tem not lrey n he when requeste: must rng requeste tem nto he, n evt some exstng tem, f full. Gol. Evton sheule tht mnmzes numer of he msses. Ex: k =, ntl he =, requests:,,,,,,,. Optml evton sheule: he msses. requests he Optml Offlne Chng: Frthest-In-Future Frthest-n-future. Evt tem n the he tht s not requeste untl frthest n the future. urrent he: e f future queres: g e e f e f g h... he mss eet ths one Theorem. [Belly, 960s] FF s optml evton sheule. Pf. Algorthm n theorem re ntutve; proof s sutle. 0

Reue Evton heules Def. A reue sheule s sheule tht only nserts n tem nto the he n step n whh tht tem s requeste. Intuton. Cn trnsform n unreue sheule nto reue one wth no more he msses. x x n unreue sheule reue sheule Reue Evton heules Clm. Gven ny unreue sheule, n trnsform t nto reue sheule ' wth no more he msses. oesn't enter he t requeste tme Pf. (y nuton on numer of unreue tems) uppose rngs nto the he t tme t, wthout request. Let e the tem evts when t rngs nto the he. Cse : evte t tme t', efore next request for. Cse : requeste t tme t' efore s evte. ' ' t t t t t' t' t' t' e evte t tme t', efore next request e requeste t tme t' Cse Cse Frthest-In-Future: Anlyss Theorem. FF s optml evton lgorthm. Pf. (y nuton on numer or requests ) Invrnt: There exsts n optml reue sheule tht mkes the sme evton sheule s FF through the frst + requests. Let e reue sheule tht stsfes nvrnt through requests. We proue ' tht stsfes nvrnt fter + requests. Conser (+) st request = +. ne n FF hve gree up untl now, they hve the sme he ontents efore request +. Cse : ( s lrey n the he). ' = stsfes nvrnt. Cse : ( s not n the he n n FF evt the sme element). ' = stsfes nvrnt.

Frthest-In-Future: Anlyss Pf. (ontnue) Cse 3: ( s not n the he; FF evts e; evts f e). egn onstruton of ' from y evtng e nste of f sme e f sme e f ' + sme e sme f now ' grees wth FF on frst + requests; we show tht hvng element f n he s no worse thn hvng element e ' Frthest-In-Future: Anlyss Let ' e the frst tme fter + tht n ' tke fferent ton, n let g e tem requeste t tme '. must nvolve e or f (or oth) ' sme e sme f ' Cse 3: g = e. Cn't hppen wth Frthest-In-Future sne there must e request for f efore e. Cse 3: g = f. Element f n't e n he of, so let e' e the element tht evts. f e' = e, ' esses f from he; now n ' hve sme he f e' e, ' evts e' n rngs e nto the he; now n ' hve the sme he Note: ' s no longer reue, ut n e trnsforme nto reue sheule tht grees wth FF through step + Frthest-In-Future: Anlyss Let ' e the frst tme fter + tht n ' tke fferent ton, n let g e tem requeste t tme '. must nvolve e or f (or oth) ' sme e sme f ' otherwse ' woul tke the sme ton Cse 3: g e, f. must evt e. Mke ' evt f; now n ' hve the sme he. ' sme g sme g '

Chng Perspetve Onlne vs. offlne lgorthms. Offlne: full sequene of requests s known pror. Onlne (relty): requests re not known n vne. Chng s mong most funmentl onlne prolems n C. LIFO. Evt pge rought n most reently. LRU. Evt pge whose most reent ess ws erlest. FF wth reton of tme reverse! Theorem. FF s optml offlne evton lgorthm. Proves ss for unerstnng n nlyzng onlne lgorthms. LRU s k-ompettve. [eton 3.8] LIFO s rtrrly. 3