Dynamic programming Flashcards
What is the basic idea behind dynammic programming?
- Dynammic programming solves problems by combining the solutions to subproblems.*
- Dynamic programming is applicable when the sub problems are not independent, that is, when sub problems share subsubproblems.*
- A dynamic-programming algorithm solves every subsubproblem just once and then saves its answer in a table.*
- Dynamic programming is typically applied to optimization problems. There are many solution, and we wish to find an optimal one.*
What are the 4 steps of dynamic programming?
- Characterize the structure of OPT*
- Recursively define the value of OPT*
- Compute the value of OPT in a bottom-up fashion*
- Construct OPT from computed information*
- Step 4 can be ommited if only the value of an optimal solution is required.*
- When we do perform step 4, we sometimes maintain additional information during step 3 to ease the construction of OPT.*
What shall we do if we found a working recursion which is not efficient?(becuase it computes the same subproblem over and over again)
- We arrange for each subproblem to be solved only once, saving its solution.*
- Dynamic programming uses additional memory to save computation time*
- Dynamic programming approach runs in polynomial time when the number of* distinct sub problems involved is polynomial in the input size.
There are usually two equivalent ways to implement a dynamic-programming approach. What are they?
top-down with memoization
- we write the procedure recursively in a natural manner, but modified to save the result of each subproblem.(usually in an array)
- this method first checks to see whether it has previously solved this problem. If so, it returns the saved value, else it computes it.
bottom-up method
- this method is derived from the notion that solving any particular subproblem depends only on solving “smaller” subproblems.
- we sort the subproblems by size and solve them in size-order, smallest first. When solving a partricular subproblem we already solved all of the smaller problems its solution depends upon, and we have saved their solutions.
Rod cutting problem
can you describe the recursive alogirthm which runs in exponential time?
Rod cutting problem
Describe top-down with memoization algorithm
Rod cutting problem
describe the bottom-up algorithm
- Rod cutting problem*
- describe the extended bottom-up algorithm, where we wish to print the solution.*
What are the steps in solving a problem using dynammic programming?
Matrix-chain multiplication
give a formal definition of the problem
Matrix-chain multiplication
describe the subproblems and OPT
Matrix-chain multiplication
describe the formula
Matrix-chain problem
What are the base cases?
- The base cases are subproblems which suggest an immidiate result.*
- In the Matrix-chain problem, every instance of the problem with only one matrix is a base case. There is only one solution which is 0 - no multiplication is needed.*
- Matrix-chain problem*
- what is the proof for the formula?*
- *notice how we start with OPT[i,j] and using definitions (and a theorem), we end up with a recursive defintion, as in the formula.*
Matrix-Chain
describe the top-down solution