Greedy Algorithm Introduction Simple and straightforward Take decisions

  • Slides: 10
Download presentation
Greedy Algorithm

Greedy Algorithm

Introduction Simple and straightforward. Take decisions on the basis of information at hand without

Introduction Simple and straightforward. Take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future. Easy to invent, easy to implement and most of the time quite efficient. Many problems cannot be solved correctly by greedy approach. Greedy algorithms are used to solve optimization problems

Greedy Approach Greedy Algorithm works by making the decision that seems most promising at

Greedy Approach Greedy Algorithm works by making the decision that seems most promising at any moment; it never reconsiders this decision, whatever situation may arise later.

Greedy Approach Structure Greedy Algorithm • Initially the set of chosen items is empty

Greedy Approach Structure Greedy Algorithm • Initially the set of chosen items is empty i. e. , solution set. • At each step qitem will be added in a solution set by using selection function. q. IF the set would no longer be feasible o reject items under consideration (and is never consider again). q. ELSE IF set is still feasible THEN o add the current item.

Coin Changing Given currency denominations: 1, 5, 10, 25, 50, devise a method to

Coin Changing Given currency denominations: 1, 5, 10, 25, 50, devise a method to pay amount to customer using fewest number of coins. (i) For the amount 91 (ii) For the amount 86 (iii)For the amount 87 (iv)For the amount 9 the solution contains one 50 Rs, four 10 Rs, one 1 Re currency. The algorithm is greedy because at every stage it chooses the largest coin without worrying about the consequences. Moreover, it never changes its mind in the sense that once a coin has been included in the solution set, it remains there.

Greedy Algorithm Make change for n units using the least possible number of coins.

Greedy Algorithm Make change for n units using the least possible number of coins. MAKE-CHANGE (n) C ← {50, 25, 10, 5, 1} // constant. S ← { }; // set that will hold the solution set. Sum ← 0 // sum of item in solution set WHILE sum not = n for all items in C x = item in C such that sum + x ≤ n S ← S {value of x} sum ← sum + x RETURN S

Knapsack Problem The knapsack problem or rucksack problem is a problem in combinatorial optimization:

Knapsack Problem The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. File: Knapsack Problem Greedy Algorithm 2 Video Ref: https: //www. youtube. com/watch? v=ZFK 9_jg. CBr. E

Homework 1 Apply Greedy Algorithm to the following instance of knapsack problem: Maximum capacity

Homework 1 Apply Greedy Algorithm to the following instance of knapsack problem: Maximum capacity is 6 Item 1 2 3 Weight 3 2 1 Value $25 $20 $15 4 5 $40 $50

Homework 2 Apply Greedy Algorithm to the following instance of knapsack problem: Maximum capacity

Homework 2 Apply Greedy Algorithm to the following instance of knapsack problem: Maximum capacity is 20 Chemical 1 2 3 Weight 18 15 10 Value 25 24 15

Homework 3 Given some items, pack the knapsack to get the maximum total value.

Homework 3 Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is not more than 13. Item # 1 2 3 Weight Value 1 8 3 6 5 5