CS3000:&Algorithms&&&Data Jonathan&Ullman - - PowerPoint PPT Presentation

cs3000 algorithms data jonathan ullman
SMART_READER_LITE
LIVE PREVIEW

CS3000:&Algorithms&&&Data Jonathan&Ullman - - PowerPoint PPT Presentation

CS3000:&Algorithms&&&Data Jonathan&Ullman Lecture&18:& Greedy&Algorithms:&Proof&Techniques March&30,&2020 Obligatory& Wall$Street$ Quotation The&movie& Wall$Street


slide-1
SLIDE 1

CS3000:&Algorithms&&&Data Jonathan&Ullman

Lecture&18:&

  • Greedy&Algorithms:&Proof&Techniques

March&30,&2020

slide-2
SLIDE 2

Obligatory&Wall$Street$Quotation

The&movie&Wall$Street,&however,&is&not.

slide-3
SLIDE 3

Greedy&Algorithms

  • What’s&a&greedy&algorithm?
  • I&know&it&when&I&see&it
  • Roughly,&an&algorithm&that&builds&a&solution&myopically&

and&never&looks&back&(compare&to&DP)

  • Typically,&make&a&single&pass&over&the&input&(e.g.&Kruskal)
  • Why&care&about&greedy&algorithms?
  • Greedy&algorithms&are&the&fastest&and&simplest&

algorithms&imaginable,&and&sometimes&they&work!

  • Sometimes&make&useful&heuristics&when&they&don’t
  • Simplicity&makes&them&easy&to&adapt&to&different&models
slide-4
SLIDE 4

Interval&Scheduling

slide-5
SLIDE 5

(Weighted)&Interval&Scheduling

  • Input:&! intervals& "#, %

# with&values&&#

  • Output: a&compatible&schedule&' with&the&largest&

possible&total&value

  • A&schedule&is&a&subset&of&intervals&' ⊆ {1, … ,!}
  • A&schedule&' is compatible&if&no&two&-, . ∈ ' overlap
  • The&total&value&of&' is&∑

&#

#∈1

slide-6
SLIDE 6

(Unweighted)&Interval&Scheduling

  • Input:&! intervals& "#, %

#

  • Output: a&compatible&schedule&' with&the&largest&

possible&size

  • A&schedule&is&a&subset&of&intervals&' ⊆ {1, … ,!}
  • A&schedule&' is compatible&if&no&two&-, . ∈ ' overlap

O

O

A compatible subset of

size

3

slide-7
SLIDE 7

Possibly&Greedy&Rules

  • Choose&the&shortest&interval&first
  • Choose&the&interval&with&earliest&start&first
  • Choose&the&interval&with&earliest&finish&first

2 3

OP 1

2

GREEDY

_I

2

C

37

y

s OP 1

4

GREED

L

s c

30

27

slide-8
SLIDE 8

Greedy&Algorithm:&Earliest&Finish&First

  • Sort&intervals&so&that&%

2 ≤ % 4 ≤ ⋯ ≤ % 6

  • Let&' be&empty
  • For&- = 1, …, !:
  • If&interval&- doesn’t&create&a&conflict,&add&- to&'
  • Return&'
slide-9
SLIDE 9

Greedy&Stays&Ahead

  • How&do&we&know&we&found&an&optimal&schedule
  • “Greedy&Stays&Ahead” strategy
  • We’ll&show&that&at&every&point&in&time,&the&greedy&

schedule&does&better&than&any&other&schedule

Proof by Induction

f s

  • io

a

  • i

Purple

greedy

Red

some other schedule

slide-10
SLIDE 10

Greedy&Stays&Ahead

  • Let&8 = -2, … , -9 be&greedy’s&schedule
  • Let&: = {.2, … , .;} be&some&optimal&schedule
  • Key$Claim: for&every&< = 1, … , =,&%

#> ≤ % ?>

O

e x

G 1,3 5,8

it

0 9214,7

i

3

9 is

5

j

2

ice

8

g2

4

jig _7

jy 9

O

O

00ns

rosco

O

00

fi Efj

fiah fjafizh

fj.fi

Efg

slide-11
SLIDE 11

Greedy&Stays&Ahead

  • Let&8 = -2, … , -9 be&greedy’s&schedule
  • Let&: = {.2, … , .;} be&some&optimal&schedule
  • Key$Claim: for&every&< = 1, … , =,&%

#> ≤ % ?>

O

Claim Greedy is optimal

suppose

s r

Ther

Sjr

fjr

fir

greedy wouldalso choose

O

O

00ns

As

s

r

fi Efj

fi Ifj

fishfj

fig Efg

would alsobe chosenby greedy

slide-12
SLIDE 12

Greedy&Stays&Ahead

  • Let&8 = -2, … , -9 be&greedy’s&schedule
  • Let&: = {.2, … , .;} be&some&optimal&schedule
  • Key$Claim: for&every&< = 1, … , =,&%

#> ≤ % ?>

Proofbylnduction

Base case

fi

e f

f'IIIII.tnI dHntiD

Inductive Step

If

feet fj

then fit

fj.ie

ie

IT IT

e

itt

je IT

IS

Jet

slide-13
SLIDE 13

Greedy&Stays&Ahead

  • Let&8 = -2, … , -9 be&greedy’s&schedule
  • Let&: = {.2, … , .;} be&some&optimal&schedule
  • Key$Claim: for&every&< = 1, … , =,&%

