G27
Self-checkedTier 1 15 min readalgo.dp-classic-problems

LCS, edit distance, knapsack and matrix chain

The four classic dynamic programs of the syllabus: longest common subsequence, edit distance, 0/1 knapsack and matrix chain multiplication. For each: the state, the recurrence, the table filled step by step, how to recover the solution, the running time and space, and the standard facts GATE asks (table sizes, pseudo-polynomial time, Catalan number of parenthesisations).

Why it exists

Four problems appear again and again in exams on dynamic programming, and between them they contain every idea a DP question uses: a two-index table over prefixes (LCS, edit distance), a table indexed by a capacity (knapsack), and a table indexed by intervals filled by increasing length (matrix chain). For each GATE asks you to write or read the recurrence, fill a small table, state the time and space, and recognise the traps (pseudo-polynomial time, the number of parenthesisations).

Prerequisites

You need the recipe of dynamic programming: state, recurrence, filling order, answer (dynamic programming).

Precise definitions

Subsequence. A string obtained by deleting zero or more letters, keeping the order (ACE\text{ACE} is a subsequence of ABCDE\text{ABCDE}). Not necessarily contiguous (a substring is).

Longest common subsequence (LCS). The longest string that is a subsequence of both xx and yy.

Edit distance (Levenshtein). The fewest operations that turn xx into yy, where an operation is the insertion of a letter, the deletion of a letter, or the replacement of a letter by another (each costing 11).

0/1 knapsack. Items i=1..ni = 1..n with weights wiw_i and values viv_i (integers), capacity WW; choose a subset of total weight at most WW with maximum total value. Each item is taken or left.

Matrix chain multiplication. Multiply A1A2⋯AnA_1 A_2 \cdots A_n, where AiA_i is pi−1×pip_{i-1} \times p_i. The product does not depend on the bracketing, but the cost does: multiplying a p×qp \times q matrix by a q×rq \times r matrix costs pqrpqr scalar multiplications. Find the bracketing with the fewest scalar multiplications (the order of the matrices is fixed, only the brackets are chosen).

How it works

Longest common subsequence

Let L[i][j]L[i][j] be the length of an LCS of the first ii letters of xx and the first jj letters of yy. Look at the last letters xix_i and yjy_j:

