liveBook · Manning

Subproblem

As explained in Grokking Algorithms, Second Edition

A subproblem is a smaller instance of a larger computational problem whose solution contributes to solving the larger instance.

Suppose you have a knapsack with a 4 lb limit and want to pack the most valuable items. What if you first solved the same problem for smaller knapsacks? Each smaller question is a subproblem. Dynamic programming uses its answers to build toward the answer for the full knapsack. section 11.3

A grid helps keep those questions straight. In the knapsack grid, rows represent the items available so far and columns represent weight limits. A cell asks for the best value possible with that row’s items and that column’s limit. Fill in the smaller cases, and their answers help you tackle larger ones. section 11.3 section 11.20

A different kind of smaller question

To compare a misspelled word with a possible match, you can compare shorter parts of the words first. Instead of starting with “hish” and “fish,” try “his” and “fis.” A grid cell can hold the length of the longest substring those parts have in common. Now the grid’s axes—parts of the two words—have a purpose. section 11.14 section 11.15

How do you spot useful subproblems? Ask what each cell should answer, then choose grid axes that describe the smaller cases. There is no single formula for every dynamic-programming problem; finding one takes experimentation. section 11.15 section 11.16 section 11.20

The smaller cases must be discrete: one choice must not change the cost of another in a way the subproblem leaves out. For example, visiting one Paris attraction makes another cheaper because the trip to Paris has already been paid for. Treating each visit as an independent item would count that travel time more than once. section 11.11

How Grokking Algorithms, Second Edition covers it

How deep it goes
Works through grid-cell meanings and choosing smaller cases, while leaving formula discovery to experimentation.
Examples
Uses smaller knapsacks, comparisons of “hish” with “fish,” and shared travel time among Paris visits.
What it assumes
Introduces dynamic programming through a knapsack walkthrough, then asks readers to reason about smaller string comparisons.
What you can do after
Practice/perform it by defining smaller grid-cell questions for knapsack or substring problems and checking whether choices interact.
How it connects
Connects subproblems to dynamic programming, grid cells, knapsack optimization, and longest common substring.

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.