Denomination: Rs. 1, Rs. 2, Rs. 5 (are available as much as)
For Rs. 15, 14, 13 this denomination gives optimal using greedy approach (cover with maximum value as much as …). Coin change problem #2: Denominations: Rs. 1, 5, 7, 10 (again are available as much as). Required Rupees I want to have is 14 with minimal number of coins. Now, greedy strategy does not work. Since with greedy strategy, you will give me 5 coins (1 x 10, 4 x1), but 2 coins are sufficient (2x7). Lesson learnt: 1. Greedy strategy is applied to optimization problem. 2. Greedy strategy gives an optimal solution, but need not all. 3. Greedy strategy does not always give optimal solution. Fractional knapsack: Problem Formulation: Let x_i, 0<= x_i <= 1 denote the fraction of the amount of item i to be included. If x_i = 1, full item of i is included, if x_i = 0.5, means half of the item i is included, if x_i = 0 means, item i is not included. Benefit of item i = b_i, weight of item i = w_i, total weight of the bag is W. Objective: Maximize Ʃ xi * bi subject to Ʃ xi * wi <= W. Algorithm: Input: All items with their benefits and weights, total capacity of the bag W. Output: X = x_1, x_2,…., x_n ( 0 <= I <= 1). Step 1: find ratio b_i / w_i and sort the ratio in decreasing order (largest to smallest). So, use any sorting algorithm. Step 2: Initialize array x_i = 0 for all i from 1 to n. Ste 3: Initialize w = 0 and i=1. (w = current weight of the bag) Step 4: While (i <=n and w <= W) do 1. If w + w_i <= W, then x_i = 1. 2. Else x_i = (W – w) / w_i // W –w is the available weight 3. w = w + x_i *w_i 4. i = i ++ Step 5: Return all x_i. Eg: (280, 40), (100, 10), (120, 20), (120, 24). W = 60kgs. A = (280, 40) format: (benefit, weight) B = (100, 10) C = (120, 20) D = (120, 24) Ratio: R(A) = 280/40 =7, R(B) = 10, R(C) = 6, R(D) = 5. Arrange in descending order: 10,7,6,5. Revised order of the Items= B, A, C, D. Output: X_B = 1, X_A = 1, X_C = ½ X_D = 0