L[i][j]={0i=0 or j=0,L[i−1][j−1]+1xi=yj,max⁡(L[i−1][j], L[i][j−1])xi≠yj.L[i][j] = \begin{cases} 0 & i = 0 \text{ or } j = 0,\\ L[i-1][j-1] + 1 & x_i = y_j,\\ \max(L[i-1][j],\ L[i][j-1]) & x_i \ne y_j. \end{cases}

If the letters are equal they can be matched at the end of an LCS (and matching them is never worse); if not, at least one of them is not used. Time and space Θ(mn)\Theta(mn): (m+1)(n+1)(m+1)(n+1) entries, O(1)O(1) each. To find the string, walk back from L[m][n]L[m][n]: if the letters are equal output the letter and go diagonally up-left; otherwise go to the larger of the neighbours above and to the left. If only the length is needed, two rows suffice: space Θ(min⁡(m,n))\Theta(\min(m, n)).

Edit distance

Let D[i][j]D[i][j] be the edit distance from the first ii letters of ss to the first jj letters of tt:

D[i][j]={ji=0,ij=0,D[i−1][j−1]si=tj,1+min⁡(D[i−1][j], D[i][j−1], D[i−1][j−1])si≠tj.D[i][j] = \begin{cases} j & i = 0,\\ i & j = 0,\\ D[i-1][j-1] & s_i = t_j,\\ 1 + \min(D[i-1][j],\ D[i][j-1],\ D[i-1][j-1]) & s_i \ne t_j. \end{cases}

The three terms in the minimum are: delete sis_i (move up), insert tjt_j (move left), replace sis_i by tjt_j (move diagonally). Time and space Θ(mn)\Theta(mn).

The program below computes the LCS of DYNAMIC and DANCING and the edit distance from SPRING to STRONG, and prints both tables.

#include <stdio.h>
#include <string.h>
 
static int max(int a, int b) { return a > b ? a : b; }
static int min3(int a, int b, int c) { return a < b ? (a < c ? a : c) : (b < c ? b : c); }
 
int main(void) {
    const char *x = "DYNAMIC", *y = "DANCING";
    int m = (int)strlen(x), n = (int)strlen(y);
    int L[8][8];                                         /* L[i][j] = length of a longest common subsequence of the first i letters of x and the first j of y */
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
            if (i == 0 || j == 0) L[i][j] = 0;
            else if (x[i - 1] == y[j - 1]) L[i][j] = L[i - 1][j - 1] + 1;
            else L[i][j] = max(L[i - 1][j], L[i][j - 1]);
        }
    }
    char lcs[8];
    int len = L[m][n];
    lcs[len] = 0;
    for (int i = m, j = n, k = len - 1; i > 0 && j > 0;) {       /* walk back from the corner */
        if (x[i - 1] == y[j - 1]) { lcs[k--] = x[i - 1]; i--; j--; }
        else if (L[i - 1][j] >= L[i][j - 1]) i--;
        else j--;
    }
    printf("LCS of %s and %s has length %d: %s\n", x, y, len, lcs);
 
    const char *s = "SPRING", *t = "STRONG";
    int p = (int)strlen(s), q = (int)strlen(t);
    int D[7][7];                                         /* D[i][j] = fewest edits turning the first i letters of s into the first j of t */
    for (int i = 0; i <= p; i++) {
        for (int j = 0; j <= q; j++) {
            if (i == 0) D[i][j] = j;
            else if (j == 0) D[i][j] = i;
            else if (s[i - 1] == t[j - 1]) D[i][j] = D[i - 1][j - 1];
            else D[i][j] = 1 + min3(D[i - 1][j], D[i][j - 1], D[i - 1][j - 1]);
        }
    }
    printf("edit distance from %s to %s is %d\n", s, t, D[p][q]);
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) printf("%2d", L[i][j]);
        printf("\n");
    }
    for (int i = 0; i <= p; i++) {
        for (int j = 0; j <= q; j++) printf("%2d", D[i][j]);
        printf("\n");
    }
    return 0;
}
LCS of DYNAMIC and DANCING has length 3: DNI
edit distance from SPRING to STRONG is 2
 0 0 0 0 0 0 0 0
 0 1 1 1 1 1 1 1
 0 1 1 1 1 1 1 1
 0 1 1 2 2 2 2 2
 0 1 2 2 2 2 2 2
 0 1 2 2 2 2 2 2
 0 1 2 2 2 3 3 3
 0 1 2 2 3 3 3 3
 0 1 2 3 4 5 6
 1 0 1 2 3 4 5
 2 1 1 2 3 4 5
 3 2 2 1 2 3 4
 4 3 3 2 2 3 4
 5 4 4 3 3 2 3
 6 5 5 4 4 3 2

The LCS has length 33 (the program finds DNI; DAC is another one: the LCS is not unique). The edit distance is 22: replace P\text{P} by T\text{T} and I\text{I} by O\text{O}. Step through the two tables; in each step the grammar highlights the case of the recurrence that is used and the explanation gives the three numbers compared.

Longest common subsequence of DYNAMIC and DANCING: start

Computed by a script, not typed
  1. 1L[i][j] = 0 if i = 0 or j = 0
  2. 2x_i = y_j: L[i][j] = L[i-1][j-1] + 1
  3. 3x_i != y_j: L[i][j] = max(L[i-1][j], L[i][j-1])
L0:-1:D2:A3:N4:C5:I6:N7:G
0:-00000000
1:D0
2:Y0
3:N0
4:A0
5:M0
6:I0
7:C0

Step 0

L[i][j] is the length of the longest common subsequence of the first i letters of DYNAMIC (rows) and the first j letters of DANCING (columns). The first row and column are the base case (0).

0 / 49
computed by script: the quantities are measured by running C, or evaluated, one row at a time. python3 scripts/gen-algo-dp-traces.py

Edit distance from SPRING to STRONG: start

Computed by a script, not typed
  1. 1D[i][0] = i and D[0][j] = j
  2. 2s_i = t_j: D[i][j] = D[i-1][j-1]
  3. 3s_i != t_j: D[i][j] = 1 + min(D[i-1][j], D[i][j-1], D[i-1][j-1])
D0:-1:S2:T3:R4:O5:N6:G
0:-0123456
1:S1
2:P2
3:R3
4:I4
5:N5
6:G6

Step 0

D[i][j] is the smallest number of single-letter insertions, deletions and replacements that turn the first i letters of SPRING (rows) into the first j letters of STRONG (columns).

