Prove weighted independent set DP has optimal substructure
Prove the optimal-substructure recurrence for a maximum-weight independent set
on a path. The input is JSON of the form { "weights": number[] }. For a
prefix ending at vertex i, an optimum either skips i, or takes i and
combines it with an optimum for the prefix ending at i - 2. Thus, with
F(0) = 0 and F(1) = weights[0], prove
F(i) = max(F(i - 1), F(i - 2) + weights[i - 1]).
This statement is for a path, not an arbitrary graph. The executable companion computes the maximum weight for the supplied path, but that computation is evidence for examples only and is not a substitute for the proof.
Examples
Input: "{\"weights\":[1,4,5,4]}"
Output: 8
Input: "{\"weights\":[2]}"
Output: 2
Hints
Recall the definition of optimal substructure and how it applies to dynamic programming solutions.
Consider how the solution to a problem of size n can be constructed from solutions to smaller subproblems in the weighted independent set problem.
Analyze the recursive case of the DP solution for the weighted independent set problem and prove that the optimal solution for a problem of size n includes the optimal solutions to its subproblems.
Prove weighted independent set DP has optimal substructure
Prove the optimal-substructure recurrence for a maximum-weight independent set