liveBook · Manning

Knapsack problem

As explained in Grokking Algorithms, Second Edition

The knapsack problem is an optimization problem that selects items within a capacity constraint to maximize their combined value.

Imagine a bag that holds 35 pounds. You have several items, each with a weight and a value. Which ones should you take? The knapsack problem asks you to choose items that fit within the capacity while making their combined value as high as possible. section 10.2

Why the obvious choice can lose

A tempting strategy is to take the most expensive item that fits, then repeat. But taking a stereo worth $3,000 leaves no room for anything else, while a laptop and guitar together are worth $3,500. A greedy choice can be useful when a good-enough answer will do, but it does not always find the best one. section 10.2

To find the best choice, dynamic programming works up from smaller knapsacks. Draw a grid with items as rows and capacities as columns. At each cell, ask whether taking the current item gives a better result than leaving it out. In a 4-pound example, a 1-pound guitar worth $1,500 fits in the first cell; the remaining cells build on smaller decisions. Even when the best answer contains several items, its smaller solutions can build on still smaller ones. section 11.3 section 11.12

The same question comes up when the limit is time rather than weight. For a two-day trip, give each place a time cost and a desirability rating, then use a grid to choose what to visit. section 11.10

How Grokking Algorithms, Second Edition covers it

How deep it goes
Works through an item-by-capacity grid from smaller knapsacks, beginning with a concrete cell before the formula.
Examples
Uses a stereo-versus-laptop-and-guitar choice, a 4-pound knapsack grid, and a time-limited travel itinerary.
What it assumes
Starts with item weights and values and builds the dynamic programming grid from an empty grid.
What you can do after
Practice/perform a grid-based comparison of item choices and recognize when a greedy pick may miss the best combination.
How it connects
Connects greedy algorithms, dynamic programming, subproblems, and optimization under a capacity constraint.

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 Grokking Algorithms, Second Edition. The section links point to the parts of the book it draws on.