explainer

Dynamic programming is tropical linear algebra.

Shortest-path DP looks like a pile of nested loops. Written in the right algebra, it is a single matrix identity — and every classic algorithm (Bellman–Ford, Floyd–Warshall, Viterbi) is that identity in disguise.

Use the min-plus semiring: a ⊕ b = min(a, b) and a ⊗ b = a + b. Read a matrix entry A[i][j] as the cost of the edge from i to j (and +∞ when there is no edge). Now watch what ordinary linear-algebra operations become.

the Bellman update is a matrix–vector product

d'[i] = mink ( A[i][k] + d[k] )

The relaxation step at the heart of every shortest-path DP is exactly one min-plus product d' = A ⊗ d. "Try every predecessor, keep the cheapest" is the tropical dot product.

matrix powers are optimal multi-step costs

Ak[i][j] = cheapest cost from i to j in exactly k steps

Raising A to the k-th power accumulates costs along paths and keeps the minimum. One Bellman–Ford pass is one more power. Iterating to a fixed point is value iteration.

the Kleene star is all-pairs shortest paths

A = I ⊕ A ⊕ A2 ⊕ … = all-pairs shortest costs

Summing over all path lengths is the closure. Computing it by successively allowing more intermediate nodes is precisely the Floyd–Warshall algorithm — Gaussian elimination over the semiring.

the eigenvalue is the optimal average cost per stage

λ = the minimum cycle mean

The same eigenvalue that sets the period in the kinesin demo reappears here as the best achievable long-run cost per step — the object behind average-cost Markov decision processes and cycle-cancelling. Max there, min here; identical structure.

So Bellman–Ford, Floyd–Warshall, and the Viterbi algorithm are not separate inventions. Each is a matrix computation in a tropical semiring: repeated products, a closure, or a max/min-plus contraction. Seeing the shared skeleton is what tropmat is for.

interactive

DP as repeated min-plus products

Edit the edge-cost matrix, then raise the step budget k. Each increment is one Bellman–Ford relaxation round — one min-plus matrix product. Entries that just improved are highlighted; when nothing changes, you have hit the all-pairs shortest paths (the Kleene star).

edge costs A  (blank = no edge)
steps ≤ k 1
shortest cost using at most k edges

The default graph is a little four-city network. Node i→j costs what the cell says; the table shows (A ⊕ I)k in min-plus, which is the cheapest way from i to j using at most k hops. It only ever decreases, and it stops changing once every shortest path has been found — that stable matrix is Floyd–Warshall's output, reached here by pure repeated multiplication.