AlgoViz

Unique paths

medium 18 min

A robot at the top-left of an m × n grid may only move right or down.

Where it shows up

The same pattern appears across everyday software.

  • Robots on warehouse and game grids

    Warehouse robots and game characters often move on grids with restricted directions. Counting or scoring routes with a table, and treating shelves or walls as blocked cells, is exactly Unique Paths II. Swapping the count for a cost gives the cheapest route.

  • Routes through a city grid

    In a city of square blocks, the number of shortest walking routes between two corners that are a blocks east and b blocks south is C(a + b, a). Closing a road is an obstacle; planners and puzzle books use the same counting.

  • Option pricing on a binomial lattice

    The binomial options pricing model moves a stock price up or down at each step. The number of paths that end at a given price after n steps is a binomial coefficient, and the backward pass over the lattice is a grid DP like this one.

  • Diff tools and DNA alignment

    git diff, spell checkers and DNA sequence alignment fill a table indexed by positions in two sequences, where each cell is built from the cell above, the cell to the left and the diagonal. Unique paths is the simplest member of that family.

  • Galton boards and probability

    A ball dropping through a board of pegs goes left or right at each row. The number of routes to each bin follows Pascal's triangle, the same numbers that fill the unique paths table, which is why the bins form a bell-shaped curve.