Dynamic Programming
Last updated
Dynamic Programming is basically DFS with memoization.
We can use DFS to solve the DP problem in small scale, but when the input size is large, plain DFS will be too slow because we recompute the result for some specific states over and over again, wasting time.
Top-down DP:
= DFS + memo
It's recursive
We start from the original problem, and search into sub-problems (i.e. use the results of the sub-problems).
If we visualize the DFS search as a tree, top-down DP is going from the root to leaf nodes.
Bottom-up DP:
It's iterative
We start from resolving the minimal sub-problems, then use those results to build up solutions for sub-problems with larger scale, until we get enough results to build the solution for the original problem.
If we visualize the DFS search as a tree, bottom-up DP is going from the leaf nodes to the root.
it involves searching, especially DFS. We can use DFS to solve the problem in small scale
it only cares about the optimal solution -- Optimal substructure
During searching, we might go into the same state from different paths -- Overlapping sub-problems.
Think in DFS way. Identify the states we need to use in each DFS invocation.
Define the transition between states.
Define special/trivial cases.
We scan the sequence one by one, make decisions at each step.
Second Type 375 1246
Usually used to solve NP-hard problems on small scale dataset, faster then search (DFS or BFS).
Example: Travelling salesman problem (TSP)
Last updated