AlgoViz

Dynamic programming: core concepts

medium 18 min

Dynamic programming solves a problem by breaking it into smaller subproblems, solving each distinct subproblem once and storing the answer.

Where it shows up

The same pattern appears across everyday software.

  • Diff tools and version control

    `diff` and the line diffs in code review find the longest common subsequence of two files with a DP table, then show everything outside it as additions and deletions.

  • Spell checkers and fuzzy search

    Edit distance (Levenshtein) is a DP over prefixes of two words: the cheapest way to turn the first i letters into the first j letters. Spell checkers use it to rank suggestions.

  • DNA and protein alignment

    Needleman-Wunsch and Smith-Waterman align biological sequences with a 2D DP table of match, mismatch and gap scores, the backbone of tools that compare genomes.

  • Network routing

    Bellman-Ford computes shortest paths by DP over the number of edges used, and distance-vector routing protocols such as RIP run the same update between routers.

  • Speech recognition and line breaking

    The Viterbi algorithm finds the most likely sequence of hidden states in speech and text models by DP over time steps. TeX breaks paragraphs into lines with a DP that minimises total badness.