Professional Documents
Culture Documents
cs333/cutler Greedy
Optimization problems
An optimization problem:
Given a problem instance, a set of constraints and an objective function. Find a feasible solution for the given instance for which the objective function has an optimal value either maximum or minimum depending on the problem being solved.
A feasible solution satisfies the problems constraints The constraints specify the limitations on the required solutions. For example in the knapsack problem we require that the items in the knapsack will not exceed a given weight cs333/cutler Greedy
A proof is needed to show that the algorithm finds an optimal solution. A counter example shows that the greedy algorithm does not provide an optimal solution.
cs333/cutler Greedy 3
cs333/cutler Greedy
Is it optimal for all sets of coin sizes? Is there a solution for all sets of coin sizes?
(12,D,N,P/15)
cs333/cutler Greedy
A greedy solution:
Input: Set of coins of different denominations, amountowed change = {} while (more coin-sizes && valueof(change)<amountowed) Choose the largest remaining coin-size // Selection // feasibility check while (adding the coin does not make the valueof(change) exceed the amount-owed ) then add coin to change //check if solved if ( valueof(change) equals amount-owed) return change cs333/cutler Greedy else delete coin-size
cs333/cutler Greedy
Activity Selection
Given a set S of n activities with start time si and finish time fi of activity i Find a maximum size subset A of compatible activities (maximum number of activities). Activities are compatible if they do not overlap Can you suggest a greedy choice?
cs333/cutler Greedy 10
Example
Activities 1 2 3 4 5 6 7 11 13 2 3 3 7 10 11 15 12
1
0 2
4
Time
11
0 1 2 3 4 5 cs333/cutler 9Greedy 12 13 14 15 6 7 8 10 11
Counter Example 1
Select by start time
Activities 1 2 3 0
11 1 4
15
15 Time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cs333/cutler Greedy
12
Counter Example 2
Select by minimum duration
Activities 1 2 3
8 1 7 8 9
15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Time
cs333/cutler Greedy
13
11 13 12 10 7 11 15
0 1 2 3 4 5 cs333/cutler 9Greedy 12 13 14 15 6 7 8 10 11
Time
14
Activity Selection
Assume without loss of generality that we number the intervals in order of finish time. So f1...fn. Greedy choice: choose activity with minimum finish time The following greedy algorithm starts with A={1} and then adds all compatible jobs. (Theta(n)) Theta(nlogn) when including sort
cs333/cutler Greedy 15
Greedy-Activity-Selector(s,f)
n <- length[s] // number of activities A <- {1} j <- 1 //last activity added for i <- 2 to n //select if si >= fj then //compatible (feasible) add {i} to A j <- i //save new last activity return A
cs333/cutler Greedy 16
cs333/cutler Greedy
17
cs333/cutler Greedy
18
cs333/cutler Greedy
19