• 沒有找到結果。

# Algorithm Design and Analysis Greedy Algorithms (2)

N/A
N/A
Protected

Share "Algorithm Design and Analysis Greedy Algorithms (2)"

Copied!
22
0
0

(1)

(2)

(3)

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

(4)

Textbook Exercise 16.2-2

(5)

1

2

𝑛

1

2

𝑛

𝑖

1

2

𝑛

𝑖

𝑖

### • 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

(6)

𝑖

𝑗

𝑖

𝑗

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

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 𝑎04

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 𝑎01 𝑎4

20

𝐻′

𝐻

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

(7)

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

𝑎2 𝑎3 𝑎6 𝑎5

0 n

Penalty 2

𝑎7 𝑎04 𝑎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

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

(8)

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

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

𝑖

### ≥ 𝑤

𝑗

𝑎𝑗

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

(9)

### • Greedy algorithm

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?

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

(10)

𝑖

𝑖

### ) 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 𝑎06

30

(11)

## Scheduling to Minimize Lateness

(12)

1

2

𝑛

1

2

𝑛

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

(13)

𝑗

𝑗

𝑗

𝑗

### 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

(14)

### 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

(15)

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

Job 1 2

Processing Time (𝑡𝑖) 1 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

(16)

### • Greedy algorithm

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

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

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

(17)

### • 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

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

(18)

### •

𝑎𝑘 𝑎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

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

(19)

1

2

𝑛

𝑖

𝑗

𝑖

𝑗

𝑗

𝑖

𝑖

𝑗

𝑖

𝑗

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

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

(20)

### •

𝑎𝑗 𝑎𝑖

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

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

(21)

### • Greedy choice property

Optimal Solution

Greedy Choice

Subproblem Solution

= +

(22)

Important announcement will be sent to

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

As was the case in this study, several reports found that the best success rate in terms of pulp and periodontal healing was observed when the donor tooth was at the ½ to ¾ stage

Although Taiwan stipulates explicit regulations governing the requirements for organic production process, certification management, and the penalties for organic agricultural

• makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution.. • not always yield optimal solution; may end up at

✓ Combining an optimal solution to the subproblem via greedy can arrive an optimal solution to the original problem. Prove that there is always an optimal solution to the

✓ Combining an optimal solution to the subproblem via greedy can arrive an optimal solution to the original problem.. Prove that there is always an optimal solution to the

Algorithm Design Methods Greedy Algorithm.. by Chin

• The randomized bipartite perfect matching algorithm is called a Monte Carlo algorithm in the sense that.. – If the algorithm ﬁnds that a matching exists, it is always correct

• The randomized bipartite perfect matching algorithm is called a Monte Carlo algorithm in the sense that.. – If the algorithm ﬁnds that a matching exists, it is always correct

✓ Combining an optimal solution to the subproblem via greedy can arrive an optimal solution to the original problem.. Prove that there is always an optimal solution to the

Greedy-Choice Property : making locally optimal (greedy) choices leads to a globally optimal

 “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

In this work, for a locally optimal solution to the NLSDP (2), we prove that under Robinson’s constraint qualiﬁcation, the nonsingularity of Clarke’s Jacobian of the FB system

In this work, for a locally optimal solution to the nonlin- ear SOCP (4), under Robinson’s constraint qualification, we show that the strong second-order sufficient condition

● 應用 Greedy choice，將問題劃分成子問題，根據 optimal. substructure，可以持續應用

● 應用 Greedy choice ，將問題劃分成子問題，根據 optimal su bstructure ，可以持續應用 Greedy choice

 Combining an optimal solution to the subproblem via greedy can arrive an optimal solution to the original problem. Prove that there is always an optimal solution to the

- Greedy Best-First Search (or Greedy Search) Minimizing estimated cost from the node to reach a goal Expanding the node that appears to be closest to goal - A* Search.. Minimizing

We will design a simple and effective iterative algorithm to implement “The parallel Tower of Hanoi problem”, and prove its moving is an optimal

proposed a greedy algorithm to utilize the Divide-and-Conquer technique to obtain near optimal scheduling while attempting to minimize the size of total communication messages

To maximize coverage rate and minimize overlapping sensing area, we propose a greedy algorithm to find an arc on the circumference of the minimum overlapping area which can

The proposed algorithms use the optimal-searching technique of genetic algorithm (GA) to get an efficient scheduling solution in grid computing environment and adapt to

To reduce the harm of this study point of view to engage in the practice of drug treatment, and ultimately hope that social security will be improved, the researchers refer to

At first this project will deploy the Value Chain Analysis tool to compare the operation strategies of TMTC and KMTC, then use the Data Envelopment Analysis to