Dynamic Programming
Dynamic programming solves a problem by solving overlapping subproblems once, storing their answers, and combining them. The naive recursion is the specification. The table is the implementation.
Detailed Explanation
A problem has optimal substructure if an optimum is built from optima of smaller instances. It has overlapping subproblems if those instances appear more than once. Fibonacci has both: F(n) needs F(n-1) and F(n-2), and those share almost all work. The recursive tree is exponential. A table dp[i] = dp[i-1] + dp[i-2] is linear.
Two styles. Top-down memoization: write the recursive function and cache (arguments) -> answer in an array or a hash table. Bottom-up: decide an order that only needs already-filled cells, then loop. Bottom-up in C is usually simpler because the cache is a plain array and there is no recursion depth.
The hard part is the state. What do you have to remember so that a subproblem is self-contained? For knapsack, (index, remaining_capacity). For a string edit, (i, j) prefixes. For a path on a DAG, (vertex). If the state is wrong, you either recompute or you get a wrong answer that looks cached.
C tables are arrays. A 2-D DP of n * m ints is malloc(n * m * sizeof(int)) indexed as dp[i*m + j], or a VLA if you accept the stack risk. Rolling arrays keep only the last row when the recurrence only looks back one layer, dropping space from O(n*m) to O(m).
Initialization is the base cases. dp[0] = 0, dp[1] = 1, empty-string costs, unreachable cells as a sentinel. A wrong base case poisons the table. Print a small n by hand before you trust a large one.
DP is not "I used an array." A prefix sum is a table; it is DP only in the loose sense. Use the word when you chose a recurrence because the obvious recursion overlapped.
Code Examples
#include <stdio.h>
#include <stdlib.h>
static long long fib(int n)
{
if (n < 2) {
return n;
}
long long *dp = malloc((size_t)(n + 1) * sizeof *dp);
if (dp == NULL) {
return -1;
}
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
long long ans = dp[n];
free(dp);
return ans;
}
int main(void)
{
printf("%lld\n", fib(10)); /* prints: 55 */
return 0;
}#include <stdio.h>
/* Unbounded knapsack-style coin change: fewest coins for amount. */
enum { INF = 1e9 };
static int coins(const int *c, int nc, int amount)
{
int dp[32];
for (int i = 0; i <= amount; i++) {
dp[i] = INF;
}
dp[0] = 0;
for (int a = 1; a <= amount; a++) {
for (int i = 0; i < nc; i++) {
if (c[i] <= a && dp[a - c[i]] + 1 < dp[a]) {
dp[a] = dp[a - c[i]] + 1;
}
}
}
return dp[amount] >= INF ? -1 : dp[amount];
}
int main(void)
{
int c[] = { 1, 5, 10 };
printf("%d\n", coins(c, 3, 12)); /* prints: 3 (10+1+1) */
return 0;
}Common Gotchas
A table of `int` overflows on Fibonacci-like recurrences quickly
use `long long` or modular arithmetic.
Recursive memoization still uses `O(n)` stack
bottom-up avoids that.
Sentinel "infinity" must not overflow when you add one
`INT_MAX + 1` is undefined. Use a smaller `INF`.