**Greedy Algorithms (2)**

**Outline**

### • Greedy Algorithms

### • Greedy #1: Activity-Selection / Interval Scheduling

### • Greedy #2: Coin Changing

### • Greedy #3: Fractional Knapsack Problem

### • Greedy #4: Breakpoint Selection

### • Greedy #5: Huffman Codes

### • Greedy #6: Task-Scheduling

### • Greedy #7: Scheduling to Minimize Lateness

**Algorithm Design Strategy**

### • Do not focus on “specific algorithms”

### • But “some strategies” to “design” algorithms

### • First Skill: Divide-and-Conquer (各個擊破/分治)

### • Second Skill: Dynamic Programming (動態規劃)

### • Third Skill: Greedy (貪婪法則)

**Greedy #6: Task-Scheduling**

Textbook Exercise 16.2-2

**Task-Scheduling Problem**

### • Input: a finite set 𝑆 = 𝑎

_{1}

### , 𝑎

_{2}

### , … , 𝑎

_{𝑛}

**of 𝑛 unit-time tasks, their **

**of 𝑛 unit-time tasks, their**

### corresponding integer deadlines 𝑑

_{1}

### , 𝑑

_{2}

### , … , 𝑑

_{𝑛}

### (1 ≤ 𝑑

_{𝑖}

### ≤ 𝑛), and nonnegative penalties 𝑤

_{1}

### , 𝑤

_{2}

### , … , 𝑤

_{𝑛}

### if 𝑎

_{𝑖}

### is not finished by time 𝑑

_{𝑖}

### • Output: a schedule that minimizes the total penalty

**Job** **1** **2** **3** **4** **5** **6** **7**

Deadline (𝑑_{𝑖}) 1 2 3 4 4 4 6

Penalty (w_{𝑖}) 30 60 40 20 50 70 10

𝑎_{2} 𝑎_{3} 𝑎_{6} 𝑎_{5}

Penalty 30

𝑎_{7} 𝑎_{1}
𝑎_{4}

20

**Task-Scheduling Problem**

### • Let a schedule 𝐻 is the OPT

### • A task 𝑎

_{𝑖}

### is late in 𝐻 if 𝑓 𝐻, 𝑖 > 𝑑

_{𝑗}

### • A task 𝑎

_{𝑖}

### is early in 𝐻 if 𝑓 𝐻, 𝑖 ≤ 𝑑

_{𝑗}

**• We can have an early-first schedule 𝐻′ with the same total penalty (OPT)**

**Task-Scheduling Problem**

Input: 𝑛 tasks with their deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛} and penalties 𝑤_{1}, 𝑤_{2}, … , 𝑤_{𝑛}
Output: the schedule that minimizes the total penalty

𝑎_{2} 𝑎_{3} 𝑎_{6} 𝑎_{5}

0 n

Penalty 2

𝑎_{7} 𝑎0_{4}

**Task** **1** **2** **3** **4** **5** **6** **7**

𝑑_{𝑖} 1 2 3 4 4 4 6

w_{𝑖} 30 60 40 20 50 70 10

𝑎_{1}
30
𝑎_{2} 𝑎_{3} 𝑎_{6} 𝑎_{5}

0 n

Penalty 3

𝑎_{7} 𝑎0_{1}
𝑎_{4}

20

𝐻′

𝐻

If the late task proceeds the early task, switching them makes the early one earlier and late one still late

**Possible Greedy Choices**

*• Rethink the problem: “maximize the total penalty for the set of early tasks”*

### • Greedy idea

### • Largest-penalty-first w/o idle time?

### • Earliest-deadline-first w/o idle time?

𝑎_{2} 𝑎_{3} 𝑎_{6} 𝑎_{5}

0 n

Penalty 2

𝑎_{7} 𝑎0_{4} 𝑎_{1}
30

**Task** **1** **2** **3** **4** **5** **6** **7**

𝑑_{𝑖} 1 2 3 4 4 4 6

w_{𝑖} 30 60 40 20 50 70 10 60 40 70 50 10

**Task-Scheduling Problem**

Input: 𝑛 tasks with their deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛} and penalties 𝑤_{1}, 𝑤_{2}, … , 𝑤_{𝑛}
Output: the schedule that minimizes the total penalty

**Prove Correctness**

**• Greedy choice: select the largest-penalty task into the early set if feasible**

**• Greedy choice: select the largest-penalty task into the early set if feasible**

### • Proof via contradiction

### • Assume that there is no OPT including this greedy choice

• If OPT processes 𝑎_{𝑖} after 𝑑_{𝑖}, we can switch 𝑎_{𝑗} and 𝑎_{𝑖} into OPT’

### • The maximum penalty must be equal or lower, because 𝑤

