Dynamic programming is a type of computational thinking that has two key properties:
- optimal sub-structure
- optimal solution can be constructed from optimal solutions to smaller sub-problems.
- overlapping sub-problems
Generally, dynamic programming allows for a formula to be derived to find a certain result in a scenario.
Prize Collecting
Let
P[v,k]
be the maximum prize that can be collected starting atv
takingk
steps.
P[v, k]
can be considered as the maximum ofP[w, k - 1] + weight(e)
wheree
is an edge fromw
tov
.Thus, a formula can be derived.
P[v, k] = MAX{ P[w_1, k_1] + weight(v, w_1), ...... }
Approaches
Bottom-down
- Solve smallest problem
- Combine smaller problems.
- Repeat
- Solve root problem
Top-down
- Start at root and recurse
- Solve and memoize.
General Strategy
- Identify optimal substructure
- Define sub-problems
- Solve problems using sub-problems