CSC373 Week 2: Greedy Algorithms Nisarg Shah 373F19 - Nisarg Shah - - PowerPoint PPT Presentation

β–Ά
csc373 week 2 greedy algorithms nisarg shah
SMART_READER_LITE
LIVE PREVIEW

CSC373 Week 2: Greedy Algorithms Nisarg Shah 373F19 - Nisarg Shah - - PowerPoint PPT Presentation

CSC373 Week 2: Greedy Algorithms Nisarg Shah 373F19 - Nisarg Shah 1 Recap Divide & Conquer Master theorem Counting inversions in ( log ) Finding closest pair of points in 2 in log Fast


slide-1
SLIDE 1

CSC373 Week 2: Greedy Algorithms

373F19 - Nisarg Shah 1

Nisarg Shah

slide-2
SLIDE 2

Recap

373F19 - Nisarg Shah 2

  • Divide & Conquer

➒ Master theorem ➒ Counting inversions in 𝑃(π‘œ log π‘œ) ➒ Finding closest pair of points in ℝ2 in 𝑃 π‘œ log π‘œ ➒ Fast integer multiplication in 𝑃 π‘œlog2 3 ➒ Fast matrix multiplication in 𝑃 π‘œlog2 7 ➒ Finding π‘™π‘’β„Ž smallest element (in particular, median) in

𝑃(π‘œ)

slide-3
SLIDE 3

Greedy Algorithms

373F19 - Nisarg Shah 3

  • Greedy (also known as myopic) algorithm outline

➒ We want to find a solution 𝑦 that maximizes some

  • bjective function 𝑔

➒ But the space of possible solutions 𝑦 is too large ➒ The solution 𝑦 is typically composed of several parts (e.g.

𝑦 may be a set, composed of its elements)

➒ Instead of directly computing 𝑦…

  • Compute it one part at a time
  • Select the next part β€œgreedily” to get maximum immediate benefit

(this needs to be defined carefully for each problem)

  • May not be optimal because there is no foresight
  • But sometimes this can be optimal too!
slide-4
SLIDE 4

Interval Scheduling

373F19 - Nisarg Shah 4

  • Problem

➒ Job π‘˜ starts at time 𝑑

π‘˜ and finishes at time 𝑔 π‘˜

➒ Two jobs are compatible if they don’t overlap ➒ Goal: find maximum-size subset of mutually compatible jobs

slide-5
SLIDE 5

Interval Scheduling

373F19 - Nisarg Shah 5

  • Greedy template

➒ Consider jobs in some β€œnatural” order ➒ Take each job if it’s compatible with the ones already

chosen

  • What order?

➒ Earliest start time: ascending order of 𝑑

π‘˜

➒ Earliest finish time: ascending order of 𝑔

π‘˜

➒ Shortest interval: ascending order of 𝑔

π‘˜ βˆ’ 𝑑 π‘˜

➒ Fewest conflicts: ascending order of 𝑑

π‘˜, where 𝑑 π‘˜ is the

number of remaining jobs that conflict with π‘˜

slide-6
SLIDE 6

Example

373F19 - Nisarg Shah 6

  • Earliest start time: ascending order of 𝑑

π‘˜

  • Earliest finish time: ascending order of 𝑔

π‘˜

  • Shortest interval: ascending order of 𝑔

π‘˜ βˆ’ 𝑑 π‘˜

  • Fewest conflicts: ascending order of 𝑑

π‘˜, where 𝑑 π‘˜ is the number of

remaining jobs that conflict with π‘˜

slide-7
SLIDE 7

Interval Scheduling

373F19 - Nisarg Shah 7

  • Does it work?

earliest start time Counterexamples for shortest interval fewest conflicts

slide-8
SLIDE 8

Interval Scheduling

373F19 - Nisarg Shah 8

  • Implementing greedy with earliest finish time (EFT)

➒ Sort jobs by finish time. Say 𝑔

1 ≀ 𝑔 2 ≀ β‹― ≀ 𝑔 π‘œ

➒ When deciding whether job π‘˜ should be included, we

need to check whether it’s compatible with all previously added jobs

  • We only need to check if 𝑑

π‘˜ β‰₯ 𝑔 π‘—βˆ—, where π‘—βˆ— is the last added job

  • This is because for any jobs 𝑗 added before π‘—βˆ—, 𝑔

𝑗 ≀ 𝑔 π‘—βˆ—

  • So we can simply store and maintain the finish time of the last

added job

➒ Running time: 𝑃 π‘œ log π‘œ

