Professional Documents
Culture Documents
Lab5 - Notes On Dynamic Programming
Lab5 - Notes On Dynamic Programming
You can consider "dynamic programming" as "careful/controlled brute force". What we usually
do is that we basically solve the problem using brute force and add memoization after we have
solved the problem. So you are given a problem, what you should do is NOT think about
memoization at first but instead you should think about solving it using brute force then add
memoization. For optimization problems, we want to write a function that returns the optimal
solution according to some variables that are sent to the function.
1. First of all, you need to identify problem variables. This can be, for example, the health of a
monster as in the lab or the current location (i,j) in the grid problem as in the assignment. These
variables are used as inputs to the function and will be used for memoization later on. What I mean
by variables are the things that change from one sub-problem to another, for example if we have
an input to a function which is the power of each weapon. The power of each weapon does not
change from one sub-problem to another so it's not considered as of the problem variables (though
they should be sent to the function) the health of a monster changes from one sub-problem to
another, so this is considered a variable.
2. Then you should define sub-problems. Assuming you're trying to draw a recursion tree for the
monster killing problem, if we think about each monster individually, the root node of the tree will
contain the starting health of the monster. What I mean by defining sub-problems is what will be
the children of the root node. If we have 3 weapons, we want to try all of them since we want to
try all possibilities since it's brute force. So the root node will have 3 children, one for each weapon
and the node will have value = current health - weapon power.
3. The next step is to relate sub-problems solutions. What I mean by that is, if we have 3 weapons,
this means we have 3 sub-problems, but how can we relate these sub-problems and return the
optimal solution? This is usually done for optimization problems where we try to find a minimum
or maximum value. So if we have 3 sub-problems, we relate them by a function (for example MIN
or MAX) or (MAX + something). You should assume that these sub-problems return the optimal
solution and you think what is the optimal solution for the original problem given these sub-
problems. So, you relate these sub-problems by a function that returns the optimal solution for the
problem.
Steps 1 to 4 is what we used to do throughout the whole semester, which is brute force. Step 5 is
the new part added for dynamic programming.