90 likes | 307 Views
Lecture 8 Greedy Algorithms. Design and Analysis. Designing a Greedy Algorithm: 1. Break the problem into a sequence of decisions. 2. Identify a rule for the “best” option. Analyzing a Greedy Algorithm: Important! Often fails if you cannot find a proof.
E N D
Design and Analysis • Designing a Greedy Algorithm: • 1. Break the problem into a sequence of decisions. • 2. Identify a rule for the “best” option. • Analyzing a Greedy Algorithm: • Important! Often fails if you cannot find a proof. • Technique: Proof by contradiction.Assume there is a better solution, show that it is actually not better than what the algorithm did.
Example 2: Fractional Knapsack • There is a knapsack that can hold items of total weight at most W. There are now n items with weights w1,w2,…, wn. Each item also has a value v1,v2,…,vn. • The items are infinitely divisible: can put ½ (or any fraction) of an item into the knapsack. • Goal: Select fractions p1,p2,…,pn such that • Capacity constraint: p1w1+p2w2+…+pnwn <= W • Maximum Value: p1v1+p2v2+…+pnvnmaximized.
Designing the Algorithm • Think of the problem as making a sequence of decisions- Pick one item to put into the knapsack • Think of all the options for the first decision, use a simple criteria to select the “best” • Question: Which item should we put in first?A. The one that has max valueB. The one that has min weightC. The one that has max value/weight
Proof Idea • Assume there is a better solution OPT • Find some contradiction • Previously: can make the solution OPT the same as ALG, so OPT cannot be better • Fractional Knapsack: By combining ALG and OPT, can find a better solution.
Example 3: Horn-SAT • Considers puzzles like • If Mr. Smith has a dog, then Mrs. Brown has a cat. • If Mr. Jones has a dog, then he has a cat too. • If Mr. Smith has a dog and Mr. Jones has a cat, then Mrs. Peacock has a dog. • If Mrs. Brown and Mr. Jones share a pet of the same species, then Mr. Smith has a cat • All the men have dogs. • Not all women have cats.
Example 3: Horn-SAT • Logical formulas • Variable – true or false • Negation • Horn Clauses • Problem: Given a set of Horn clauses, determine whether there exists an assignment to variables such that all clauses are satisfied.
Designing the Algorithm • Think of the problem as making a sequence of decisions- Decide which variables are true • Think of all the options for the first decision, use a simple criteria to select the “best” • Question: Which variable has to be true?
Proof Idea • If the algorithm succeeds, then there is clearly an assignment • Need to show: if the algorithm fails, then no assignment can satisfy the clauses • Assume there is actually an assignment, how to find a contradiction?