slide-9
SLIDE 9

Interval Scheduling

373F19 - Nisarg Shah 9

  • Optimality of greedy with EFT

➒ Suppose for contradiction that greedy is not optimal ➒ Say greedy selects jobs 𝑗1, 𝑗2, … , 𝑗𝑙 sorted by finish time ➒ Consider the optimal solution π‘˜1, π‘˜2, … , π‘˜π‘› (also sorted by

finish time) which matches greedy for as long as possible

  • That is, we want π‘˜1 = 𝑗1, … , π‘˜π‘  = 𝑗𝑠 for greatest possible 𝑠
slide-10
SLIDE 10

Interval Scheduling

373F19 - Nisarg Shah 10

  • Optimality of greedy with EFT

➒ Both 𝑗𝑠+1 and π‘˜π‘ +1 were compatible with the previous

selection (𝑗1 = π‘˜1, … , 𝑗𝑠 = π‘˜π‘ )

➒ Consider the solution 𝑗1, 𝑗2, … , 𝑗𝑠, 𝑗𝑠+1, π‘˜π‘ +2, … , π‘˜π‘›

  • It should still be feasible (since 𝑔

𝑗𝑠+1 ≀ 𝑔 π‘˜π‘ +1)

  • It is still optimal
  • And it matches with greedy for one more step (contradiction!)

Another standard method is induction

slide-11
SLIDE 11

Interval Partitioning

373F19 - Nisarg Shah 11

  • Problem

➒ Job π‘˜ starts at time 𝑑

π‘˜ and finishes at time 𝑔 π‘˜

➒ Two jobs are compatible if they don’t overlap ➒ Goal: group jobs into fewest partitions such that jobs in

the same partition are compatible

  • One idea

➒ Find the maximum compatible set using the previous

greedy EFT algorithm, call it one partition, recurse on the remaining jobs.

➒ Doesn’t work (check by yourselves)

slide-12
SLIDE 12

Interval Partitioning

373F19 - Nisarg Shah 12

  • Think of scheduling lectures for various courses

into as few classrooms as possible

  • This schedule uses 4 classrooms for scheduling 10

lectures

slide-13
SLIDE 13

Interval Partitioning

373F19 - Nisarg Shah 13

  • Think of scheduling lectures for various courses

into as few classrooms as possible

  • This schedule uses 3 classrooms for scheduling 10

lectures

slide-14
SLIDE 14

Interval Partitioning

373F19 - Nisarg Shah 14

  • Let’s go back to the greedy template!

➒ Go through lectures in some β€œnatural” order ➒ Assign each lecture to a compatible classroom (which?),

and create a new classroom if the lecture conflicts with every existing classroom

  • Order of lectures?

➒ Earliest start time: ascending order of 𝑑

π‘˜

➒ Earliest finish time: ascending order of 𝑔

π‘˜

➒ Shortest interval: ascending order of 𝑔

π‘˜ βˆ’ 𝑑 π‘˜

➒ Fewest conflicts: ascending order of 𝑑

π‘˜, where 𝑑 π‘˜ is the

number of remaining jobs that conflict with π‘˜

slide-15
SLIDE 15

Interval Partitioning

373F19 - Nisarg Shah 15

  • At least when you

assign each lecture to an arbitrary feasible classroom, three of these heuristics do not work.

  • The fourth one works!

(next slide)

slide-16
SLIDE 16

Interval Partitioning

373F19 - Nisarg Shah 16

slide-17
SLIDE 17

Interval Partitioning

373F19 - Nisarg Shah 17

  • Running time

➒ Key step: check if the next lecture can be scheduled at

some classroom

➒ Store classrooms in a priority queue

  • key = finish time of its last lecture

➒ Is lecture π‘˜ compatible with some classroom?

  • Same as β€œIs 𝑑

π‘˜ at least as large as the minimum key?”

  • If yes: add lecture π‘˜ to classroom 𝑙 with minimum key, and

increase its key to 𝑔

π‘˜

  • Otherwise: create a new classroom, add lecture π‘˜, set key to 𝑔

π‘˜

➒ 𝑃(π‘œ) priority queue operations, 𝑃(π‘œ log π‘œ) time

slide-18
SLIDE 18

Interval Partitioning

373F19 - Nisarg Shah 18

  • Proof of optimality (lower bound)

➒ # classrooms needed β‰₯ maximum β€œdepth” at any point

  • depth = number of lectures running at that time

➒ We now show that our greedy algorithm uses only these

many classrooms!

slide-19
SLIDE 19

Interval Partitioning

