L9 Greedy, Knapsack
L9 Greedy, Knapsack
Knapsack Problem
Lecture 12
Optimization Problems
➢ Finding out the optimal solution from all feasible solutions.
➢ Solution Space: Set of all solution over the given n input.
➢ Feasible Solution: Solutions from the solution space which will satisfy the constraints (conditions).
➢ Optimal Solution: One of the feasible solution which optimizes our goal.
➢ Optimization problem can be either maximization problem or minimization problem.
➢ Techniques used to solve Optimization Problems:
▪ Greedy Algorithms
▪ Dynamic Algorithms
▪ Branch and Bound
Greedy Algorithm
▪ The idea of a greedy algorithm is to make the choice that looks best at that moment.
▪ Most of the problems in greedy technique contain ‘n’ inputs (solution space) and our
objective is finding a subset which will produce optimal solution.
▪ Does not cover all combinations (takes less time).
▪ Does not always yield optimal solution.
Greedy Algorithm: Example
Problem: Want to travel from Delhi to Mumbai in 18 hours with minimum cost.
Solution Space: on foot, Bike, Bus, Car, Train, Flight.
Feasible Solution: Train, Flight (18 hours)
Optimal Solution: Train (minimum cost)
Greedy Algorithm: Applications
1. Knapsack Problem (fractional)
2. Huffman Code
3. Activity Selection Problem
4. Minimum Spanning Trees :
▪ Prim’s Algorithm
▪ Kruskal’s Algorithm
5. Single Source Shortest Paths:
▪ Dijkstra's Algorithm
▪ Bellman Ford
Knapsack Problem (Fractional)
Problem: We are given ‘n’ objects and a knapsack or bag. Object i has a weight wi and the knapsack has a capacity
W. If fraction xi, 0 ≤ xi≤1, is placed into the knapsack, then a profit of xipi is earned. The objective is to obtain a filling
of the knapsack that maximizes the total profit earned. Since the knapsack capacity is W, we require the total weight
of all chosen objects to be at most W.
Rs 120
Item 3 30 +
Item 2 50
20 Rs 100
Item 1 30
20
10 Rs 220
Rs60 Rs100 Rs120
Fractional Knapsack – Example 2
• E.g.: 20
Rs 80
Item 3 +
Item 2 50 50
20 Rs 100
Item 1 30
20 +
10 10 Rs 60
𝑝
Step 2: Sort the array based on 𝑤𝑖 in decreasing
𝑖
order. →O(nlogn)
Step 3: Take one by one object until knapsack
size becomes zero. →O(n)
50
Rs 24
15
Rs 31.5
Problem 2
Objects i Object 1 Object 2 Object 3 Object 4 Object 5
Profits p[i] 25 55 45 20 35
Weights w[i] 5 15 12 7 6
Number of objects n=5,
Knapsack size W=3.5
Problem 3
Objects i Object 1 Object 2 Object 3 Object 4 Object 5 Object 6 Object 7
Profits p[i] 10 5 15 7 6 18 3
Weights w[i] 2 3 5 7 1 4 1
Number of objects n=7,
Knapsack size W=15.
n=5 M=15
Profit 2 28 25 18 9
Weight 1 4 5 3 3
Thank You