RareProgramming
What feature makes a problem a good candidate for dynamic programming?
Short answer
Subproblems overlap
Exercise
- Subproblems never repeat
- It asks for a shortest path
- Subproblems overlap
- It involves strings
- Exhaustive search is always faster
Solution
The same subproblem can be solved once and its result reused.
In a recursive calculation of F(5), F(3) is requested directly and inside F(4). What repeats is the same question with the same input. Saving F(3) lets both branches reuse its answer. The mere presence of strings or paths in a problem does not establish that any subproblem repeats.
F(5) = F(4) + F(3), F(4) = F(3) + F(2). F(0) = 0, F(1) = 1, F(2) = 1, F(3) = 2, F(4) = 3, F(5) = 5.
A state must uniquely specify the remaining subproblem. Different routes through the computation that reach the same state can reuse the same previously computed value.
Platforms
- MAXIMUM