_{𝑖}

### ≥ 𝑤

_{𝑗}

𝑎_{𝑗}

0 n

Penalty 𝑤_{𝑖}

𝑎_{𝑖}
𝑑_{𝑖}

𝑎_{𝑖}

0 n

Penalty

𝑎_{𝑗}
𝑑_{𝑖}

𝑤_{𝑗}

𝑤_{𝑖} ≥ 𝑤_{𝑘} for all 𝑎_{𝑘} in the early set
**Task-Scheduling Problem**

Input: 𝑛 tasks with their deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛} and penalties 𝑤_{1}, 𝑤_{2}, … , 𝑤_{𝑛}
Output: the schedule that minimizes the total penalty

**Prove Correctness**

### • Greedy algorithm

Task-Scheduling(n, d[], w[])

sort tasks by penalties s.t. w[1] ≥ w[2] ≥ … ≥ w[n]

for i = 1 to n

find the latest available index j <= d[i]

if j > 0

A = A ∪ {i}

mark index j unavailable

return A // the set of early tasks

Can it be better?

**Task-Scheduling Problem**

_{1}, 𝑑_{2}, … , 𝑑_{𝑛} and penalties 𝑤_{1}, 𝑤_{2}, … , 𝑤_{𝑛}
Output: the schedule that minimizes the total penalty

**Example Illustration**

**Job** **1** **2** **3** **4** **5** **6** **7**

### Deadline (𝑑

_{𝑖}

### ) 4 2 4 3 1 4 6

### Penalty (w

_{𝑖}

### ) 70 60 50 40 30 20 10

𝑎_{1}
𝑎_{3}

𝑎_{4} 𝑎_{2}

0 1 2 3 4 5 6 7

Total penalty = 30 + 20 = 50

2
𝑎_{7} 𝑎_{5} 𝑎0_{6}

30

### Practice: how about the greedy algorithm using “earliest-deadline-first”

**Greedy #7:**

**Scheduling to Minimize Lateness**

**Scheduling to Minimize Lateness**

### • Input: a finite set 𝑆 = 𝑎

_{1}

### , 𝑎

_{2}

### , … , 𝑎

_{𝑛}

### of 𝑛 tasks, their processing time 𝑡

_{1}

### , 𝑡

_{2}

### , … , 𝑡

_{𝑛}

### , and integer deadlines 𝑑

_{1}

### , 𝑑

_{2}

### , … , 𝑑

_{𝑛}

### • Output: a schedule that minimizes the maximum lateness

**Job** **1** **2** **3** **4**

Processing Time (𝑡_{𝑖}) 3 5 3 2

Deadline (𝑑_{𝑖}) 4 6 7 8

𝑎_{4} 𝑎_{1} 𝑎_{3} 𝑎_{2}

0 2 5 8 13

Lateness 0 1 1 7

**Scheduling to Minimize Lateness**

### • Let a schedule 𝐻 contains 𝑠 𝐻, 𝑗 and 𝑓 𝐻, 𝑗 as the start time and finish time of job 𝑗

### • 𝑓 𝐻, 𝑗 − 𝑠 𝐻, 𝑗 = 𝑡

_{𝑗}

### • Lateness of job 𝑗 in 𝐻 is 𝐿 𝐻, 𝑗 = max 0, 𝑓 𝐻, 𝑗 − 𝑑

_{𝑗}

### • The goal is to minimize max

𝑗

### 𝐿 𝐻, 𝑗 = max

𝑗

### 0, 𝑓 𝐻, 𝑗 − 𝑑

_{𝑗}

**Scheduling to Minimize Lateness Problem**

Input: 𝑛 tasks with their processing time 𝑡_{1}, 𝑡_{2}, … , 𝑡_{𝑛}, and deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛}
Output: the schedule that minimizes the maximum lateness

**Possible Greedy Choices**

### • Greedy idea

### • Shortest-processing-time-first w/o idle time?

### • Earliest-deadline-first w/o idle time?

### Practice: prove that any schedule w/ idle is not optimal

**Scheduling to Minimize Lateness Problem**

Input: 𝑛 tasks with their processing time 𝑡_{1}, 𝑡_{2}, … , 𝑡_{𝑛}, and deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛}
Output: the schedule that minimizes the maximum lateness

**Possible Greedy Choices**

### • Idea

**• Shortest-processing-time-first w/o idle time?**

**Job** **1** **2**

Processing Time (𝑡_{𝑖}) 1 2

Deadline (𝑑_{𝑖}) 10 2

𝑎_{1} 𝑎_{2}

0 1 3

Lateness 0 1

𝑎_{2} 𝑎_{1}

0 2 3

Lateness 0 0

