Knapsack
Knapsack
Knapsack problem
• Types:
• Fractional knapsack problem
• 0/1 knapsack problem
• Fractional knapsack problem: Greedy technique
• 0/1 knapsack problem: Can be solved using Dynamic programming
Greedy Algorithm
• Follows the problem-solving heuristic of making the locally optimal choice
at each stage
• An approach for solving a problem by selecting the best option available at
the moment
• Doesn't worry whether the current best result will bring the overall optimal
result
• In many cases, a greedy strategy does not produce an optimal solution
• But a greedy heuristic can yield locally optimal solutions that approximate
a globally optimal solution in a reasonable amount of time
Greedy Algorithm
• Can be determined whether the algorithm can be used with any
problem if the problem has the following properties:
• Greedy Choice Property:
• If an optimal solution to the problem can be found by choosing the best choice at each
step
• Without reconsidering the previous steps once chosen, the problem can be solved using
a greedy approach
• Optimal Substructure:
• If the optimal overall solution to the problem corresponds to the optimal solution to its
subproblems
• Then the problem can be solved using a greedy approach
Advantages of Greedy Approach
• Easier to describe
• Can perform better than other algorithms (but, not in all cases)
Drawback of Greedy Approach
• Doesn't always produce the optimal solution
• Because it does not consider the entire data. The choice made by the
greedy approach does not consider future data and choices.