373F19 - Nisarg Shah 19

  • Proof of optimality (upper bound)

➒ Let 𝑒 = # classrooms used by greedy ➒ Classroom 𝑒 was opened because there was a schedule π‘˜

which was incompatible with some lectures already scheduled in each of 𝑒 βˆ’ 1 other classrooms

➒ All these 𝑒 lectures end after 𝑑

π‘˜

➒ Since we sorted by start time, they all start at/before 𝑑

π‘˜

➒ So at time 𝑑

π‘˜, we have 𝑒 overlapping lectures

➒ Hence, depth β‰₯ 𝑒 ➒ So all schedules use β‰₯ 𝑒 classrooms. ➒ QED!

slide-20
SLIDE 20

Interval Graphs

373F19 - Nisarg Shah 20

  • Interval scheduling and interval partitioning can be

seen as graph problems

  • Input

➒ Graph 𝐻 = (π‘Š, 𝐹) ➒ Vertices π‘Š = jobs/lectures ➒ Edge 𝑗, π‘˜ ∈ 𝐹 if jobs 𝑗 and π‘˜ are incompatible

  • Interval scheduling = maximum independent set

(MIS)

  • Interval partitioning = graph colouring
slide-21
SLIDE 21

Interval Graphs

373F19 - Nisarg Shah 21

  • MIS and graph colouring are NP-hard for general

graphs

  • But they’re efficiently solvable for interval graphs

➒ Interval graphs = graphs which can be obtained from

incompatibility of intervals

➒ In fact, this holds even when we are not given an interval

representation of the graph

  • Can we extend this result further?

➒ Yes! Chordal graphs

  • Every cycle with 4 or more vertices has a chord
slide-22
SLIDE 22

Minimizing Lateness

373F19 - Nisarg Shah 22

  • Problem

➒ We have a single machine ➒ Each job π‘˜ requires π‘’π‘˜ units of time and is due by time π‘’π‘˜ ➒ If it’s scheduled to start at 𝑑

π‘˜, it will finish at 𝑔 π‘˜ = 𝑑 π‘˜ + π‘’π‘˜

➒ Lateness: β„“π‘˜ = max 0, 𝑔

π‘˜ βˆ’ π‘’π‘˜

➒ Goal: minimize the maximum lateness, 𝑀 = max

π‘˜

β„“π‘˜

  • Contrast with interval scheduling

➒ We can decide the start time ➒ All jobs must be scheduled on a single machine

slide-23
SLIDE 23

Minimizing Lateness

373F19 - Nisarg Shah 23

  • Example

Input An example schedule

slide-24
SLIDE 24

Minimizing Lateness

373F19 - Nisarg Shah 24

  • Let’s go back to greedy template

➒ Consider jobs one-by-one in some β€œnatural” order ➒ Schedule jobs in this order (nothing special to do here,

since we have to schedule all jobs and there is only one machine available)

  • Natural orders?

➒ Shortest processing time first: ascending order of

processing time π‘’π‘˜

➒ Earliest deadline first: ascending order of due time π‘’π‘˜ ➒ Smallest slack first: ascending order of π‘’π‘˜ βˆ’ π‘’π‘˜

slide-25
SLIDE 25

Minimizing Lateness

373F19 - Nisarg Shah 25

  • Counterexamples

➒ Shortest processing time first

  • Ascending order of processing time π‘’π‘˜

➒ Smallest slack first

  • Ascending order of π‘’π‘˜ βˆ’ π‘’π‘˜
slide-26
SLIDE 26

Minimizing Lateness

373F19 - Nisarg Shah 26

  • By now, you

should know what’s coming…

  • We’ll prove

that earliest deadline first works!

slide-27
SLIDE 27

Minimizing Lateness

373F19 - Nisarg Shah 27

  • Observation 1

➒ There is an optimal schedule with no idle time

slide-28
SLIDE 28

Minimizing Lateness

373F19 - Nisarg Shah 28

  • Observation 2

➒ Earliest deadline first has no idle time

  • Let us define an β€œinversion”

➒ 𝑗, π‘˜ such that 𝑒𝑗 < π‘’π‘˜ but π‘˜ is scheduled before 𝑗

  • Observation 3

➒ By definition, earliest deadline first has no inversions

  • Observation 4

➒ If a schedule with no idle time has an inversion, it has a

pair of inverted jobs scheduled consecutively

slide-29
SLIDE 29

Minimizing Lateness

373F19 - Nisarg Shah 29

  • Claim

➒ Swapping adjacently scheduled inverted jobs doesn’t

