Greedy Criteria for Knapsack (Capacity 100 kg): Optimal Choice for Given Items
We consider the 0/1 knapsack problem with capacity 100 kg and items that cannot be split (either take an item or leave it). Each item has a value and a weight as follows:
| Item | Value | Weight |
|---|---|---|
| 10 | 15 | |
| 20 | 25 | |
| 30 | 35 | |
| 40 | 45 | |
| 50 | 55 |
A common greedy criterion is to sort items by value-to-weight ratio (a.k.a. density). We will compute that ratio and select items in descending order until the capacity is reached, producing a candidate solution.
Note: For fractional knapsack (items can be split), this greedy strategy is provably optimal. For 0/1 knapsack (no splitting), greedy by ratio is not guaranteed optimal in general; however, for this particular instance we can check optimality by comparing feasible combinations.
Key terms:
- Greedy algorithm
- 0/1 knapsack
- Capacity constraint
- Value-to-weight ratio
Knapsack Problem (Greedy vs Dynamic Programming) - Fractional & 0/1
Step 1: Compute value-to-weight ratios
Compute :
- :
- :
- :
- :
- :
Sorted by decreasing ratio:
We now apply the greedy selection:
- keep adding the next item in sorted order if it fits within remaining capacity.
Greedy-by-ratio construction (capacity = 100 kg)
- 1Step 1
Order items descending by : .
- 2Step 2
Start with remaining capacity kg and total value .
- 3Step 3
Include (weight ). New total weight , so keep it. , remaining .
- 4Step 4
Next is (weight ). It fits exactly: . Keep it. , remaining .
- 5Step 5
Cannot add any other item. Final greedy set: with total weight and total value .
Greedy solution result
The greedy-by-ratio algorithm returns:
- Chosen items:
- Total weight: kg
- Total value:
Mermaid view of the greedy picks:
Step 2 (verification): Check whether a better feasible set exists
To justify optimality for this specific instance, we can enumerate feasible combinations (there are only subsets).
Weights:
Check high-value candidates:
-
Pairs:
- weight , value ✅
- weight , value
- weight , value
- weight , value
- weight , value
- weight , value
- weight , value
- weight , value
- weight , value
- weight , value
-
Triples (only those with weight ):
- weight , value
- weight , value
- weight , value
- weight (not feasible)
- weight (not feasible)
- Any triple including plus exceeds capacity with additional items quickly.
-
Quadruples / all five:
- weight (not feasible)
- weight (not feasible)
- Any subset of 4 or 5 items is infeasible here.
The maximum value among all feasible subsets is 90, achieved by .
Conclusion: For this particular instance, the greedy-by-value-to-weight criterion produces the optimal 0/1 knapsack solution:
type="tip" title="Why this greedy works here (even though it’s not always guaranteed)" content="Greedy-by-ratio selected two items that perfectly fill the capacity (55+45=100) while also having the highest densities among remaining items. For other datasets, the same greedy rule can miss a better combination."
type="warning" title="Greedy-by-ratio is not universally optimal for 0/1 knapsack" content="In 0/1 knapsack, you cannot split items. So even if one item has slightly better ratio, a different combination may yield higher total value without exceeding capacity."
Item value-to-weight ratios used by the greedy rule
Higher ratio items are considered earlier in greedy selection.
Greedy knapsack workflow for this instance
Evaluate densities
Compute ratiosCalculate for each ."
Sort by greedy criterion
Order itemsOrder items: ."
Add if it fits
Select while feasibleTake (55 kg), then take (45 kg). Capacity becomes 0."
Confirm best feasible set
Verify outcomeEnumerate feasible subsets; best value is 90 with ."
Greedy vs optimality: common edge cases
Knowledge Check
Which greedy criterion is used to choose items in the greedy approach described?