liveBook · Manning

Knapsack problem

As explained in Advanced Algorithms and Data Structures

An optimization problem that selects each item at most once to maximize total value subject to a capacity constraint.

The 0-1 knapsack problem chooses each item at most once, maximizing total value without exceeding a fixed capacity. A brute-force search considers every subset, so the number of combinations grows explosively as items are added. section 1.3.3

The main design choices are:

  • Use dynamic programming when capacity is sufficiently limited: the pseudo-polynomial solution takes time proportional to the capacity multiplied by the number of items. section 1.3.3
  • Recognize the trade-off: 0-1 knapsack is NP-complete, so no polynomial-time algorithm for finding an optimal solution is known. section 1.3.3
  • Question the constraints before optimizing: allowing fractional quantities changes the problem and enables a greedy strategy based on value per unit weight. section 1.3.4

Packing a mission crate

With 62 possible items and a 1000-kilogram capacity, dynamic programming requires about 1000 × 62 capacity-item steps instead of checking 2^62 combinations. If fractional quantities and repacking are allowed, selecting by calories per kilogram yields peanut butter, rice, wheat flour, strawberry jam, and 230 kilograms of beans, totaling 3,342,800 calories. section 1.3.3 section 1.3.4

The problem illustrates a broader algorithm-design lesson: preserve necessary constraints, but test whether relaxing an expensive constraint produces a sufficiently valuable and cheaper solution. section 1.3.5

How Advanced Algorithms and Data Structures covers it

How deep it goes
Contrasts brute force, dynamic programming, and greedy alternatives but stops short of detailing the dynamic-programming implementation.
Examples
Uses a mission crate, food items, allergy-driven combinations, a 1000-kilogram capacity, and calorie-per-kilogram selection.
What you can do after
Evaluate whether dynamic programming or constraint relaxation suits a capacity-limited selection problem.
How it connects
Connects 0-1 knapsack with dynamic programming, NP-complete problems, unbounded knapsack, greedy algorithms, and requirements analysis.

Also explained in

Compare how 4 books explain knapsack problem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.