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 defined by the recurrence of the lesson is the maximum value obtainable with the first objects and capacity .
Proof. By induction on . For there are no objects and the value is 0. For , consider an optimal selection for . Either object is not in , and then is a selection of the first objects with capacity , so its value is at most ; or object is in , and then is a selection of the first objects with capacity , so its value is at most . 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.
This is the principle of optimality in action: the part of an optimal solution that concerns the first objects is itself optimal for the capacity it leaves. Filling the table costs .
Pascal's rule
Claim. for .
Proof. Count the subsets of elements of according to whether they contain element . Those that contain it are formed by plus of the other elements: . Those that do not are of the other : .
The recursive algorithm that applies this rule reaches the base cases exactly times (each base case returns 1, and the result is their sum), so it makes calls, which is exponential for . The table of dynamic programming has only cells.
The LCS recurrence is correct
Let be the length of the longest common subsequence of the prefixes and , with .
Claim. If , then ; otherwise .
Proof. If : 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 and . So . If : a common subsequence cannot end with both and , so it does not use or it does not use , and it is a common subsequence of or of . The maximum of the two is the answer.
Errors, suggestions and public questions open a form on GitHub, visible to everyone.