0 / 36
computed by script: the quantities are measured by running C, or evaluated, one row at a time. python3 scripts/gen-algo-dp-traces.py

Relations. If only insertions and deletions are allowed, the distance is m+n−2⋅LCSm + n - 2 \cdot \text{LCS} (checked on random strings). The longest palindromic subsequence of a string is the LCS of the string and its reverse.

0/1 knapsack

Let K[i][c]K[i][c] be the best value that can be obtained from the first ii items with capacity cc. Item ii is either left (value K[i−1][c]K[i-1][c]) or, if it fits, taken (value viv_i plus the best for the remaining capacity):

K[i][c]={0i=0,K[i−1][c]wi>c,max⁡(K[i−1][c], vi+K[i−1][c−wi])wi≤c.K[i][c] = \begin{cases} 0 & i = 0,\\ K[i-1][c] & w_i > c,\\ \max\bigl(K[i-1][c],\ v_i + K[i-1][c - w_i]\bigr) & w_i \le c. \end{cases}

Time and space Θ(nW)\Theta(nW). Capacity WW is a number: the time is polynomial in its value but exponential in its length in bits, so this is a pseudo-polynomial algorithm (the problem is NP-hard, no polynomial algorithm is known). The table can be reduced to one row of W+1W + 1 entries by processing the capacities from WW down to wiw_i (so that the entry c−wic - w_i still holds the value of the previous item); space Θ(W)\Theta(W) when only the value is needed. The set of items is recovered by walking back: item ii is taken exactly when K[i][c]≠K[i−1][c]K[i][c] \ne K[i-1][c].

The table for items (w,v)=(3,30),(4,50),(2,15),(5,60)(w, v) = (3, 30), (4, 50), (2, 15), (5, 60) and capacity 88:

0/1 knapsack, capacity 8: start

Computed by a script, not typed
  1. 1K[0][c] = 0
  2. 2w_i > c: K[i][c] = K[i-1][c]
  3. 3K[i][c] = max(K[i-1][c], v_i + K[i-1][c-w_i])
Kc = 0c = 1c = 2c = 3c = 4c = 5c = 6c = 7c = 8
0 items000000000
item 1 (w 3, v 30)
item 2 (w 4, v 50)
item 3 (w 2, v 15)
item 4 (w 5, v 60)

Step 0

K[i][c] is the best value using only the first i items with capacity c. Item weights 3, 4, 2, 5 and values 30, 50, 15, 60.

0 / 36
computed by script: the quantities are measured by running C, or evaluated, one row at a time. python3 scripts/gen-algo-dp-traces.py

The best value is 9090 (items 11 and 44, weight 3+5=83 + 5 = 8). The greedy rule of the fractional knapsack (best value per weight first, taking whole items) would take item 22 (ratio 12.512.5), then item 11 (ratio 1010) and reach only 8080: the greedy rule fails for the 0/1 problem, DP does not (greedy knapsack).

Matrix chain multiplication

The number of ways to bracket nn matrices is the Catalan number Cn−1=1n(2n−2n−1)C_{n-1} = \frac{1}{n}\binom{2n-2}{n-1}: 1,1,2,5,14,42,…1, 1, 2, 5, 14, 42, \ldots for n=1,2,3,4,5,6,…n = 1, 2, 3, 4, 5, 6, \ldots (the DP never lists them). Let m[i][j]m[i][j] be the fewest scalar multiplications for Ai⋯AjA_i \cdots A_j. The last multiplication splits the chain after some kk (i≤k<ji \le k < j): multiply Ai⋯AkA_i \cdots A_k (a pi−1×pkp_{i-1} \times p_k matrix) by Ak+1⋯AjA_{k+1} \cdots A_j (a pk×pjp_k \times p_j matrix):

m[i][j]={0i=j,min⁡i≤k<j (m[i][k]+m[k+1][j]+pi−1 pk pj)i<j.m[i][j] = \begin{cases} 0 & i = j,\\ \min_{i \le k < j}\ \bigl(m[i][k] + m[k+1][j] + p_{i-1}\, p_k\, p_j\bigr) & i < j. \end{cases}

The table is filled by increasing chain length j−ij - i, because the entries on the right use shorter chains. There are Θ(n2)\Theta(n^2) states and each takes O(n)O(n) time: time Θ(n3)\Theta(n^3), space Θ(n2)\Theta(n^2); exactly (n3−n)/6(n^3 - n)/6 split points are examined in total. Keeping the best kk in a second table s[i][j]s[i][j] gives the bracketing.

The program computes the knapsack and the chain A1(5×12),A2(12×4),A3(4×20),A4(20×8),A5(8×10)A_1 (5 \times 12), A_2 (12 \times 4), A_3 (4 \times 20), A_4 (20 \times 8), A_5 (8 \times 10).

#include <stdio.h>
 
static int max(int a, int b) { return a > b ? a : b; }
 
int main(void) {
    /* 0/1 knapsack: items 1..4, capacity 8 */
    int w[5] = {0, 3, 4, 2, 5}, v[5] = {0, 30, 50, 15, 60}, W = 8;
    int K[5][9];                                         /* K[i][c] = best value using only the first i items with capacity c */
    for (int i = 0; i <= 4; i++) {
        for (int c = 0; c <= W; c++) {
            if (i == 0) K[i][c] = 0;
            else if (w[i] > c) K[i][c] = K[i - 1][c];
            else K[i][c] = max(K[i - 1][c], v[i] + K[i - 1][c - w[i]]);
        }
    }
    printf("knapsack: best value %d, items taken:", K[4][W]);
    for (int i = 4, c = W; i > 0; i--)
        if (K[i][c] != K[i - 1][c]) { printf(" %d", i); c -= w[i]; }
    printf("\n");
 
    /* matrix chain: A1 is 5x12, A2 is 12x4, A3 is 4x20, A4 is 20x8, A5 is 8x10 */
    int p[6] = {5, 12, 4, 20, 8, 10}, n = 5;
    long m[6][6];                                        /* m[i][j] = fewest scalar multiplications for A_i ... A_j */
    int s[6][6];                                         /* s[i][j] = the best place k to split */
    for (int i = 1; i <= n; i++) m[i][i] = 0;
    for (int len = 2; len <= n; len++) {
        for (int i = 1; i + len - 1 <= n; i++) {
            int j = i + len - 1;
            m[i][j] = 1L << 40;
            for (int k = i; k < j; k++) {
                long cost = m[i][k] + m[k + 1][j] + (long)p[i - 1] * p[k] * p[j];
                if (cost < m[i][j]) { m[i][j] = cost; s[i][j] = k; }
            }
        }
    }
    printf("matrix chain: fewest multiplications %ld\n", m[1][n]);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (j < i) printf("%6s", ".");
            else printf("%6ld", m[i][j]);
        }
        printf("\n");
    }
    return 0;
}
knapsack: best value 90, items taken: 4 1
matrix chain: fewest multiplications 1400
     0   240   640  1040  1400
     .     0   960  1024  1440
     .     .     0   640   960
     .     .     .     0  1600
     .     .     .     .     0

