3D Labs · Engineering
Algorithms and systems in motion
Each lab is a small game: fill a table, scale a cluster, follow a coroutine. What is abstract on paper moves here. Looking for machine learning? 16 more labs →
DP Terrain
What to take away
Dynamic programming = a recursion whose subproblems repeat, stored in a table and filled in an order where every cell's inputs are already done.
LCS and edit distance fill an m × n table: O(m·n) time, and each cell reads at most three neighbours.
Greedy can fail: with coins {1, 3, 4}, making 6 greedily takes 4 + 1 + 1 (3 coins); the table finds 3 + 3 (2 coins).
0/1 knapsack is O(n·W): it grows with the capacity's value, not its number of digits (pseudo-polynomial). Traceback rebuilds which items made the best value.
Did you get it?
How long is the LCS of ABCBDAB and BDCABA?
Coins of 1, 3 and 4 to pay 6: how many coins in the best answer?
Why does the table fill in a fixed order?