We don’t know ahead of time which value to choose.
We first need to compute the results of subproblems and before computing
The selection of is done based on the results of the subproblems.
explained in the next slide..
Theorem: There exists an optimal solution such that
Proof: Consider an arbitrary optimal solution , where
How can you judge whether
A greedy algorithm will solve a particular optimization problem?
Two key ingredients
A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems
Example: Activity selection problem
Knapsack Problems(S, w)
Each item has:
A thief has a knapsack of weight capacity
Which items to choose to maximize the value of the items in the knapsack?
The 0-1 knapsack problem:
The fractional knapsack problem:
Optimal substructure: must be an optimal solution for
Proof: By contradiction, assume there is a solution for , which is better than .
Consider an optimal solution L for (S, W)
If we remove a weight of item from optimal load and let:
That is, Lj´ should be an optimal soln to the
Two different problems:
The problems are similar.
Which algorithm to solve each problem?
Can we use a greedy algorithm?
Greedy choice: Take as much as possible from the item with the largest value per pound
Does the greedy choice property hold?
Thus, by sorting the items by value per pound the greedy algorithm runs in time
Notation: :
Consider an optimal load for problem
Let’s consider two cases:
is an array;
Note : table is computed in row-major order
Run time: