Chapter 3: Dynamic Programming

Chapter 3: Dynamic Programming · Study Questions
Basic conceptsFibonacci0/1 knapsackUnbounded knapsackSubset sumLongest increasing subsequenceCommon substring and LCS
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Basic Concepts

Question 1: The Two Properties of DP

Which two properties must a problem have to be solved efficiently by dynamic programming?

B: Subproblems must be called again and again (so that a table helps) and the optimal solution must be buildable from optimal solutions of the subproblems (Bellman's principle of optimality). The greedy-choice property is the condition for greedy algorithms; DP tries all options.

Multiple Choice · Basic Concepts

Question 2: Why No Table?

Merge sort is recursive; why does it not use a DP table?

A: When the array is split in two, the halves are disjoint parts; no subproblem is solved twice, so there is nothing repeated to store. This is divide and conquer. In Fibonacci, on the other hand, subproblems such as fib(3) are called many times.

Output Question · Fibonacci

Question 3: Number of Calls

A call counter was added to the plain recursive fib method. What does the code print? How many calls would the same computation make with memoization, as in the lectures?

static int calls = 0;
static long fib(int n) {
  calls++;
  if (n <= 1) return n;
  return fib(n - 1) + fib(n - 2);
}
// main:
System.out.println(fib(7) + " " + calls);
Show answer
13 41
  • F(7) = 13. Number of calls 2 · F(n + 1) − 1 = 2 · 21 − 1 = 41: T(n) = T(n − 1) + T(n − 2) + 1.
  • With memoization each fib(k) is expanded at most once; fib(n), fib(n − 1) and fib(0) are called once each, and fib(1) … fib(n − 2) twice each (for fib(2) … fib(n − 2) the second call is answered from the table) → 1 + 1 + 2(n − 2) + 1 = 2n − 1 = 13 calls.

From exponential to linear: Θ(φⁿ) → O(n).

Step-by-Step Trace · Fibonacci

Question 4: Memoization: fib(4)

In the memoized call fib(4), how does the memo array fill up, and which call is answered from the table?

memo0000001234

The memo array starts at 0 (0 = "not computed yet"). fib(4) is not in the table → fib(3) is called.

memo0000001234

fib(3) is not in the table → fib(2) is called.

memo0000001234

fib(2) is not in the table → fib(1) is called.

memo0000001234

fib(1): base case, returns 1.

memo0000001234

In fib(2), a = 1; now fib(0) is called: base case, returns 0.

memo0010001234

memo[2] = 1 + 0 = 1 is stored and returned.

memo0010001234

In fib(3), a = 1; fib(1) is called → 1 (base case).

memo0012001234

memo[3] = 1 + 1 = 2.

memo0012001234

In fib(4), a = 2; fib(2) is called: memo[2] = 1 is ready, it returns without any sub-calls.

memo0012301234

memo[4] = 2 + 1 = 3. 7 calls in total (2n − 1); plain recursion would make 9 calls.

static long[] memo = new long[50];

static long fib(int n) {
  if (n <= 1) return n;          // base case
  if (memo[n] != 0) return memo[n];
  long a = fib(n - 1);
  long b = fib(n - 2);
  memo[n] = a + b;
  return memo[n];
}

Multiple Choice · Fibonacci

Question 5: Memoization and Tabulation

For memoization (top-down) and tabulation (bottom-up), which of the following is false?

C is false: both solve each subproblem once and have the same asymptotic time; tabulation is often faster in practice because it makes no recursive calls. A, B and D are properties covered in the lecture.

Output Question · 0/1 Knapsack

Question 6: The Maximum Value

Items (v, w): (10, 5), (40, 4), (30, 6), (50, 3); capacity W = 10. Find the last row of the 0/1 knapsack table, the maximum value and the chosen items.

Show answer
iK[i][0..10]
10 0 0 0 0 10 10 10 10 10 10
20 0 0 0 40 40 40 40 40 50 50
30 0 0 0 40 40 40 40 40 50 70
40 0 0 50 50 50 50 90 90 90 90

K[4][10] = 90. Traceback: K[4][10] = 90 ≠ K[3][10] = 70 → item 4 (c = 7); K[3][7] = K[2][7] = 40 → item 3 is not taken; K[2][7] = 40 ≠ K[1][7] = 10 → item 2 (c = 3); K[1][3] = 0 → item 1 is not taken.

Selection: item 2 + item 4 (40 + 50, weight 7). No other item fits in the remaining 3 kg.

Multiple Choice · 0/1 Knapsack

Question 7: Why Does Greedy Fail?

W = 10; items (value, weight): (30 ₺, 6 kg), (20 ₺, 5 kg), (20 ₺, 5 kg). How much do greedy selection by value/weight ratio and the optimal 0/1 solution give?

A: The ratios are 5, 4, 4: greedy takes the 6 kg item first; the 5 kg items do not fit in the remaining 4 kg → 30 ₺. The two 5 kg items together weigh 10 kg and are worth 40 ₺. 50 ₺ would exceed the weight limit (11 kg). Greedy is optimal only for the fractional knapsack.

Output Question · 0/1 Knapsack

Question 8: Loop Direction

For a single item (value 10, weight 3) and capacity 9, a one-dimensional table is filled in two different directions. What does the code print? Which one is the 0/1 knapsack?

int v = 10, w = 3, W = 9;
int[] dp = new int[W + 1];
int[] dq = new int[W + 1];
for (int c = W; c >= w; c--)   // large to small
  dp[c] = Math.max(dp[c], v + dp[c - w]);
for (int c = w; c <= W; c++)   // small to large
  dq[c] = Math.max(dq[c], v + dq[c - w]);
System.out.println(dp[W] + " " + dq[W]);
Show answer
10 30
  • Large to small: dp[c − 3] has not been updated yet in this pass (old row) → the item is taken at most once: 10 (0/1).
  • Small to large: dq[3] = 10, dq[6] = 10 + dq[3] = 20, dq[9] = 30 → the same item is taken three times: 30 (unbounded knapsack).

Multiple Choice · 0/1 Knapsack

Question 9: Pseudo-Polynomial

The 0/1 knapsack DP takes O(n · W). Why is this time called "pseudo-polynomial"?

D: W = 10⁹ is written with only 30 bits but the table needs 10⁹ columns. The time depends on the magnitude of W, not its length; so this DP does not contradict the problem being NP-hard, and it is practical only when W is small.

Multiple Choice · Unbounded Knapsack

Question 10: How Many Different Ways?

In how many different ways can the amount 10 be paid with coins {2, 3, 5}? (Order does not matter; the outer loop is over the coins: d[x] += d[x − c].)

D: {5, 5}, {2, 3, 5}, {2, 2, 3, 3}, {2, 2, 2, 2, 2} → 4. If the loops are swapped (outer loop over the amount), 2 + 3 and 3 + 2 are counted separately, so ordered sequences get counted.

Step-by-Step Trace · Subset Sum

Question 11: The subsetSum Table

subsetSum({3, 4, 5, 2}, 7): how does its table fill up, row by row?

dp[i][j]012345670✓·······1 (3)2 (4)3 (5)4 (2)

a = {3, 4, 5, 2}, target 7. Column 0 is always ✓ (empty set); in row 0 no other sums are reachable.

dp[i][j]012345670✓·······1 (3)✓··✓····2 (4)3 (5)4 (2)

i = 1, x = 3: the row above is copied; the j for which dp[i−1][j−3] is ✓ also become ✓. New sums (blue): 3.

dp[i][j]012345670✓·······1 (3)✓··✓····2 (4)✓··✓✓··✓3 (5)4 (2)

i = 2, x = 4: the row above is copied; the j for which dp[i−1][j−4] is ✓ also become ✓. New sums (blue): 4, 7.

dp[i][j]012345670✓·······1 (3)✓··✓····2 (4)✓··✓✓··✓3 (5)✓··✓✓✓·✓4 (2)

i = 3, x = 5: the row above is copied; the j for which dp[i−1][j−5] is ✓ also become ✓. New sums (blue): 5.

dp[i][j]012345670✓·······1 (3)✓··✓····2 (4)✓··✓✓··✓3 (5)✓··✓✓✓·✓4 (2)✓·✓✓✓✓✓✓

i = 4, x = 2: the row above is copied; the j for which dp[i−1][j−2] is ✓ also become ✓. New sums (blue): 2, 6.

dp[i][j]012345670✓·······1 (3)✓··✓····2 (4)✓··✓✓··✓3 (5)✓··✓✓✓·✓4 (2)✓·✓✓✓✓✓✓

dp[4][7] = true. Traceback: dp[4][7] = dp[3][7] = dp[2][7] ✓ (2 and 5 are not needed) but dp[1][7] · → 4 chosen (j = 3); dp[1][3] ✓ → 3. Subset {3, 4}; {5, 2} is also valid.

boolean subsetSum(int[] a, int target) {
  int n = a.length; // a[i] ≥ 0, target ≥ 0
  boolean[][] dp =
      new boolean[n + 1][target + 1];
  for (int i = 0; i <= n; i++)
    dp[i][0] = true;           // empty set
  for (int i = 1; i <= n; i++) {
    int x = a[i - 1];
    for (int j = 1; j <= target; j++)
      dp[i][j] = dp[i - 1][j]  // x is not taken
        || (x <= j && dp[i - 1][j - x]);
  }
  return dp[n][target];
}

Multiple Choice · Subset Sum

Question 12: Equal Partition

{1, 5, 11, 5}: how can we use subset sum to find out whether this array can be split into two parts with equal sums, and what is the result?

C: The total is S = 22; if one part is S/2 = 11, the other is also 11. {11} or {1, 5, 5} → yes. If S were odd the answer would be no immediately; target 22 asks about the whole array itself and says nothing about splitting.

Step-by-Step Trace · Longest Increasing Subsequence

Question 13: Tracing lis

lis({3, 1, 4, 1, 5, 9, 2, 6}) returns what value?

a3141592601234567dp

dp[i]: the length of the longest increasing subsequence ending at a[i]. Blue: earlier candidates with a[j] < a[i].

a3141592601234567dp1

i = 0 (a = 3): no smaller element before it → dp[0] = 1.

a3141592601234567dp11

i = 1 (a = 1): no smaller element before it → dp[1] = 1.

a3141592601234567dp112

i = 2 (a = 4): candidates 3, 1; the largest dp is at j = 0 (dp = 1) → dp[2] = 2.

a3141592601234567dp1121

i = 3 (a = 1): no smaller element before it → dp[3] = 1.

a3141592601234567dp11213

i = 4 (a = 5): candidates 3, 1, 4, 1; the largest dp is at j = 2 (dp = 2) → dp[4] = 3.

a3141592601234567dp112134

i = 5 (a = 9): candidates 3, 1, 4, 1, 5; the largest dp is at j = 4 (dp = 3) → dp[5] = 4.

a3141592601234567dp1121342

i = 6 (a = 2): candidates 1, 1; the largest dp is at j = 1 (dp = 1) → dp[6] = 2.

a3141592601234567dp11213424

i = 7 (a = 6): candidates 3, 1, 4, 1, 5, 2; the largest dp is at j = 4 (dp = 3) → dp[7] = 4.

a3141592601234567dp11213424

Result 4 = max dp (e.g. 3, 4, 5, 9 or 1, 4, 5, 6). dp[n − 1] is also 4 but that is a coincidence; the answer is always the largest dp. O(n²).

static int lis(int[] a) {
  int n = a.length, longest = 0;
  int[] dp = new int[n];
  for (int i = 0; i < n; i++) {
    dp[i] = 1;               // on its own
    for (int j = 0; j < i; j++)
      if (a[j] < a[i] && dp[j] + 1 > dp[i])
        dp[i] = dp[j] + 1;   // extend
    longest = Math.max(longest, dp[i]);
  }
  return longest;
}

Multiple Choice · Longest Increasing Subsequence

Question 14: Which Cell Holds the Answer?

In the lis method, a student replaces longest with dp[n - 1] as the returned value. For {5, 1, 6, 2, 7, 0}, what are the wrong and the correct results?

B: dp = [1, 1, 2, 2, 3, 1]. The last element 0 cannot extend any sequence → dp[5] = 1. The LIS can end anywhere; the correct answer is max dp = 3 (5, 6, 7 or 1, 2, 7). There is no increasing subsequence of length 4.

Output Question · Common Substring

Question 15: Substring and Subsequence

For "programlama" and "gramer", what are the lengths of the longest common substring (contiguous) and the longest common subsequence (LCS)? Write the common substring; where are the answer cells in the tables?

Show answer
  • Longest common substring: "gram", length 4 (3–6 in programlama, 0–3 in gramer). The answer is the largest value of the table.
  • The LCS length is also 4: there is no common subsequence longer than "gram" (the remaining "er" of gramer does not appear in order after the m in programlama). The answer is the last cell L[n][m].
  • A common substring is always ≤ the LCS; here they are equal. For "abdca" · "cbda" they are 2 and 3.

Multiple Choice · Longest Common Subsequence

Question 16: Subsequence or Substring?

For A = "GXTXAYB", which of the following is true?

C: G, T, A, B occur in order at positions 0, 2, 4, 6: the relative order is kept → subsequence; but they are not contiguous → not a substring. Every substring is also a subsequence (so B and D are false); "XAY" occurs contiguously at positions 3, 4, 5; it is both a subsequence and a substring.

Output Question · Longest Common Subsequence

Question 17: lcs Lengths

Using the lecture's lcs method (it returns the length), what does the code print? Give one LCS for each pair.

System.out.println(lcs("DINAMIK", "DIAMOND"));
System.out.println(lcs("ABCBDAB", "BDCABA"));
System.out.println(lcs("ABC", ""));
Show answer
4
4
0
  • DINAMIK · DIAMOND: "DIAM" (D, I, A, M appear in order in both).
  • ABCBDAB · BDCABA: "BCBA" (or "BDAB", "BCAB"); the LCS is not unique, its length is.
  • With an empty string the LCS is empty: L[0][m] = 0.

Time and memory O(m · n); if only the length is needed, two rows suffice, giving O(min(m, n)) memory.

Step-by-Step Trace · Other Problems

Question 18: Tracing Kadane

Maximum-sum subarray (Kadane, V(i) = max(a[i], V(i−1) + a[i])): kadane({2, -3, 4, -1, -2, 1, 5, -3}) returns what?

a2-34-1-215-301234567V2

V(i): the sum of the maximum-sum subarray ending at a[i]. V(0) = a[0] = 2; best = 2.

a2-34-1-215-301234567V2-1

i = 1: V = max(-3, 2 + -3) = -1 (extend); best = 2.

a2-34-1-215-301234567V2-14

i = 2: V = max(4, -1 + 4) = 4 (restart); best = 4.

a2-34-1-215-301234567V2-143

i = 3: V = max(-1, 4 + -1) = 3 (extend); best = 4.

a2-34-1-215-301234567V2-1431

i = 4: V = max(-2, 3 + -2) = 1 (extend); best = 4.

a2-34-1-215-301234567V2-14312

i = 5: V = max(1, 1 + 1) = 2 (extend); best = 4.

a2-34-1-215-301234567V2-143127

i = 6: V = max(5, 2 + 5) = 7 (extend); best = 7.

a2-34-1-215-301234567V2-1431274

i = 7: V = max(-3, 7 + -3) = 4 (extend); best = 7.

a2-34-1-215-301234567V2-1431274

Result 7: subarray [4, −1, −2, 1, 5] (indices 2–6). Each state is O(1) → O(n) time, O(1) memory.

static int kadane(int[] a) {
  int best = a[0], v = a[0];   // V(0)
  for (int i = 1; i < a.length; i++) {
    v = Math.max(a[i], v + a[i]);  // V(i)
    best = Math.max(best, v);
  }
  return best;
}

Multiple Choice · Other Problems

Question 19: Matrix Chain

In matrix-chain multiplication, M[i][j] = min { M[i][k] + M[k+1][j] + pi−1 · pk · pj : i ≤ k < j }. What is the time complexity for n matrices?

B: There are O(n²) states (pairs i ≤ j) and for each state O(n) choices of k are tried → O(n³). Trying all parenthesizations would take Catalan-number time, i.e. exponential. The rule is again "number of states × work per state".

Classic Question · Other Problems

Question 20: DP Design: Making Change

For coins c1, …, ck and amount X, design the "fewest coins" problem with the steps from the lectures: state, recurrence, base case, filling order, table size and complexity. Fill in the table for coins {1, 3, 4} and X = 6.

Show answer
  • State: P[x] = the fewest coins needed to pay the amount x.
  • Recurrence: P[x] = 1 + min P[x − c] (c ≤ x); base: P[0] = 0.
  • Order: x = 1, 2, …, X (smaller amounts must be ready first). Table: X + 1 cells.
  • Complexity: O(k · X) time, O(X) memory (pseudo-polynomial).
0123456P0121122

P[6] = 1 + min(P[5], P[3], P[2]) = 1 + min(2, 1, 2) = 2 (3 + 3). Greedy 4 + 1 + 1 gives 3 coins. Which coins are chosen is read back by storing the c used for each x.

The End

Chapter 3: Dynamic Programming: Study Questions
Lecture slides: Chapter 3: Dynamic Programming · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.