increase lateness but reduces #inversions by one

  • Proof

➒ Let β„“ and β„“β€² denote lateness before/after swap ➒ Clearly, ℓ𝑙 = ℓ𝑙

β€² for all 𝑙 β‰  𝑗, π‘˜

➒ Also, clearly, ℓ𝑗

β€² ≀ ℓ𝑗

slide-30
SLIDE 30

Minimizing Lateness

373F19 - Nisarg Shah 30

  • Claim

➒ Swapping adjacently scheduled inverted jobs doesn’t

increase lateness but reduces #inversions by one

  • Proof

➒ β„“π‘˜

β€² = 𝑔 π‘˜ β€² βˆ’ π‘’π‘˜ = 𝑔 𝑗 βˆ’ π‘’π‘˜ ≀ 𝑔 𝑗 βˆ’ 𝑒𝑗 = ℓ𝑗

➒ 𝑀′ = max ℓ𝑗

β€², β„“π‘˜ β€², max 𝑙≠𝑗,π‘˜ ℓ𝑙 β€²

≀ max ℓ𝑗, ℓ𝑗, max

𝑙≠𝑗,π‘˜ ℓ𝑙 ≀ 𝑀

slide-31
SLIDE 31

Minimizing Lateness

373F19 - Nisarg Shah 31

  • Proof of optimality of earliest deadline first

➒ Suppose for contradiction that it’s not optimal ➒ Consider an optimal schedule π‘‡βˆ— which has fewest inversions

among all optimal schedules

  • We can assume it has no idle time
  • If π‘‡βˆ— has zero inversions, it’s exactly earliest deadline first
  • So assume π‘‡βˆ— has at least one inversion
  • So it must have an adjacent inversion (𝑗, π‘˜)
  • But swapping these jobs doesn’t increase lateness (so new schedule

stays optimal) and reduces the number of inversions by 1

  • Contradiction given that π‘‡βˆ— has fewest inversions among all optimal

schedules.

  • QED!
slide-32
SLIDE 32

Lossless Compression

373F19 - Nisarg Shah 32

  • Problem

➒ We have a document that is written using π‘œ distinct labels ➒ NaΓ―ve encoding: represent each label using 𝑙 = log π‘œ bits ➒ If the document has length 𝑛, this uses 𝑛 log π‘œ bits ➒ Say for English documents with no punctuations etc, we

have π‘œ = 26, so we can use 5 bits.

  • 𝑏 = 00000
  • 𝑐 = 00001
  • 𝑑 = 00010
  • 𝑒 = 00011
  • …
slide-33
SLIDE 33

Lossless Compression

373F19 - Nisarg Shah 33

  • Is this optimal?

➒ What if 𝑏, 𝑓, 𝑠, 𝑑 are much more frequent in the

document than 𝑦, π‘Ÿ, 𝑨?

➒ Can we assign shorter codes to more frequent letters?

  • Say we assign…

➒ 𝑏 = 0, 𝑐 = 1, 𝑑 = 01, … ➒ See a problem?

  • What if we observe the encoding β€˜01’?
  • Is it β€˜ab’? Or is it β€˜c’?
slide-34
SLIDE 34

Lossless Compression

373F19 - Nisarg Shah 34

  • To avoid conflicts, we need prefix-free encoding

➒ Map each label 𝑦 to a bit-string 𝑑(𝑦) such that for all

distinct labels 𝑦 and 𝑧, 𝑑(𝑦) is not a prefix of 𝑑 𝑧

➒ Then it’s impossible to have a scenario like this

………………………..

➒ So we can read left to right, find the first point where it

becomes a valid encoding, decode the label, and continue

𝑑(𝑦) 𝑑(𝑧)

slide-35
SLIDE 35

Lossless Compression

373F19 - Nisarg Shah 35

  • Formal problem

➒ Given π‘œ symbols and their frequencies (π‘₯1, … , π‘₯π‘œ), find a

prefix-free encoding with lengths (β„“1, … , β„“π‘œ) assigned to the symbols which minimizes σ𝑗=1

π‘œ

π‘₯𝑗 β‹… ℓ𝑗

  • Note that σ𝑗=1

π‘œ

π‘₯𝑗 β‹… ℓ𝑗 is the length of the compressed document

  • Example

➒ (π‘₯𝑏, π‘₯𝑐, π‘₯𝑑, π‘₯𝑒, π‘₯𝑓, π‘₯𝑔) = (42,20,5,10,11,12) ➒ No need to remember the numbers ☺

