1. Home
  2. Explore
  3. Computer Science
  4. Dynamic Programming

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Dynamic Programming

  • 47Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Recursive Design

Why is recursive algorithm design a useful way to describe algorithms that handle inputs of arbitrary size?

47 questions

  1. 1

    Why is recursive algorithm design a useful way to describe algorithms that handle inputs of arbitrary size?

    Model answer

    A recursive algorithm describes how to solve a problem using smaller subproblems, reusing a fixed set of instructions for inputs of arbitrary size. The course uses this view because it aligns with induction proofs and makes subproblem dependencies explicit.

    Explanation

    Recursive design expresses a problem in terms of smaller instances, allowing a fixed algorithm to handle inputs of arbitrary size.

  2. 2

    What is dynamic programming, and what basic idea does it add to recursive algorithm design?

    Model answer

    Dynamic programming is a powerful and general algorithm-design paradigm built on recursive problem solving. It can solve many problems by reusing solutions to subproblems.

    Explanation

    The lecture treats dynamic programming as a general recursive design paradigm and emphasizes its wide applicability.

  3. 3

    What does the acronym SRTBOT stand for?

    Model answer

    SRTBOT stands for Subproblems, Relations, Topological order, Base cases, Original problem, and Time. These steps organize the design and analysis of a recursive algorithm.

    Explanation

    SRTBOT gives a checklist for specifying a recursive algorithm: subproblems, relations, topological order, base cases, original problem, and time.

  4. 4

    What are subproblems in the SRTBOT framework, and why is choosing them important?

    Model answer

    Subproblems are the smaller or related problems whose solutions will help solve the target problem. Choosing them is often the hardest part, and a useful design generally has only polynomially many.

    Explanation

    Defining the subproblems is often the most challenging design step; their number should generally be polynomial.

  5. 5

    What role does the relation step play in a recursive algorithm?

    Model answer

    A recurrence relation describes how to solve a subproblem using solutions to other, typically smaller, subproblems. It supplies the recursive structure of the algorithm.

    Explanation

    A relation specifies how the solution to a subproblem can be obtained from solutions to other subproblems.

  6. 6

    How does the subproblem graph help establish that a recursive algorithm will terminate?

    Model answer

    The subproblem graph has one vertex per subproblem and edges representing dependencies. Its dependencies must be acyclic so recursive calls terminate; a topological order gives a valid order for solving them.

    Explanation

    The subproblem graph has subproblems as vertices and dependency edges; cycles would cause recursive calls to continue indefinitely.

  7. 7

    What does a topological order mean for a set of dependent subproblems?

    Model answer

    A topological order is an ordering in which each subproblem is solved after the subproblems it depends on. It can be specified explicitly, often with a loop, or established by another proof.

    Explanation

    A topological order arranges dependent subproblems so that prerequisites are solved first.

  8. 8

    What purposes do the base-case and original-problem steps serve in SRTBOT?

    Model answer

    Base cases specify subproblems that can be solved without further recursive calls. The original-problem step states how the desired answer is obtained from the defined subproblems.

    Explanation

    Base cases stop recursion, while the original-problem step identifies which subproblem solution answers the input question.

  9. 9

    What is examined in the time step of SRTBOT?

    Model answer

    The time step analyzes the running time of the recursive algorithm. In particular, it accounts for the number of subproblems and the work used to compute each one.

    Explanation

    The final SRTBOT step analyzes the algorithm’s running time after its subproblems and dependencies have been specified.

  10. 10

    How can merge sort be described using subproblems and a recurrence relation?

    Model answer

    Define a subproblem as sorting the array range from index iii up to, but not including, index jjj. The original problem is the range from 000 to nnn, and its solution is obtained by merging the sorted ranges split at the midpoint.

    Explanation

    Merge sort divides the array into two smaller ranges, sorts them, and merges their sorted results.

  11. 11

    What is a valid topological order for merge sort’s subproblems, and what is its base case?

    Model answer

    A valid order is increasing subarray length, because each recursive call sorts a strictly shorter range. The base case is an empty range, which is already sorted.

    Explanation

    The range length decreases in recursive calls, so solving ranges in order of increasing length places prerequisites before the ranges that need them.

  12. 12

    Why does memoization not improve merge sort, and what loose bound results from counting all possible ranges?

    Model answer

    Memoization is unnecessary for merge sort because its recursive calls do not contain repeated subproblems. Counting all possible ranges and the cost of merging each gives a loose cubic upper bound, rather than its actual nlog⁡nn\log nnlogn running time.

    Explanation

    Merge sort does not revisit the same range in its usual recursion, so memoizing ranges adds overhead without useful reuse.

  13. 13

    Give the Fibonacci recurrence and explain why directly implementing it recursively is inefficient.

    Model answer

    The straightforward recursion uses fi=fi−1+fi−2f_i=f_{i-1}+f_{i-2}fi​=fi−1​+fi−2​ with f1=f2=1f_1=f_2=1f1​=f2​=1. Its call tree repeats subproblems, leading to exponential running time.

    Explanation

    The Fibonacci recurrence is a natural recursive definition, but its call tree repeats the computation of many terms.

  14. 14

    What subproblems, original problem, and base cases can be used to compute the nnnth Fibonacci number?

    Model answer

    Use the values fif_ifi​ for indices from 111 through nnn as the subproblems. The desired answer is fnf_nfn​, and the base cases are f1=f2=1f_1=f_2=1f1​=f2​=1.

    Explanation

    There are only nnn Fibonacci subproblems up to the requested index, even though the naïve recursion makes exponentially many calls.

  15. 15

    What is a valid topological order for the Fibonacci subproblems, and why does it work?

    Model answer

    Compute Fibonacci subproblems in increasing index order, starting with the base cases. Each term then depends only on terms already computed.

    Explanation

    The recurrence’s dependencies run from smaller indices to larger ones, so increasing index is a valid computation order.

  16. 16

    What is memoization, and how does a memo table use it?

    Model answer

    Memoization stores the solution to each subproblem and checks the store before computing it. If the answer is already present, the algorithm returns it instead of making the recursive calls again.

    Explanation

    The memo stores a solved subproblem’s answer so later requests can return that value without repeating the recursive work.

  17. 17

    What information does the memo data structure store, and name two possible implementations.

    Model answer

    The memo can be implemented as a dictionary or, when identifiers permit, a direct-access array; a hash table is also possible. It maps each solved subproblem to its solution.

    Explanation

    A dictionary or direct-access array can map subproblem identifiers to their computed answers; a hash table is another option.

  18. 18

    How does memoization change the number of Fibonacci computations, under the usual constant-time addition assumption?

    Model answer

    With memoization, each Fibonacci subproblem is solved at most once, so only a linear number of subproblems and additions are needed. The running time is O(n)O(n)O(n) when arithmetic operations are treated as constant time.

    Explanation

    After the first recursive call computes a needed Fibonacci term, later calls for that term can retrieve it from the memo.

  19. 19

    Describe the standard sequence of steps in a recursive algorithm with memoization.

    Model answer

    First check whether the subproblem is in the memo and return its solution if so. Otherwise solve a base case or recursively evaluate the relation, store the result, and return it.

    Explanation

    Memoized recursion checks for a stored solution, handles a base case if necessary, otherwise evaluates the recurrence and stores the result.

  20. 20

    How can the running time of a memoized recursive algorithm be bounded?

    Model answer

    Sum the nonrecursive work over all distinct subproblems, since each is solved only once. If there are polynomially many subproblems and polynomial nonrecursive work per subproblem, this gives a polynomial-time bound.

    Explanation

    The memoized running-time bound adds the nonrecursive cost for each distinct subproblem, because recursive subproblem work is counted separately.

  21. 21

    Why may Fibonacci additions take more than constant time in a bit-complexity analysis?

    Model answer

    The nnnth Fibonacci number has Θ(n)\Theta(n)Θ(n) bits, so its addition is not necessarily constant time when the values exceed the machine word size www. Adding nnn-bit values takes O(⌈n/w⌉)O(\lceil n/w\rceil)O(⌈n/w⌉) time in the stated word model.

    Explanation

    Fibonacci values have exponentially growing magnitude and therefore require a number of bits proportional to the index.

  22. 22

    What running-time bound does the lecture give for memoized Fibonacci when the cost of multi-bit addition is included?

    Model answer

    With memoization, there are linearly many additions, each on values requiring Θ(n)\Theta(n)Θ(n) bits near the end of the computation. The stated total bound is O(n+n2/w)O(n+n^2/w)O(n+n2/w), which is polynomial, unlike the naïve recursive algorithm’s exponential time.

    Explanation

    There are linearly many Fibonacci subproblems, and each addition on the growing values costs about the number of words needed to represent them.

  23. 23

    What are the subproblems, original task, and base case for single-source shortest paths in a DAG?

    Model answer

    For each vertex vvv, define a subproblem as the shortest-path weight from source sss to vvv. The original problem asks for these values for all vertices, and the base case is δ(s,s)=0\delta(s,s)=0δ(s,s)=0.

    Explanation

    A DAG’s acyclicity supplies a topological order, and the shortest path to a vertex can be obtained by considering its possible last edges.

  24. 24

    Explain the recurrence for the shortest-path weight from a source to a vertex in a DAG.

    Model answer

    For each incoming edge (u,v)(u,v)(u,v), consider the shortest-path weight to uuu plus the weight of (u,v)(u,v)(u,v). The distance to vvv is the minimum of these candidates, including ∞\infty∞ if no path reaches vvv.

    Explanation

    Any path reaching vvv must arrive along an incoming edge from some predecessor uuu, so minimizing over those choices captures the best path.

  25. 25

    Why does a topological ordering allow the DAG shortest-path recurrence to be evaluated?

    Model answer

    Use a topological order of the DAG. The shortest-path subproblem for each vertex can then be computed after the subproblems for the vertices with edges into it.

    Explanation

    A topological ordering ensures that a vertex’s predecessors are processed before it, allowing their distances to be used in its recurrence.

  26. 26

    What is the running time of the DAG shortest-path dynamic program, and how is it obtained?

    Model answer

    The nonrecursive work for a vertex is proportional to its incoming-edge count, plus constant overhead. Summed over all vertices, the running time is O(V+E)O(V+E)O(V+E).

    Explanation

    The work for a vertex is proportional to the number of its incoming edges, and summing those degrees counts every edge once.

  27. 27

    How is the recurrence-based DAG shortest-path algorithm related to DAG relaxation?

    Model answer

    The recurrence-based algorithm and DAG relaxation compute the same minimum over incoming edges. The recurrence views the minimum as one operation, while relaxation builds it through successive distance updates.

    Explanation

    DAG relaxation updates a vertex’s distance whenever an edge gives a better route; processing all incoming edges computes their minimum candidate.

  28. 28

    What graph-search behavior is implicit in a memoized recursive dynamic program?

    Model answer

    Memoized recursion performs a depth-first search of the subproblem graph in the direction from a problem to the smaller problems it calls. The memo acts like the visited check, and computation completes in an order compatible with the dependencies.

    Explanation

    The recursive calls follow dependencies backward from a larger subproblem to its prerequisites, while memoized results prevent revisiting already explored vertices.

  29. 29

    Compare the number of prefix, suffix, substring, and subsequence subproblems for an input sequence of length nnn.

    Model answer

    Natural choices are all prefixes, all suffixes, or all contiguous substrings. They give Θ(n)\Theta(n)Θ(n), Θ(n)\Theta(n)Θ(n), and Θ(n2)\Theta(n^2)Θ(n2) subproblems respectively, whereas arbitrary subsequences can give 2n2^n2n.

    Explanation

    For a sequence of length nnn, prefixes and suffixes have one parameter, while substrings have two; arbitrary subsequences have exponentially many possibilities.

  30. 30

    Why are prefixes and suffixes often preferable to subsequences as dynamic-programming subproblems?

    Model answer

    Prefixes and suffixes are often preferable because they yield only linearly many subproblems. Arbitrary subsequences are usually unsuitable when they create exponentially many subproblems.

    Explanation

    The number of possible subproblems is a key constraint for efficient dynamic programming; subsequences can make that number exponential.

  31. 31

    Distinguish a substring from a subsequence, and describe how prefixes and suffixes relate to a sequence.

    Model answer

    A prefix contains the sequence up to a chosen position, a suffix contains the sequence from a chosen position onward, and a substring contains all consecutive items between two positions. A subsequence may omit items in between.

    Explanation

    A prefix or suffix is identified by one boundary, whereas a contiguous substring is identified by its start and end boundaries.

  32. 32

    Which boundary or position is typically examined when designing recurrences for suffixes, prefixes, and substrings?

    Model answer

    For suffix subproblems, consider the first item or first few items; for prefix subproblems, consider the last item. For a substring, removing an item from the middle can leave two smaller substrings.

    Explanation

    For suffix-based recurrences, removing the first item or first few items leaves another suffix, so the recurrence stays within the chosen subproblem family.

  33. 33

    How should a designer choose between prefix-based and suffix-based subproblems when both seem applicable?

    Model answer

    Either often works because both produce linearly many subproblems. Choose the direction that makes the recurrence and its progression through the input more natural.

    Explanation

    Prefixes and suffixes usually provide comparable subproblem counts, so the choice can be based on which direction makes the recurrence clearer.

  34. 34

    What constraint should guide the choice of a dynamic program’s subproblem family?

    Model answer

    A useful subproblem family should keep the number of subproblems polynomial in the input size. Choosing arbitrary subsequences can violate this requirement because there may be 2n2^n2n of them.

    Explanation

    A dynamic program needs a polynomial-sized set of subproblems; defining subproblems as arbitrary subsequences would already produce exponentially many.

  35. 35

    State the scoring rules and objective of the linear bowling problem.

    Model answer

    Hitting pin iii alone scores viv_ivi​, while hitting neighboring pins iii and i+1i+1i+1 together scores vivi+1v_i v_{i+1}vi​vi+1​. The player may skip pins and seeks to maximize the total score.

    Explanation

    The game awards the value of a single pin or the product of two neighboring pins, and a player may leave pins unhit.

  36. 36

    What constraints do the bowling rules place on which pins can be hit together or left unused?

    Model answer

    A hit can cover one pin or two adjacent pins, and pins knocked down cannot be used in another hit. The player can also leave a pin unhit, so not every pin must be covered.

    Explanation

    Because a ball can hit only one pin or a neighboring pair, selected hits cannot overlap; skipping is also permitted.

  37. 37

    Define the bowling suffix subproblem and identify which one gives the answer for the full game.

    Model answer

    Let b(i)b(i)b(i) be the maximum score obtainable from the suffix of pins starting at index iii. The original problem is b(0)b(0)b(0), the maximum score for all pins.

    Explanation

    The suffix starting at index iii is a valid subproblem, and the entire input is the suffix starting at 000.

  38. 38

    What choices for the first pin produce the bowling recurrence?

    Model answer

    For the first pin in a suffix, either skip it, hit it alone, or hit it together with the next pin. These choices lead respectively to the suffix beginning at i+1i+1i+1, that suffix plus viv_ivi​, or the suffix beginning at i+2i+2i+2 plus vivi+1v_i v_{i+1}vi​vi+1​.

    Explanation

    The three possibilities for the first pin exhaust the choices: skip it, hit it alone, or hit it with its neighbor.

  39. 39

    Write the recurrence and base case for the bowling dynamic program, including the condition on the pair option.

    Model answer

    For i<n−1i<n-1i<n−1, b(i)=max⁡{b(i+1), b(i+1)+vi, b(i+2)+vivi+1}b(i)=\max\{b(i+1),\ b(i+1)+v_i,\ b(i+2)+v_i v_{i+1}\}b(i)=max{b(i+1), b(i+1)+vi​, b(i+2)+vi​vi+1​}. If only one pin remains, omit the pair option; the empty suffix has value b(n)=0b(n)=0b(n)=0.

    Explanation

    The recurrence takes the best of the three legal choices, and the pair choice is available only when another pin remains.

  40. 40

    Why is the bowling base case b(n)=0b(n)=0b(n)=0?

    Model answer

    Set b(n)=0b(n)=0b(n)=0, because the suffix beginning after the final pin contains no pins and yields no points.

    Explanation

    The empty suffix contains no pins and therefore cannot score any points.

  41. 41

    Why does the bowling dynamic program run in linear time?

    Model answer

    The nonrecursive work per subproblem is constant: evaluate a small number of candidate scores and take their maximum. With nnn suffixes, the memoized algorithm runs in O(n)O(n)O(n) time.

    Explanation

    The recurrence uses only a constant number of comparisons and arithmetic operations once the smaller suffix values are available.

  42. 42

    What topological order evaluates all bowling suffix subproblems?

    Model answer

    Compute b(i)b(i)b(i) in decreasing order of iii, starting with the empty suffix at nnn and ending at 000. This order ensures the needed values b(i+1)b(i+1)b(i+1) and, when valid, b(i+2)b(i+2)b(i+2) have already been computed.

    Explanation

    The dependency for b(i)b(i)b(i) is on later suffixes, so computing from the end toward the beginning puts those dependencies first.

  43. 43

    How can the bowling dynamic program be implemented bottom-up?

    Model answer

    Initialize the table with b(n)=0b(n)=0b(n)=0, then fill its entries in decreasing index order using the recurrence, and return b(0)b(0)b(0). This is a nonrecursive, bottom-up implementation of the same dynamic program.

    Explanation

    The bottom-up version initializes the base case and fills the table in topological order, avoiding recursive calls and memo lookups.

  44. 44

    Why does trying every local bowling choice not make the dynamic program exponential?

    Model answer

    The recurrence tries every possible local action for the first pin and chooses the best, which is local brute force. Reusing the suffix solutions prevents repeatedly exploring the same remaining game, keeping the work polynomial.

    Explanation

    The recurrence considers all local actions at the first pin, while subproblem reuse prevents the choices from producing repeated exponential work.

  45. 45

    How should the recurrence’s combination operation reflect the objective of an optimization problem?

    Model answer

    After enumerating the relevant choices, combine their values with a maximum for a maximization problem or a minimum for a minimization problem. Other problems may use an appropriate operation such as logical OR or AND.

    Explanation

    For optimization recurrences, the combination operator reflects the goal: maximize scores, minimize costs, or use another suitable logical combination.

  46. 46

    What general design principle helps construct a dynamic-programming recurrence?

    Model answer

    Identify a decision or feature of a solution such that fixing it reduces the task to smaller subproblems. Enumerate the possible choices, combine their results appropriately, and ensure the subproblem family remains polynomial in size.

    Explanation

    A good recurrence identifies a feature of the solution that, once chosen, leaves a smaller subproblem whose answer can be reused.

  47. 47

    What makes a dynamic-programming design successful, beyond simply writing a recurrence?

    Model answer

    The key is to choose subproblems that capture enough information to solve the original task and a recurrence that reduces to them. If the choices and the subproblem family are polynomially bounded, memoization can yield a polynomial-time algorithm.

    Explanation

    The hardest design work is finding the right subproblem description and a manageable set of choices that reduce to those subproblems.

1 of 47
Question 1 of 47

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent