liveBook · Manning

Chapter 11: Dynamic programming

What you will learn in chapter 11 of Grokking Algorithms, Second Edition

The introduction defines dynamic programming as solving a hard problem by first solving smaller subproblems.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction defines dynamic programming as solving a hard problem by first solving smaller subproblems.

Key terms: dynamic programming, subproblems

11.1 The knapsack problem (revisited)

This section sets up the knapsack problem as choosing the most valuable goods a thief can carry within a weight limit.

Key terms: knapsack, 4 lb, maximum money’s worth

11.2 The simple solution

The section presents exhaustive search as a way to find the most valuable set of goods and explains why its exponential running time makes it impractical.

Key terms: possible set of goods, most value, O(2n) time, approximate solution, optimal solution

11.3 Dynamic programming

The section builds a knapsack grid from smaller-capacity solutions and uses it to find the most valuable combination of items.

Key terms: dynamic programming, grid, sub-knapsacks, rows, columns, max value, old estimate, space left over, subproblems

11.4 Knapsack problem FAQ

11.5 What happens if you add an item?

The section shows how to add an item to a dynamic-programming knapsack grid and why the best recorded value cannot decrease.

Key terms: dynamic programming, max value, subproblem, row, current max estimate, column

11.6 What happens if you change the order of the rows?

Shows that changing the order of item rows in the knapsack grid does not change the answer.

Key terms: grid, rows

11.7 Can you fill in the grid column-wise instead of row-wise?

11.8 What happens if you add a smaller item?

Adding an item with a smaller weight shows why the knapsack grid may need finer weight increments.

Key terms: grid, finer granularity, 3.5 lb knapsack

11.9 Can you steal fractions of an item?

The section explains why the all-or-nothing dynamic programming solution does not handle fractional items and shows how a greedy approach does.

Key terms: dynamic programming, fraction of an item, greedy algorithm, most valuable item, per pound

11.10 Optimizing your travel itinerary

The section recasts a two-day sightseeing itinerary as a knapsack problem that can be represented with a dynamic programming grid.

Key terms: knapsack problem, limited amount of time, dynamic programming grid

11.11 Handling items that depend on each other

This section uses a Paris itinerary to show why the knapsack dynamic-programming approach cannot handle items whose costs depend on other chosen items.

Key terms: dynamic programming, subproblems, discrete, knapsack

11.12 Is it possible that the solution will require more than two sub-knapsacks?

This section explains how a solution can include more than two items even though the algorithm combines at most two sub-knapsacks at a time.

Key terms: sub-knapsacks, best solution

11.13 Is it possible that the best solution doesn’t fill the knapsack completely?

A valuable diamond shows that the best knapsack choice can leave unused space.

Key terms: knapsack, value

11.14 Longest common substring

The section summarizes when dynamic programming is useful and gives a grid-based way to think about its subproblems before introducing a word-matching example.

Key terms: dynamic programming, constraint, discrete subproblems, grid, cells, subproblem, axes

11.15 Making the grid

The section sketches a dynamic-programming grid for finding the longest substring shared by two words.

Key terms: dynamic programming, grid, cells, subproblems, longest substring

11.16 Filling in the grid

This section frames filling the longest-common-substring grid as a matter of finding a rule for its subproblems, but leaves the rule itself for the reader to try and the next section to explain.

Key terms: grid, cell, subproblem, formula, framework, substring

11.17 The solution

The section shows how to read the completed longest-common-substring grid to decide which word Alex probably meant.

Key terms: longest common substring, grid, last cell

11.18 Longest common subsequence

This section uses a misspelled-word example to show why the longest common subsequence can be more useful than the longest common substring for comparing words.

Key terms: longest common substring, longest common subsequence

11.19 Longest common subsequence—solution

The section presents the longest common subsequence solution and gives examples of dynamic programming in real-world use.

Key terms: dynamic programming, longest common subsequence, diff, Levenshtein distance

11.20 Recap

The recap summarizes when dynamic programming is useful and how its grid represents an optimization problem as subproblems.

Key terms: dynamic programming, constraint, discrete subproblems, grid, cells, subproblem

Concepts explained in this chapter

← Chapter 10: Greedy algorithms Chapter 12: k-nearest neighbors →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.