slide-36
SLIDE 36

Lossless Compression

373F19 - Nisarg Shah 36

  • Observation: prefix-free encoding = tree

𝑏 β†’ 0, 𝑓 β†’ 100, 𝑔 β†’ 101, 𝑑 β†’ 1100, 𝑒 β†’ 1101, 𝑐 β†’ 111

slide-37
SLIDE 37

Lossless Compression

373F19 - Nisarg Shah 37

  • Huffman Coding

➒ Build a priority queue by adding 𝑦, π‘₯𝑦 for each symbol 𝑦 ➒ While |queue|β‰₯ 2

  • Take the two symbols with the lowest weight (𝑦, π‘₯𝑦) and (𝑧, π‘₯𝑧)
  • Merge them into one symbol with weight π‘₯𝑦 + π‘₯𝑧
  • Let’s see this on the previous example
slide-38
SLIDE 38

Lossless Compression

373F19 - Nisarg Shah 38

slide-39
SLIDE 39

Lossless Compression

373F19 - Nisarg Shah 39

slide-40
SLIDE 40

Lossless Compression

373F19 - Nisarg Shah 40

slide-41
SLIDE 41

Lossless Compression

373F19 - Nisarg Shah 41

slide-42
SLIDE 42

Lossless Compression

373F19 - Nisarg Shah 42

slide-43
SLIDE 43

Lossless Compression

373F19 - Nisarg Shah 43

  • Final Outcome

𝑏 β†’ 0, 𝑓 β†’ 100, 𝑔 β†’ 101, 𝑑 β†’ 1100, 𝑒 β†’ 1101, 𝑐 β†’ 111

slide-44
SLIDE 44

Lossless Compression

373F19 - Nisarg Shah 44

  • Running time

➒ 𝑃(π‘œ log π‘œ) ➒ Can be made 𝑃(π‘œ) if the labels are given to you sorted by

their frequencies

  • Proof of optimality

➒ Induction on the number of symbols π‘œ ➒ Base case: For π‘œ = 2, there are only two possible

encodings, both are optimal, assign 1 bit to each symbol

➒ Hypothesis: Assume it returns an optimal encoding with

π‘œ βˆ’ 1 symbols

slide-45
SLIDE 45

Lossless Compression

373F19 - Nisarg Shah 45

  • Proof of optimality

➒ Consider the case of π‘œ symbols ➒ Lemma 1: If π‘₯𝑦 < π‘₯𝑧, then ℓ𝑦 β‰₯ ℓ𝑧 in any optimal tree.

  • Proof sketch: Otherwise, swapping 𝑦 and 𝑧 would strictly reduce

the overall length (exercise!).

➒ Lemma 2: There is an optimal tree π‘ˆ in which the two

least frequent symbols are siblings.

  • Proof sketch: First prove that they must have the same longest

length assigned to them. Then, if they’re not siblings, chop and rearrange the tree to make them siblings (exercise!).

➒ Now, we can compare the tree 𝐼 produced by Huffman

vs such an optimal tree π‘ˆ

slide-46
SLIDE 46

Lossless Compression

373F19 - Nisarg Shah 46

  • Proof of optimality

➒ Let 𝑦 and 𝑧 be the two least frequency symbols ➒ In Huffman, we combine them in the first step into β€œxy” ➒ Let 𝐼′ and π‘ˆβ€² be trees obtained from 𝐼 and π‘ˆ by treating

𝑦𝑧 as one symbol with frequency π‘₯𝑦 + π‘₯𝑧

➒ Use induction hypothesis: π‘€π‘“π‘œπ‘•π‘’β„Ž 𝐼′ ≀ π‘€π‘“π‘œπ‘•π‘’β„Ž(π‘ˆβ€²) ➒ π‘€π‘“π‘œπ‘•π‘’β„Ž 𝐼 = π‘€π‘“π‘œπ‘•π‘’β„Ž 𝐼′ + π‘₯𝑦 + π‘₯𝑧 β‹… 1 ➒ π‘€π‘“π‘œπ‘•π‘’β„Ž π‘ˆ = π‘€π‘“π‘œπ‘•π‘’β„Ž π‘ˆβ€² + π‘₯𝑦 + π‘₯𝑧 β‹… 1 ➒ QED!

slide-47
SLIDE 47

Other Greedy Algorithms

373F19 - Nisarg Shah 47

  • If you aren’t familiar with the following algorithms,

spend some time checking them out!

➒ Dijkstra’s shortest path algorithm ➒ Kruskal and Prim’s minimum spanning tree algorithms