CSC373 Week 2: Greedy Algorithms Nisarg Shah
CSC373 Week 2: Greedy Algorithms Nisarg Shah
Nisarg Shah
𝑂(𝑛)
• What order?
➢ Earliest start time: ascending order of 𝑠𝑗
➢ Earliest finish time: ascending order of 𝑓𝑗
shortest interval
fewest conflicts
• 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)
• Order of lectures?
➢ Earliest start time: ascending order of 𝑠𝑗
➢ Earliest finish time: ascending order of 𝑓𝑗
➢ Hence, depth ≥ 𝑑
➢ So all schedules use ≥ 𝑑 classrooms.
➢ QED!
• Input
➢ Graph 𝐺 = (𝑉, 𝐸)
➢ Vertices 𝑉 = jobs/lectures
➢ Edge 𝑖, 𝑗 ∈ 𝐸 if jobs 𝑖 and 𝑗 are incompatible
➢ Lateness: ℓ𝑗 = max 0, 𝑓𝑗 − 𝑑𝑗
Input
An example schedule
• Natural orders?
➢ Shortest processing time first: ascending order of
processing time 𝑡𝑗
➢ Earliest deadline first: ascending order of due time 𝑑𝑗
• We’ll prove
that earliest
deadline
first works!
• 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
• Say we assign…
➢ 𝑎 = 0, 𝑏 = 1, 𝑐 = 01, …
➢ See a problem?
o What if we observe the encoding ‘01’?
o Is it ‘ab’? Or is it ‘c’?
𝑐(𝑥)
𝑐(𝑦)
• Example
➢ (𝑤𝑎 , 𝑤𝑏 , 𝑤𝑐 , 𝑤𝑑 , 𝑤𝑒 , 𝑤𝑓 ) = (42,20,5,10,11,12)
➢ No need to remember the numbers ☺
𝑎 → 0, 𝑒 → 100,
𝑓 → 101, 𝑐 → 1100,
𝑑 → 1101, 𝑏 → 111
• 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
➢ 𝐿𝑒𝑛𝑔𝑡ℎ 𝑇 = 𝐿𝑒𝑛𝑔𝑡ℎ 𝑇
′ + 𝑤 +𝑤
𝑥 𝑦 ⋅1
➢ QED!