Open In App

GFact | Why doesn’t Greedy Algorithm work for 0-1 Knapsack problem?

Last Updated : 03 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss why the 0-1 knapsack problem cannot be solved by the greedy method, or why the greedy algorithm is not optimal for the 0-1 Knapsack Problem.

Greedy algorithms are widely used in optimization problems, where the goal is to find the best solution from a set of choices. The greedy approach makes locally optimal choices at each step in hopes of reaching a global optimum. It selects the item with the highest value-to-weight ratio and continues to add items until the knapsack’s weight capacity is reached or surpassed.

Why Greedy Algorithms Fail for the 0-1 Knapsack Problem:

The primary reason the greedy algorithm fails for the 0-1 Knapsack problem is that it does not consider the possibility of excluding certain items to achieve a better overall solution. The greedy approach only focuses on immediate gains, choosing items with high value-to-weight ratios without considering the potential impact on future decisions.

Consider a backpack with a weight capacity of 4, and items with the following weights and values:

Item | Weight | Value | value/Weight
A 3 1.8 0.6
B 2 1 0.5
C 2 1 0.5

If you apply Greedy on value you will first select item A, so the residual weight capacity will be 1. Since both items A and B weigh more than that residual value, you won’t be able to add any more items. Now, this is a feasible solution but not an optimal solution.

Greedy based on value per weight would first choose item A and then quit, as residual capacity is not enough to accommodate any more item, so the total value reached is 1.8.

The optimal solution, however, is to choose items B and C, which together exactly weigh the full capacity and have a combined value of 2.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads