# Dynamic Programming Flashcards

1
Q

Which class of problems can DP solve efficiently?

A

Optimization problems. Like minimizing something, maximizing something, best of something etc. Some examples are Shortest Path, Maximum subsequence of two strings, Knapsack etc.

2
Q

Which are two ways to solve DP problems?

A

Bottom Up or Top Down

3
Q

Define Bottom Up approach to DP?

A

You compute base case first and store that in table and then next moves are dependent on base case. You compute dp[0] first, dp[1] and then till dp[n]. dp[n] is our answer. So all subsequent steps are dependent on some calculation we have already performed.

4
Q

Define Top Down approach to DP?

A

Recursion based solution. dp(n) -> dp(n-1) -> … dp(0).

5
Q

Which is the first step to solve a DP problem

?

A

First step is to figure out subproblems which can help to solve the original problem. They don’t have to be same as original problem at hand, but should somehow help in achieving to the solution. Then memoize the answer to subproblem so that if we have to solve it again we can reuse the answer.

6
Q

Running time of DP problem?

A

No of subproblems * Time to solve each subproblem. So for fibonacci the running time will be n (subproblems) * O(1) constant time per subproblem (multiplication). Here we ignore the recursive call overhead (for the memoized calls).

7
Q

What kind of sort are we doing for subproblems in DP?

A

We are doing a “topological sort” of the subproblems in the DAG (Directed Acyclic Graph) of subproblem dependency.

8
Q

How much space do you need for memoization of fibonacci?

A

If we create a DAG and write code you will realize that you only need to remember last two values. So we can achieve in constant space and linear time.

9
Q

Which is a tool that you have when you don’t know the answer but you would like to know?

A

Guessing. Guess and optimize for the best guess. So we guess but intelligently. Algorithmically we don’t just guess one, we guess all guesses. So this is similar to Brute Force but carefully.

10
Q

Which are the central concepts in DP?

A

DP is equivalent to recursion+ memoization + guessing

11
Q

Which is the one thing needed for memoization to work?

A

Its that the subproblem dependencies should be acyclic. Otherwise we get an infinite algorithm.