Fewest multiplications for A1 A2 A3 A4 A5: start

Computed by a script, not typed
  1. 1m[i][i] = 0
  2. 2m[i][j] = min over k of m[i][k] + m[k+1][j] + p(i-1) p(k) p(j)
mj = 1j = 2j = 3j = 4j = 5
i = 10
i = 20
i = 30
i = 40
i = 50

Step 0

A1 is 5 x 12, A2 is 12 x 4, A3 is 4 x 20, A4 is 20 x 8, A5 is 8 x 10 (so p = 5, 12, 4, 20, 8, 10). m[i][j] is the fewest scalar multiplications to compute the product A_i ... A_j. Chains of length 2 are filled first, then 3, 4 and 5.

0 / 10
computed by script: the quantities are measured by running C, or evaluated, one row at a time. python3 scripts/gen-algo-dp-traces.py

The cheapest bracketing is ((A1A2)((A3A4)A5))((A_1 A_2)((A_3 A_4) A_5)) with 14001400 scalar multiplications; multiplying from left to right, (((A1A2)A3)A4)A5(((A_1 A_2) A_3) A_4) A_5, costs 18401840. In the animation the explanation of each cell lists every split point it tries and marks the best one.

Worked examples

EX 1LCS table by hand

Easy

x=ABCx = \text{ABC}, y=ACy = \text{AC}. Row i=0i = 0 and column j=0j = 0 are 00. Row AA: L[1][1]=1L[1][1] = 1 (A=A\text{A} = \text{A}), L[1][2]=max⁡(L[0][2],L[1][1])=1L[1][2] = \max(L[0][2], L[1][1]) = 1. Row BB: L[2][1]=max⁡(L[1][1],L[2][0])=1L[2][1] = \max(L[1][1], L[2][0]) = 1, L[2][2]=max⁡(L[1][2],L[2][1])=1L[2][2] = \max(L[1][2], L[2][1]) = 1. Row CC: L[3][1]=1L[3][1] = 1, L[3][2]L[3][2]: C=C\text{C} = \text{C}, so L[2][1]+1=2L[2][1] + 1 = 2. The LCS is AC\text{AC}, length 22.