#> ≤ % ?>

g

y

subtle pairs

i

arguing why this picture is impossible

Proofoflnductpoo

If

fit E fit

ther

fit t

E fj

sun

in

D

in

I

l

TT

l

17

I

1

Greedy would have considered jet before

it

andchosen it

slide-14
SLIDE 14

Minimum&Lateness&Scheduling

slide-15
SLIDE 15

Minimum&Lateness&Scheduling

  • Input:&! jobs&with&length <# and&deadline @#
  • Simplifying&assumption:&all&deadlines&are&distinct
  • Output: a&minimum_lateness&schedule&for&the&jobs
  • Can&only&do&one&job&at&a&time,&no&overlap
  • The&lateness&of&job&- is&max %

# − @#, 0

  • The&lateness&of&a&schedule is&max

#

max %

# − @#,0

Csi f

sa fa S3f

I no breaks

The

schedule

you

red

This schedhas

0 lateness in

j

job3

slide-16
SLIDE 16

Possible&Greedy&Rules

  • Choose&the&shortest&job&first&(min <#)?
  • Choose&the&most&urgent&job&first&(min @# − <#)?

1

Jobs

L

4

1

f d

20

Job2

12 10

dz

to

1 2

IMB

Hd

Ida

JobI

ti't

di

2

Job 2

ta

lo

j da

lo

O

I

101

11

11111442444444346427

lateness

9

dz

d's

slide-17
SLIDE 17

Greedy&Algorithm:&Earliest&Deadline&First

  • Sort&jobs&so&that&@2 ≤ @4 ≤ ⋯ ≤ @6
  • For&- = 1, …, !:
  • Schedule&job&- right&after&job&- − 1Hfinishes

Jobs

4

1

d

20

1

i

e

Job2

D 12 10

dz

10

Greedy would do

2

then

L

w

lateness

Job 1 C 4 1

di

2

Job 2

tz

lo

j da _10

Greely

would

choose 2 then 2

ul lateness

2

slide-18
SLIDE 18

Exchange&Argument

  • 8 =&greedy&schedule,&: =&optimal&schedule
  • Exchange&Argument:
  • We&can&transform&: to&8 by&exchanging&pairs&of&jobs
  • Each&exchange&only&reduces&the&lateness&of&:
  • Therefore&the&lateness&of&8 is&at&most&that&of&:

G O

L

S

S

slide-19
SLIDE 19

Exchange&Argument

  • 8 =&greedy&schedule,&: =&optimal&schedule
  • Observation:&the&optimal&schedule&has&no&gaps
  • A&schedule&is&just&an&ordering&of&the&jobs,&with&jobs&

scheduled&back_to_back

slide-20
SLIDE 20

Exchange&Argument

  • 8 =&greedy&schedule,&: =&optimal&schedule
  • We&say&that&two&jobs&-, . are&inverted in&: if&&&&&

@# < @? but&. comes&before&-

  • Observation:&greedy&has&no&inversions

Inversion

g

di

Inveaonwl

consecutivejobs

minus

f

Idi

slide-21
SLIDE 21

Exchange&Argument

  • We&say&that&two&jobs&-, . are&inverted in&: if&&&&&

@# < @? but&. comes&before&-

  • Claim:&the&optimal&schedule&has&no&inversions
  • Step&1:&suppose&: has&an&inversion,&then&it&has&an&

inversion&-, . where&-, . are&consecutive

Alternative Form If

a schedule has

inversions

then there is a

schedule

that

is at

least

as good

without

inversions

slide-22
SLIDE 22

Exchange&Argument

  • We&say&that&two&jobs&-, . are&inverted in&: if&&&&&

@# < @? but&. comes&before&-

  • Claim:&the&optimal&schedule&has&no&inversions
  • Step&1:&suppose&: has&an&inversion,&then&it&has&an&

inversion&-, . where&-, . are&consecutive

  • Step&2:&if&-, . are&a&consecutive&jobs&that&are&inverted&

then&flipping&them&only&reduces&the&lateness

latenessof E

y

dj

t

Kitt

titty di

II aging

I

µ Exchange

latenessofj

I

l

Litt

dj

titer

di

I

it

i

ti

t.tt

di

slide-23
SLIDE 23

Exchange&Argument

  • If&-, . are&a&consecutive&jobs&that&are&inverted&then&

flipping&them&only&reduces&the&lateness

latenessof es

de 1dg

t

Kitt

titty di

i

i

l

l

latenessofj

i

i

µ Exchange

1

I

1

Litt

dj

titer

di

I

titt

i

ti

e

ti di

slide-24
SLIDE 24

Exchange&Argument

  • We&say&that&two&jobs&-, . are&inverted in&: if&&&&&

@# < @? but&. comes&before&-

  • Claim:&the&optimal&schedule&has&no&inversions
  • Step&1:&suppose&: has&an&inversion,&then&it&has&an&

inversion&-, . where&-, . are&consecutive

  • Step&2:&if&-, . are&a&consecutive&jobs&that&are&inverted&

then&flipping&them&only&reduces&the&lateness

  • 8 is&the&unique&schedule&with&no&inversions,&: is&

the&unique&schedule&with&no&inversions,&8 = :