A greedy algorithm makes the choice that looks best right now, never reconsiders it, and hopes to arrive at a global optimum. For the right problems this hope is justified.

The General Concept

A greedy algorithm builds a solution step by step. At each step it picks the locally optimal choice from the available candidates, subject to the constraints, and commits to it. A greedy algorithm usually has these parts: a candidate set, a selection function that picks the best candidate, a feasibility check, and an objective function to be maximised or minimised. It is fast and simple, but it is correct only when the problem has the greedy-choice property (a global optimum can be reached by local choices) and optimal substructure (an optimal solution contains optimal solutions of sub-problems). For many problems, such as the 0/1 knapsack, greedy does not give the optimum, so each greedy algorithm needs a proof.

Definition: Greedy method. A technique that constructs a solution by repeatedly choosing the locally best option, without backtracking, which yields an optimal solution when the problem has the greedy-choice property.

The Fractional (General) Knapsack Problem

A thief has a knapsack of capacity W and n items; item i has weight wᵢ and value vᵢ. In the fractional knapsack, any fraction xᵢ (0 ≤ xᵢ ≤ 1) of an item can be taken, and the aim is to maximise Σ xᵢvᵢ subject to Σ xᵢwᵢ ≤ W. The greedy rule is to sort items by value per unit weight vᵢ/wᵢ in decreasing order, take whole items while they fit, and take a fraction of the next one to fill the knapsack exactly. This is optimal for the fractional problem. The time is Θ(n log n) for sorting plus Θ(n) for filling, so Θ(n log n); with a heap-based selection, or when partial selection is used, it can be improved.

Why Greedy Works Here and Fails for 0/1

Suppose an optimal solution did not use the highest ratio item fully while using a lower-ratio item; exchanging a little of the lower for the higher ratio item would increase the value, a contradiction. This exchange argument proves the greedy choice. For the 0/1 knapsack, where items cannot be split, the same rule can fail: capacity 50 and items (weight, value) = (10, 60), (20, 100), (30, 120). Greedy by ratio takes the first two items for value 160, but taking the second and third gives 220. So 0/1 knapsack needs dynamic programming (Unit III).

Table 2.6: Fractional knapsack example

Item Weight Value Value per unit weight
1 10 60 6
2 20 100 5
3 30 120 4

Example 2.6: Fractional knapsack

Problem: Capacity W = 50; items (weight, value): (10, 60), (20, 100), (30, 120). Find the maximum value.

Solution:

  1. Ratios: 6, 5 and 4, so the order is item 1, item 2, item 3.
  2. Take item 1 fully: weight 10, value 60; remaining capacity 40.
  3. Take item 2 fully: weight 20, value 100; remaining capacity 20.
  4. Item 3 weighs 30, so take 20/30 = 2/3 of it: value 120 × 2/3 = 80.
  5. Total value = 60 + 100 + 80 = 240.

Result: Maximum value is 240 (the 0/1 version would give only 220).

def frac_knap(items, W):
    items = sorted(items, key=lambda t: t[1] / t[0], reverse=True)
    total = 0.0
    for w, v in items:
        take = min(w, W)
        total += v * take / w
        W -= take
        if W == 0:
            break
    return total
items = [(10, 60), (20, 100), (30, 120)]
print("fractional:", frac_knap(items, 50))
print("0/1 best (items 2,3):", 100 + 120)

Output

fractional: 240.0
0/1 best (items 2,3): 220

Common mistakes

  • Using greedy by value or by weight alone instead of by ratio.
  • Applying the fractional greedy rule to the 0/1 knapsack.
  • Forgetting that the last item may be taken only in part.
  • Assuming every greedy algorithm is optimal without proof.

Key takeaways

  • Greedy: take the locally best choice and never undo it.
  • It needs the greedy-choice property and optimal substructure.
  • Fractional knapsack: sort by value/weight, fill; optimal in Θ(n log n).
  • 0/1 knapsack is not solved correctly by greedy.

Practice questions

  1. What is the greedy rule for the fractional knapsack?
  2. State the time complexity of greedy fractional knapsack.
  3. Why does greedy fail for 0/1 knapsack?

Answers to the practice questions

  1. Take items in decreasing order of value per unit weight, the last one possibly in part.
  2. Θ(n log n), dominated by sorting.
  3. Items cannot be split, so the best ratio choice may waste capacity and lose the optimum.

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit II