• 沒有找到結果。

Algorithm Design and Analysis Greedy Algorithm (1)

N/A
N/A
Protected

Academic year: 2022

Share "Algorithm Design and Analysis Greedy Algorithm (1)"

Copied!
22
0
0

加載中.... (立即查看全文)

全文

(1)

Algorithm Design and Analysis Greedy Algorithm (1)

http://ada.miulab.tw

(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

(3)

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 (貪婪法則)

(4)

Greedy Algorithms

Textbook Chapter 16 – Greedy Algorithms

Textbook Chapter 16.2 – Elements of the greedy strategy

(5)

What is Greedy Algorithms?

• always makes the choice that looks best at the moment

• 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 local optimal

Greedy: move towards max gradient and hope it is global maximum local maximal

global maximal

local maximal

(6)

Algorithm Design Paradigms

• Dynamic Programming

• has optimal substructure

• make an informed choice after getting optimal solutions to subproblems

• dependent or overlapping subproblems

• Greedy Algorithms

• has optimal substructure

• make a greedy choice before solving the subproblem

• no overlapping subproblems

✓ Each round selects only one subproblem

✓ The subproblem size decreases

Optimal Solution

Possible Case 1 Possible

Case 2

Possible Case k

max /min

Subproblem Solution Subproblem

Solution

Subproblem Solution

+

+

+

= Optimal

Solution

Greedy Choice

Subproblem Solution

= +

(7)

Greedy Procedure

1. Cast the optimization problem as one in which we make a choice and remain one subproblem to solve

2. Demonstrate the optimal substructure

✓ Combining an optimal solution to the subproblem via greedy can arrive an optimal solution to the original problem

3. Prove that there is always an optimal solution to the original problem that

makes the greedy choice

(8)

Greedy Algorithms

To yield an optimal solution, the problem should exhibit

1. Optimal Substructure : an optimal solution to the problem contains within its optimal solutions to subproblems

2. Greedy-Choice Property : making locally optimal (greedy) choices leads to

a globally optimal solution

(9)

Proof of Correctness Skills

• Optimal Substructure : an optimal solution to the problem contains within it optimal solutions to subproblems

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

• Show that it exists an optimal solution that “contains” the greedy choice using exchange argument

• For any optimal solution OPT, the greedy choice 𝑔 has two cases

• 𝑔 is in OPT: done

• 𝑔 not in OPT: modify OPT into OPT’ s.t. OPT’ contains 𝑔 and is at least as good as OPT

OPT OPT’

𝑔

If OPT’ is better than OPT, the property is proved by contradiction

If OPT’ is as good as OPT, then we showed that there exists an optimal solution containing 𝑔 by construction

(10)

Activity-Selection / Interval Scheduling

Textbook Chapter 16.1 – An activity-selection problem

(11)

Activity-Selection/ Interval Scheduling

• Input: 𝑛 activities with start times 𝑠

𝑖

and finish times 𝑓

𝑖

(the activities are sorted in monotonically increasing order of finish time 𝑓

1

≤ 𝑓

2

≤ ⋯ ≤ 𝑓

𝑛

)

• Output: the maximum number of compatible activities

• Without loss of generality: 𝑠

1

< 𝑠

2

< ⋯ < 𝑠

𝑛

and 𝑓

1

< 𝑓

2

< ⋯ < 𝑓

𝑛

• 大的包小的則不考慮大的 → 用小的取代大的一定不會變差

time 1

2 3 4 5 6

activity index

(12)

Weighted Interval Scheduling

• Subproblems

• WIS(i): weighted interval scheduling for the first 𝑖 jobs

• Goal: WIS(n)

• Dynamic programming algorithm

i 0 1 2 3 4 5 n

M[i]

Set 𝑣𝑖 = 1 for all 𝑖 to formulate it into the activity-selection problem

Weighted Interval Scheduling Problem

Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible

(13)

Activity-Selection Problem

• Dynamic programming

• Optimal substructure is already proved

• Greedy algorithm

select the 𝑖-th activity

Why does the 𝑖-th activity must appear

in an OPT?

Activity-Selection Problem

Input: 𝑛 activities with 𝑠𝑖, 𝑓𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. 𝑖 and 𝑗 are compatible Output: the maximum number of activities

(14)

Greedy-Choice Property

• Goal:

• Proof

• Assume there is an OPT solution for the first 𝑖 − 1 activities (𝑀𝑖−1)

• 𝐴𝑗 is the last activity in the OPT solution →

• Replacing 𝐴𝑗 with 𝐴𝑖 does not make the OPT worse

time 1

2 :

i i - 1

:

activity index

1 2 3 4 5 6 7 8 9

(15)

Pseudo Code

Act-Select(n, s, f, v, p) M[0] = 0

for i = 1 to n if p[i] >= 0

M[i] = 1 + M[p[i]]

return M[n]

Find-Solution(M, n) if n = 0

return {}

return {n} ∪ Find-Solution(p[n])

Activity-Selection Problem

Input: 𝑛 activities with 𝑠𝑖, 𝑓𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. 𝑖 and 𝑗 are compatible Output: the maximum number of activities

(16)

Coin Changing

Textbook Exercise 16.1

(17)

Coin Changing Problem

• Input: 𝑛 dollars and unlimited coins with values 𝑣

𝑖

(1, 5, 10, 50)

• Output: the minimum number of coins with the total value 𝑛

• Cashier’s algorithm: at each iteration, add the coin with the largest value no more than the current total

Does this algorithm return the OPT?

(18)

Step 1: Cast Optimization Problem

• Subproblems

• C(i): minimal number of coins for the total value 𝑖

• Goal: C(n)

Coin Changing Problem

Input: 𝑛 dollars and unlimited coins with values 𝑣𝑖 (1, 5, 10, 50) Output: the minimum number of coins with the total value 𝑛

(19)

Step 2: Prove Optimal Substructure

• Suppose OPT is an optimal solution to C(i) , there are 4 cases:

• Case 1: coin 1 in OPT

• OPT\coin1 is an optimal solution of C(i – v1)

• Case 2: coin 2 in OPT

• OPT\coin2 is an optimal solution of C(i – v2)

• Case 3: coin 3 in OPT

• OPT\coin3 is an optimal solution of C(i – v3)

• Case 4: coin 4 in OPT

• OPT\coin4 is an optimal solution of C(i – v4)

Coin Changing Problem

Input: 𝑛 dollars and unlimited coins with values 𝑣𝑖 (1, 5, 10, 50) Output: the minimum number of coins with the total value 𝑛

(20)

Step 3: Prove Greedy-Choice Property

• Greedy choice: select the coin with the largest value no more than the current total

• Proof via contradiction (use the case 10 ≤ 𝑖 < 50 for demo)

• Assume that there is no OPT including this greedy choice (choose 10)

→ all OPT use 1, 5, 50 to pay 𝑖

• 50 cannot be used

• #coins with value 5 < 2 → otherwise we can use a 10 to have a better output

• #coins with value 1 < 5 → otherwise we can use a 5 to have a better output

• We cannot pay 𝑖 with the constraints (at most 5 + 4 = 9) Coin Changing Problem

Input: 𝑛 dollars and unlimited coins with values 𝑣𝑖 (1, 5, 10, 50) Output: the minimum number of coins with the total value 𝑛

(21)

To Be Continued…

(22)

Question?

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

參考文獻

相關文件

Other advantages of our ProjPSO algorithm over current methods are (1) our experience is that the time required to generate the optimal design is gen- erally a lot faster than many

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

Specifically, for a locally optimal solution to the nonlinear second-order cone programming (SOCP), under Robinson’s constraint qualification, we establish the equivalence among

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

 Definition: A problem exhibits  optimal substructure if an ..

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

 Definition: A problem exhibits optimal subst ructure if an optimal solution to the proble m contains within it optimal solutions to su bproblems..  怎麼尋找 optimal

✓ Express the solution of the original problem in terms of optimal solutions for subproblems. Construct an optimal solution from