EX 2Edit distance in small cases

Easy

CAT→CUT\text{CAT} \to \text{CUT}: replace A\text{A} by U\text{U}: 11. ABC→\text{ABC} \to (empty): delete three letters: 33, the base case D[3][0]=3D[3][0] = 3. SPRING→STRONG\text{SPRING} \to \text{STRONG}: 22 (the table). The distance is at least ∣m−n∣|m - n| (the length difference) and at most max⁡(m,n)\max(m, n).

EX 3One knapsack cell

Medium

K[3][6]K[3][6] with item 33 of weight 22 and value 1515: leaving the item gives K[2][6]=50K[2][6] = 50; taking it gives 15+K[2][4]=15+50=6515 + K[2][4] = 15 + 50 = 65. The maximum is 6565, the entry in the table. For K[3][1]K[3][1] the item (weight 22) does not fit in capacity 11, so K[3][1]=K[2][1]=0K[3][1] = K[2][1] = 0.

EX 4Why the bracketing matters

Medium

A1A_1 is 10×10010 \times 100, A2A_2 is 100×5100 \times 5, A3A_3 is 5×505 \times 50. (A1A2)A3(A_1 A_2) A_3: 10⋅100⋅5+10⋅5⋅50=5000+2500=750010 \cdot 100 \cdot 5 + 10 \cdot 5 \cdot 50 = 5000 + 2500 = 7500. A1(A2A3)A_1 (A_2 A_3): 100⋅5⋅50+10⋅100⋅50=25 000+50 000=75 000100 \cdot 5 \cdot 50 + 10 \cdot 100 \cdot 50 = 25\,000 + 50\,000 = 75\,000: ten times more, for the same product.

EX 5Typical questions

GATE level

(a) Number of entries of the LCS table for strings of lengths mm and nn: (m+1)(n+1)(m+1)(n+1); time Θ(mn)\Theta(mn). (b) Longest palindromic subsequence of AGBCBA\text{AGBCBA}: LCS of AGBCBA\text{AGBCBA} and ABCBGA\text{ABCBGA} is 55 (ABCBA\text{ABCBA}). (c) Time of the matrix chain DP: Θ(n3)\Theta(n^3); space Θ(n2)\Theta(n^2); number of brackets for 55 matrices: 1414. (d) Why is the knapsack DP not polynomial? Θ(nW)\Theta(nW), and WW takes only log⁡2W\log_2 W bits to write. (e) How many entries of the matrix-chain table are actually computed (above the diagonal)? n(n−1)/2n(n-1)/2.

Cheat box

Cheat box

  • LCS: L[i][j]=L[i−1][j−1]+1L[i][j] = L[i-1][j-1] + 1 if the letters are equal, else max⁡(L[i−1][j],L[i][j−1])\max(L[i-1][j], L[i][j-1]); Θ(mn)\Theta(mn) time and space; Θ(min⁡(m,n))\Theta(\min(m, n)) space for the length.
  • Edit distance: equal letters: diagonal; otherwise 1+min⁡(up,left,diagonal)1 + \min(\text{up}, \text{left}, \text{diagonal}) (delete, insert, replace); base D[i][0]=iD[i][0] = i, D[0][j]=jD[0][j] = j; Θ(mn)\Theta(mn).
  • 0/1 knapsack: K[i][c]=max⁡(K[i−1][c], vi+K[i−1][c−wi])K[i][c] = \max(K[i-1][c],\ v_i + K[i-1][c - w_i]) if wi≤cw_i \le c; Θ(nW)\Theta(nW) time, pseudo-polynomial; Θ(W)\Theta(W) space with one row.
  • Matrix chain: m[i][j]=min⁡k(m[i][k]+m[k+1][j]+pi−1pkpj)m[i][j] = \min_k (m[i][k] + m[k+1][j] + p_{i-1} p_k p_j); by increasing length; Θ(n3)\Theta(n^3) time, Θ(n2)\Theta(n^2) space; Cn−1C_{n-1} bracketings.
  • Insert-delete distance =m+n−2 LCS= m + n - 2\,\text{LCS}; palindromic subsequence =LCS(s,sR)= \text{LCS}(s, s^R).