**Scheduling to Minimize Lateness Problem**

Input: 𝑛 tasks with their processing time 𝑡_{1}, 𝑡_{2}, … , 𝑡_{𝑛}, and deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛}
Output: the schedule that minimizes the maximum lateness

**Possible Greedy Choices**

### • Idea

**• Earliest-deadline-first w/o idle time?**

### • Greedy algorithm

Min-Lateness(n, t[], d[])

sort tasks by deadlines s.t. d[1]≤d[2]≤ ...≤d[n]

ct = 0 // current time for j = 1 to n

assign job j to interval (ct, ct + t[j]) s[j] = ct

f[j] = s[j] + t[j]

ct = ct + t[j]

return s[], f[]

**Scheduling to Minimize Lateness Problem**

_{1}, 𝑡_{2}, … , 𝑡_{𝑛}, and deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛}
Output: the schedule that minimizes the maximum lateness

**Prove Correctness** **– Greedy-Choice Property**

### • Greedy choice: first select the task with the earliest deadline

### • Proof via contradiction

### • Assume that there is no OPT including this greedy choice

• If OPT processes 𝑎_{1} as the 𝑖-th task (𝑎_{𝑘}), we can switch 𝑎_{𝑘} and 𝑎_{1} into OPT’

### • The maximum lateness must be equal or lower → 𝐿 OPT′ ≤ 𝐿 OPT exchange argument

**Scheduling to Minimize Lateness Problem**

_{1}, 𝑡_{2}, … , 𝑡_{𝑛}, and deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛}
Output: the schedule that minimizes the maximum lateness

**Prove Correctness** **– Greedy-Choice Property**

### •

𝑎_{𝑘} 𝑎_{1}

L(OPT, k)

𝑎_{1} 𝑎_{𝑘}

L(OPT’, 1) L(OPT’, k) L(OPT, 1) OPT

OPT’

If 𝑎_{𝑘} is not late in OPT’: If 𝑎_{𝑘} is late in OPT’:

Generalization of this property?

**Scheduling to Minimize Lateness Problem**

_{1}, 𝑡_{2}, … , 𝑡_{𝑛}, and deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛}
Output: the schedule that minimizes the maximum lateness

**Prove Correctness** **– No Inversions**

*• There is an optimal scheduling w/o inversions given 𝑑*

_{1}

### ≤ 𝑑

_{2}

### ≤ ⋯ ≤ 𝑑

_{𝑛}

### • 𝑎

_{𝑖}

### and 𝑎

_{𝑗}

*are inverted if 𝑑*

_{𝑖}

### < 𝑑

_{𝑗}

### but 𝑎

_{𝑗}

### is scheduled before 𝑎

_{𝑖}

### • Proof via contradiction

### • Assume that OPT has 𝑎

_{𝑖}

### and 𝑎

_{𝑗}

### that are inverted

### • Let OPT’ = OPT but 𝑎

_{𝑖}

### and 𝑎

_{𝑗}

### are swapped

### • OPT’ is equal or better than OPT → 𝐿 OPT′ ≤ 𝐿 OPT

**Scheduling to Minimize Lateness Problem**

_{1}, 𝑡_{2}, … , 𝑡_{𝑛}, and deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛}
Output: the schedule that minimizes the maximum lateness

**Prove Correctness** **– No Inversions**

### •

𝑎_{𝑗} 𝑎_{𝑖}

L(OPT, j)

𝑎_{𝑖} 𝑎_{𝑗}

L(OPT’, i) L(OPT’, j) L(OPT, i) OPT

OPT’

If 𝑎_{𝑗} is not late in OPT’: If 𝑎_{𝑗} is late in OPT’:

……

……

### The earliest-deadline-first greedy algorithm is optimal

Optimal Solution

Greedy Choice

Subproble m Solution

= +

**Scheduling to Minimize Lateness Problem**

_{1}, 𝑡_{2}, … , 𝑡_{𝑛}, and deadlines 𝑑_{1}, 𝑑_{2}, … , 𝑑_{𝑛}
Output: the schedule that minimizes the maximum lateness

**Concluding Remarks**

### • “Greedy”: always makes the choice that looks best at the moment in the hope that this choice will lead to a globally optimal solution

### • When to use greedy

### • Whether the problem has optimal substructure

### • Whether we can make a greedy choice and remain only one subproblem

### • Common for optimization problem

### • Prove for correctness

### • Optimal substructure

### • Greedy choice property

Optimal Solution

Greedy Choice

Subproblem Solution

= +

Important announcement will be sent to

@ntu.edu.tw mailbox & post to the course website

Course Website: http://ada.miulab.tw Email: ada-ta@csie.ntu.edu.tw