Dynamic Programming
- 49Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
Why do algorithms for inputs of arbitrary size generally use recursion, loops, or another form of repeated computation?
Recursive algorithm design expresses a problem of arbitrary size using a fixed amount of code that reuses instructions through recursion, loops, or both.
Or swipe the card
49 cards
Why do algorithms for inputs of arbitrary size generally use recursion, loops, or another form of repeated computation?
Recursive algorithm design expresses a problem of arbitrary size using a fixed amount of code that reuses instructions through recursion, loops, or both.
What does SRTBOT stand for in recursive algorithm design?
SRTBOT stands for Subproblems, Relations, Topological order, Base cases, Original problem, Time—six steps for specifying a recursive algorithm.
What is the goal when defining subproblems in SRTBOT, and what is often the hardest design challenge?
Choose a collection of subproblems—usually polynomially many—whose solutions can be used to solve the original problem. Finding the right subproblems is often the hardest step.
What does the “Relations” step of SRTBOT specify?
A relation, or recurrence, specifies how to solve a subproblem using solutions to other subproblems.
How can subproblem dependencies be represented, and why must they be acyclic?
Represent subproblems as vertices and dependencies as directed edges. The dependencies must be acyclic so recursive calls terminate.
What is a topological order for subproblems used for?
A topological order lists subproblems so that every dependency is solved before the subproblem that needs it. It provides a valid computation order.
What role do base cases play in a recursive algorithm?
Base cases are subproblems whose solutions can be returned directly, without making further recursive calls.
What must be explained in the “Original problem” step of SRTBOT?
Specify how the original problem is obtained from the subproblem solutions. It may itself be one subproblem, or it may require combining several solutions.
What does the final “Time” step of SRTBOT require?
Analyze the running time after specifying the subproblems, recurrence, computation order, base cases, and original problem.
How can merge sort’s subproblems and original problem be defined using array indices?
For merge sort, define s(i,j) as the sorted items in the array range from index i up to, but not including, j. The original problem is s(0,n).
What is merge sort’s recursive relation, and why is increasing subarray length a valid topological order?
To solve s(i,j), sort the two smaller ranges split at midpoint m, then merge their sorted results. Increasing range length j−i is a valid computation order because m lies strictly between i and j.
What is memoization?
Memoization stores a solved subproblem’s result and reuses it instead of solving that subproblem again.
What information does a memo store, and what are two possible implementations?
A memo can be implemented with a dictionary mapping subproblem specifications to their solutions. A direct-access array or a hash table can be used where appropriate.
What is the usual workflow for a recursive algorithm with memoization?
Check whether the subproblem is in the memo; if so, return its stored result. Otherwise, handle a base case or compute the result recursively, store it, and return it.
What recurrence and base cases define the Fibonacci numbers in this example?
The Fibonacci numbers satisfy fi=fi−1+fi−2, with base cases f1=f2=1.
Why is the straightforward recursive Fibonacci algorithm inefficient?
Naive recursion recomputes shared subproblems, such as the same Fibonacci number reached by different branches. The number of additions grows exponentially with n.
How does memoization prevent repeated work?
Once a subproblem has been solved and stored, later calls return its result directly. Thus, each distinct subproblem is solved at most once.
How can the running time of a memoized recursive algorithm be bounded?
With memoization, a useful running-time bound is the sum, over all distinct subproblems, of the nonrecursive work required to evaluate each one, excluding recursive calls.
Why does memoized Fibonacci computation take O(n) time under the usual unit-cost arithmetic model?
There are n Fibonacci subproblems from 1 through n, and each non-base subproblem requires constant-time work under the usual unit-cost arithmetic model. The running time is therefore O(n).
How does accounting for the bit length of Fibonacci numbers change the running-time analysis?
Fibonacci values grow exponentially and require [0m\Theta(n) bits. In a word-RAM model with word size w, adding such values can take O(1+n/w) time, so the total bit-sensitive time is O(n+n2/w).
What bound results from applying the general memoization analysis to every possible merge-sort interval, and why is it loose?
For merge sort, memoizing every possible interval gives [0m\Theta(n^2) subproblems, each requiring [0m\Theta(j-i) merge work. Summed over all intervals, this gives an O(n3) upper bound, even though ordinary merge sort takes [0m\Theta(n\log n) time.
What are the subproblems for single-source shortest paths in a DAG?
The subproblems are shortest-path distances [0m\delta(s,v) from source s to each vertex v. Solving them for all vertices gives the single-source shortest-path result.
What recurrence computes the shortest-path distance to a vertex v in a DAG?
For a vertex v=s, use δ(s,v)=min({δ(s,u)+w(u,v):(u,v)∈E}∪{∞}). The candidate paths come from incoming edges to v.
Why does the DAG shortest-path recurrence consider incoming edges to each vertex?
Any path to v=s must reach v from some predecessor u along an incoming edge (u,v). The best path through that edge has weight δ(s,u)+w(u,v), so the minimum over predecessors gives the distance.
Why can a topological order be used to compute the DAG shortest-path subproblems?
The recurrence depends on distances to the incoming neighbors. A topological order of the DAG ensures those predecessor distances are computed before the distance to v.
What are the base and unreachable cases for DAG shortest-path distances?
The base case is δ(s,s)=0. If no path reaches a vertex, its distance is ∞.
What is the running time of the DAG shortest-path dynamic program, and why?
The nonrecursive work for each vertex scans its incoming edges. Summed over all vertices, the algorithm takes O(V+E) time.
How is the DAG shortest-path recurrence related to the DAG relaxation algorithm?
DAG relaxation computes the same minimum by processing candidate incoming edges and updating a distance whenever a better path is found. The recurrence groups those updates into a minimum.
What connection does memoized recursion have to depth-first search on the subproblem graph?
In a recursive memoized implementation, the memo also acts like a visited set: recursive calls explore the subproblem graph in depth-first-search fashion, while stored results prevent revisiting solved subproblems.
How many prefixes and suffixes does a sequence of length n have, asymptotically?
For a sequence of length n, there are Θ(n) prefixes and Θ(n) suffixes. Each is determined by one boundary position.
What is a substring subproblem, and how many are possible in a sequence of length n?
A substring is a contiguous range specified by two boundaries. There are Θ(n2) possible substrings.
How does a subsequence differ from a substring, and why are all subsequences usually poor DP subproblems?
A subsequence may omit elements between selected positions. A sequence of length n has 2n possible subsequences, so using all of them as subproblems generally creates an exponential number.
Why are prefixes or suffixes often preferred to substrings as sequence subproblems?
Prefixes and suffixes often provide enough subproblems and are usually preferable to substrings because they give only linearly many states rather than quadratically many.
Which part of a sequence subproblem is often useful to focus on when designing a recurrence for prefixes, suffixes, or substrings?
With suffixes, focus on the first item or first few items; with prefixes, focus on the last item. For a substring, considering an interior item can split the remainder into two substrings.
How are the bowling subproblems, original problem, and empty-suffix base case defined?
Let b(i) be the maximum score obtainable from pins i through n−1. The original problem is b(0), and the empty suffix has value b(n)=0.
What are the possible actions involving the first pin of a bowling suffix?
The ball can skip pin i, hit it alone for vi points, or hit it with pin i+1 for vivi+1 points.
What recurrence computes the maximum score for a bowling suffix?
For i<n−1, b(i)=max{b(i+1), b(i+1)+vi, b(i+2)+vivi+1}. If only one pin remains, the pair option is unavailable.
Which recurrence option represents skipping pin i in the bowling problem?
Skipping pin i leaves the suffix starting at i+1, with score b(i+1).
Which recurrence option represents hitting pin i alone?
Hitting pin i alone adds vi to the best score on the remaining suffix, giving b(i+1)+vi.
Which recurrence option represents hitting two adjacent pins together?
Hitting pins i and i+1 together earns vivi+1 and leaves the suffix starting at i+2, giving b(i+2)+vivi+1.
Why can the bowling dynamic program be computed in linear time, and in what order?
There are n+1 suffix subproblems and constant nonrecursive work per subproblem. Computing them in decreasing order of i takes O(n) time.
How does the bowling recurrence use local brute force without taking exponential time?
The recurrence checks every possible action for the first pin and chooses the best. Because the suffix subproblems are reused rather than recomputed, the overall algorithm is linear rather than exponential.
How can the bowling dynamic program be implemented bottom-up?
A bottom-up implementation initializes b(n)=0, fills the table for i=n−1,n−2,…,0, uses only valid options at the end of the sequence, and returns b(0).
How are alternative choices combined in a dynamic-programming recurrence?
The recurrence is an optimization over candidate choices: use a maximum when maximizing and a minimum when minimizing. Other problems may combine options with operations such as OR or AND.
What is bottom-up dynamic programming, and when is it useful?
If the computation order can be expressed as loops, replace recursive calls with a table initialized at the base cases and fill it in topological order. This avoids recursive-call overhead.
What is a general strategy for discovering a dynamic-programming recurrence?
Identify a feature of a solution that, once chosen or known, leaves a smaller subproblem. Enumerate the possible values or choices for that feature and combine their results.
How can dynamic programming turn exploration of many choices into polynomial-time computation?
Dynamic programming can examine many local choices while reusing a polynomial-sized set of subproblem solutions. A polynomial number of states and polynomial work per state yield a polynomial-time algorithm.
What two core ideas together define dynamic programming in this framework?
Dynamic programming is recursive algorithm design combined with memoization: define subproblems and a recurrence, then store and reuse each solved subproblem.
What key conditions support a polynomial-time dynamic-programming solution?
A DP is useful when its subproblems cover what is needed for the original problem, the recurrence reduces to valid smaller subproblems, and there are polynomially many states with polynomial work per state.

Ask anything
We've read this whole study set, so questions about it get the best answers.