Traps and GATE patterns

Connections

All four use the recipe of dynamic programming. The 0/1 knapsack contrasts with the greedy fractional knapsack (activity selection and fractional knapsack). Sequence DP of the same kind is in LIS and coin change, and the interval-style table (matrix chain) has a cousin in the shortest-path table of Floyd-Warshall.

PYQs

7 previous year questions from your book. I re-solved each one myself (6 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2021 · Set 1 · MSQQ2 · page 130 of the book
Define Rn to be the maximum amount earned by
cutting a rod of length n meters into one or more
pieces of integer length and selling them. For i >
0, let p[i] denote the selling prices of a rod whose
length is i meters. Consider the array of prices :
p[1] = 1, p[2] = 5, p[3] = 8, p[4] = 9, p[5] = 10,
p[6] = 17, p[7] = 18

Which of the following statements is/are correct
about R 7 ?
  • (a)R7 cannot be achieved by a solution consisting of three pieces.
  • (b)R7 = 19
  • (c)R7 = 18
  • (d)R7 is achieved by three different solutions.
GATE 2018 · MCQ · 2 marksQ3 · page 130 of the book
Assume that multiplying a matrix G 1 of
dimension p×q with another matrix G 2 of
dimension q × r requires scalar multiplications.
Computing the product of matrices
G1 G2 G3 ......Gn can be done by
parenthesizing in different ways. Define Gi
Gi+1 as an explicitly computed pair for a given
parenthesization if they are directly multiplied.
For example, in the matrix multiplication chain
G1 G2 G3 G4 G5 G6 using parenthesization
ALGORITHMS

Check Out Our Gate Courses & Secure Your Dream Rank !

(G1(G2G3)) (G4(G5G6)).G2 G3 and G5 G6 are
the only explicitly computed pairs. Consider a
matrix multiplication chain F1 F2 F3 F4 F5 ,
where matrices F1 F2 F3 F4 F5 are of
dimensions 2 × 25, 25 × 3, 3 × 16, 16 × 1 and
1×1000, respectively.In the parenthesization of
F1 F2 F3 F4 F5 that minimizes the total number
of
scalar
multiplications,
the
explicitly
computed pairs is/are:
GATE 2018 · NAT · 2 marksQ4 · page 131 of the book
Consider the weights and values of items listed
below. Note that there is only one unit of each item.
The task is to pick a subset of these items such that
their total weight is no more than 11 Kgs and their
total value is maximized. Moreover, no item may be
split. The total value of items picked by an optimal
algorithm is denoted by Vopt. A greedy algorithm
sorts the items by their value-to-weight ratios in
descending order and packs them greedily, starting
from the first item in the ordered list. The total value
of items picked by the greedy algorithm is denoted
by Vgreedy.
The value of Vopt - Vgreedy is
GATE 2016 · Set 2 · NAT · 2 marksQ5 · page 131 of the book
Let A 1 , A 2 , A 3 , and A 4 be four matrices of
dimensions 10 × 5, 5 × 20, 20 × 10, and 10 × 5,
respectively. The minimum number of scalar
multiplications required to find the product
A1A2A3A4
using
the
basic
matrix
multiplication method is   .
GATE 2011 · MCQ · 2 marksQ8 · page 131 of the book
Four matrices M1, M2, M3 and M4 of
dimensions pxq, qxr, rxs and sxt respectively
can be multiplied is several ways with different
number of total scalar multiplications. For
example, when multiplied as ((M1 X M2) X (M3
X M4)), the total number of multiplications is
pqr + rst + prt. If p = 10, q = 100, r = 20, s = 5
and t = 80, then the number of scalar
multiplications needed is:
ALGORITHMS

Check Out Our Gate Courses & Secure Your Dream Rank !
year not read yetQ11 · page 132 of the book
The subset-sum problem is defined as follows.
Given
a
set
of
n
positive
integers,
S={a1,a2,...,an} and positive integer W, is there
a subset of S whose elements sum to W? A
dynamic program for solving this problem uses
a 2-dimensional Boolean array, X, with n rows
and W+1 columns. X[i,j], 1≤i≤n, 0≤j≤W, is
TRUE if and only if there is a subset of
{a1,a2,...,ai }whose elements sum to j.
Which entry of the array X, if TRUE, implies
that there is a subset whose elements sum to W?
  • (a)X[1,W]
  • (b)X[n,0]
  • (c)X[n,W]
  • (d)X[n-1,n]
GATE 2008 · MCQ · 2 marksQ12 · page 132 of the book
The subset-sum problem is defined as follows.
Given a set of n positive integers, S={a1,a2,...,an
} and positive integer W, is there a subset of S
whose elements sum to W? A dynamic program
for solving this problem uses a 2-dimensional
Boolean array, X, with n rows and W+1
columns. X[i,j], 1≤i≤n, 0≤j≤W, is TRUE if and
only if there is a subset of {a1,a2,...,ai }whose
elements sum to j.Which of the following is
valid for 2≤i≤n and ai≤j≤W?
  • (a)X[i,j]=X[i-1,j]∨X[i,j-ai ]
  • (b)X[i,j]=X[i-1,j]∨X[i-1,j-ai ]
  • (c)X[i,j]=X[i-1,j]∧X[i,j-ai ]
  • (d)X[i,j]=X[i-1,j]∧X[i-1,j-ai ] ALGORITHMS Check Out Our Gate Courses & Secure Your Dream Rank !

Self-check

Question 1

What is the edit distance between ABC\text{ABC} and ABD\text{ABD}, and between ABC\text{ABC} and BC\text{BC}?

Show answer

11 in both cases: replace C\text{C} by D\text{D}; delete the A\text{A}.

Question 2

How many scalar multiplications does A1A2A_1 A_2 need if A1A_1 is 3×43 \times 4 and A2A_2 is 4×54 \times 5?

Show answer

3⋅4⋅5=603 \cdot 4 \cdot 5 = 60.

Question 3

What is the time of the knapsack DP for nn items and capacity WW, and why is it called pseudo-polynomial?

Show answer

Θ(nW)\Theta(nW); polynomial in the value WW but exponential in the number of bits needed to write WW.

Question 4

How many ways are there to bracket a product of 44 matrices?

Show answer

C3=5C_3 = 5.

Revision card

One-screen revision

  • LCS: match →\to diagonal +1+1, else max(up, left); Θ(mn)\Theta(mn).
  • Edit distance: match →\to diagonal, else 1+1 + min(up, left, diagonal); Θ(mn)\Theta(mn).
  • 0/1 knapsack: max⁡(K[i−1][c],vi+K[i−1][c−wi])\max(K[i-1][c], v_i + K[i-1][c-w_i]); Θ(nW)\Theta(nW) pseudo-polynomial; greedy fails.
  • Matrix chain: min⁡k\min_k split + pi−1pkpjp_{i-1} p_k p_j; by length; Θ(n3)\Theta(n^3) time, Θ(n2)\Theta(n^2) space; Catalan bracketings.

References

  • ·Cormen, Leiserson, Rivest, Stein, Introduction to Algorithms (3rd edition), Chapter 15 Dynamic Programming (15.2 matrix-chain multiplication, 15.4 longest common subsequence) and Problem 15-5 (edit distance); Section 34.5.5 (0/1 knapsack, pseudo-polynomial)
  • ·Kleinberg and Tardos, Algorithm Design, Chapter 6 (6.4 knapsack, 6.6 sequence alignment)
  • ·Dasgupta, Papadimitriou, Vazirani, Algorithms, Chapter 6 (edit distance, knapsack, chain matrix multiplication)

Verification

Every table was compared with an independent computation: the LCS length against the maximum over ALL common subsequences (300 random pairs), the edit distance against a breadth-first search over strings with the three edit operations (120 random pairs), the knapsack against ALL subsets (300 random instances), the matrix chain against ALL parenthesisations (80 random chains, the number of parenthesisations being the Catalan number), and the number of split evaluations against (n^3 - n)/6. The step-by-step animations are the page programs with a print added after each assignment, and their final tables equal the tables printed by the page programs. Also checked: the insert-and-delete-only distance equals m + n - 2 LCS, the longest palindromic subsequence equals the LCS of a string and its reverse, and the greedy value-per-weight rule gives 80 on the page knapsack against the optimum 90. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (loop counts, recurrences, merge and quicksort counts and averages, hash probes and load-factor formulas, DP tables, MST and shortest-path runs, Floyd matrices); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never