Skip to main content

Proofs: Dynamic Programming

A dynamic programming algorithm is correct if its recurrence is correct: the optimal value of each subproblem must be obtained from the optimal values of smaller subproblems. This page proves the recurrences used in the lessons.


The 0/1 knapsack recurrence is correct​

Claim. The value V(i,j)V(i, j) defined by the recurrence of the lesson is the maximum value obtainable with the first ii objects and capacity jj.

Proof. By induction on ii. For i=0i = 0 there are no objects and the value is 0. For i>0i \gt 0, consider an optimal selection SS for (i,j)(i, j). Either object ii is not in SS, and then SS is a selection of the first i−1i - 1 objects with capacity jj, so its value is at most V(i−1,j)V(i-1, j); or object ii is in SS, and then S∖{i}S \setminus \{i\} is a selection of the first i−1i - 1 objects with capacity j−wij - w_i, so its value is at most V(i−1,j−wi)+viV(i-1, j - w_i) + v_i. In both cases the value is at most the maximum of the recurrence. Conversely, both options of the maximum correspond to valid selections. So the maximum is exactly the optimal value. ■\blacksquare

This is the principle of optimality in action: the part of an optimal solution that concerns the first i−1i - 1 objects is itself optimal for the capacity it leaves. Filling the (n+1)×(W+1)(n+1) \times (W+1) table costs O(nW)O(nW).


Pascal's rule​

Claim. (nk)=(n−1k−1)+(n−1k)\binom{n}{k} = \binom{n-1}{k-1} + \binom{n-1}{k} for 0<k<n0 \lt k \lt n.

Proof. Count the subsets of kk elements of {1,…,n}\{1, \dots, n\} according to whether they contain element nn. Those that contain it are formed by nn plus k−1k - 1 of the other n−1n - 1 elements: (n−1k−1)\binom{n-1}{k-1}. Those that do not are kk of the other n−1n - 1: (n−1k)\binom{n-1}{k}. ■\blacksquare

The recursive algorithm that applies this rule reaches the base cases (m0)=(mm)=1\binom{m}{0} = \binom{m}{m} = 1 exactly (nk)\binom{n}{k} times (each base case returns 1, and the result is their sum), so it makes 2(nk)−12\binom{n}{k} - 1 calls, which is exponential for k=n/2k = n/2. The table of dynamic programming has only O(nk)O(nk) cells.


The LCS recurrence is correct​

Let L(i,j)L(i, j) be the length of the longest common subsequence of the prefixes X[1..i]X[1..i] and Y[1..j]Y[1..j], with L(i,0)=L(0,j)=0L(i, 0) = L(0, j) = 0.

Claim. If xi=yjx_i = y_j, then L(i,j)=L(i−1,j−1)+1L(i, j) = L(i-1, j-1) + 1; otherwise L(i,j)=max⁡(L(i−1,j),L(i,j−1))L(i, j) = \max(L(i-1, j), L(i, j-1)).

Proof. If xi=yjx_i = y_j: an LCS of the two prefixes can always be chosen to end with that common character (if it does not use it, its last character can be replaced by it, or it can be appended), and removing it leaves a common subsequence of X[1..i−1]X[1..i-1] and Y[1..j−1]Y[1..j-1]. So L(i,j)=L(i−1,j−1)+1L(i, j) = L(i-1, j-1) + 1. If xi≠yjx_i \ne y_j: a common subsequence cannot end with both xix_i and yjy_j, so it does not use xix_i or it does not use yjy_j, and it is a common subsequence of (X[1..i−1],Y[1..j])(X[1..i-1], Y[1..j]) or of (X[1..i],Y[1..j−1])(X[1..i], Y[1..j-1]). The maximum of the two is the answer. ■\blacksquare

Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.