Dynamic Programming/Greedy Flashcards
1
Q
Dynamic Programming
A
- Algorithmic outline use to solve a certain class of problems
- Usually used to solve problems that can be modelled as a directed acylic graph
- Uses shortest paths in DAG algorithm to compute distance to a subproblem
- Such problems involve making “decisions” across a certain “time” axis
2 Requirements
- Overlapping subproblems
- Optimal substructure
2
Q
Subproblem
A
- An instance of the main problem with a “smaller” input
- Each vertex represents a subproblem
- Each vertex should have a unique input
- Solution to subproblem is usually “distance to” that vertex
- Subprombles must also be acyclic
- Subpromlues usually come in the form:
- Prefix/Suffix ( O(n) )
- Substring ( O(n2) )
3
Q
Overlapping Subproblems
A
- A problem is said to have overlapping subproblems if the subproblems repeat
- If subproblems don’t repeat, then its not dp, its divide and conquer
4
Q
Optimal Substructure
A
- A problem has optimal substructure if the solution to the main problem can be solved by combining solutions to the same problem with “smaller” inputs
- Both greedy and dynamic programming problems must exhibit optimal substructure
Ex:
- Shortest path from A-C = Path(A-B) + Path(B-C)
- optimal solution to the problem can be constructed from optimal solutions to subproblems
- Optimal solutions to a problem incorporate optimal solutions to related subproblems
- Optimal solution to problems contains, within it, optimal solutions to subproblems
- Those subproblems can be solved independently
- Necessary condition for dynamic programming and greedy algorithms
- ex:
- fibo(10) = fibo(9) + fibo(8)
- Steps
- Show that a solution to a subproblem consists of making a choice
5
Q
Memoization
A
- Using a “memo” to keep track of solutions to a subproblem
- Subproblem dependencies must be acyclic for memoization to work (otherwise, solution would get updated more than once)
6
Q
Recurrence Relationship
A
- the relationshi betwen subproblems
- the ability to write the solution to one problem in terms of the soluton to other problems
Ex:
- in shortest paths, shortest distance to point D is minimum of all distances to A,B,C + their edges
7
Q
Top Down
A
- An approach to DP that uses recursion/DFS
Steps
- Establish recurrence relationship base case
- Use DFS to minimize distance to vertex (that subproblem) across incoming edges
- Record solution in memo
- When unwinding DFS, algorithm will end up considering vertices in toplogical order
8
Q
Bottom Up
A
- An approach to dp that uses iteration to solve subproblems in topoligical order
- Basically finding shortest paths iteratively
Steps
- Determine the topological order of the vertices (subproblems)
- Loop through vertices in topological order
- At each vertex, minimize over incoming edges, store dist_to (solution) in dp table
- Final dist_to of incoming adjacent vertices should be known because you’re going in topological order
9
Q
Greedy
A
- Similar to DP, in theory
- May be implement completed differenly in practice
- Instead of considering all ‘choices’, you select one choice
- Generally, greedy algorithms can be rephrased as DP problems by including all choices
2 Requirements
- Greedy choice property
- Optimal substructure
Ex
- Fractional knapsack
- Pour in all the gold, then all the silver, etc.
10
Q
Greedy Choice Property
A
- Similar to optimal substructure
- Making a greedy choice never eliminates the optimal solution
- This property says that globally optimal solution can obtained by making a locally optimal solution (greedy)