All Exams Test series for 1 year @ ₹349 only
Question

Consider a table $T$, where the elements $T[i][j], 0 \le i,j \le n$, represent the cost of the optimal solutions of different subproblems of a problem that is being solved using a dynamic programming algorithm. The recursive formulation to compute the table entries is as follows:

$T[0][k] = T[k][0] = 1$ for $k = 0,1,2, \dots, n$
$T[i][j] = 2T[i - 1][j] + 3T[i][j - 1]$ for $1 \le i,j \le n$

Consider the following two algorithms to compute entries of $T$. Assume that for both the algorithms, for all $0 \le i,j \le n$, $T[i][j]$ has been initialized to 1.

Algorithm $B_1$:
For $i = 1, 2, \dots, n$
For $j = 1, 2, \dots, n$
$T[i][j] = 2T[i - 1][j] + 3T[i][j - 1]$

Algorithm $B_2$:
For $s = 2, 3, \dots, 2n$
For $i = 1, 2, \dots, n$
For $j = 1, 2, \dots, n$
If ($i + j == s$)
$T[i][j] = 2T[i - 1][j] + 3T[i][j - 1]$

Algorithm $B_k, k \in \{1,2\}$ is said to be correct if and only if it calculates the correct values of $T[i][j]$, for all $0 \le i,j \le n$, (as per the recursive formulation) at the end of the execution of the algorithm $B_k$.

Which one of the following statements is true?

The correct answer is
Both algorithms $B_1$ and $B_2$ are correct

The question evaluates the correctness of two algorithms, B1 and B2, used to fill a dynamic programming table T. Correctness requires that each entry $T[i][j]$ is computed using previously computed, correct values, according to the given recursive formulation.

DP Table Dependencies

The recursive formulation for the table entries is:

  • Base cases: $T[0][k] = T[k][0] = 1$ for $0 \le k \le n$.
  • Recursive step: $T[i][j] = 2T[i - 1][j] + 3T[i][j - 1]$ for $1 \le i,j \le n$.

This means computation of $T[i][j]$ depends on the values immediately above it ($T[i-1][j]$) and immediately to its left ($T[i][j-1]$). Both $T[i-1][j]$ and $T[i][j-1]$ must be correctly computed before $T[i][j]$ can be calculated.

B1 Algorithm Correctness Analysis

Algorithm B1 uses nested loops:

$For i = 1 to n
  For j = 1 to n
    T[i][j] = 2T[i-1][j] + 3T[i][j-1]$

This order processes the table row by row, and within each row, column by column. When $T[i][j]$ is computed:

  • $T[i-1][j]$ (from the previous row) is already computed.
  • $T[i][j-1]$ (from the previous column in the current row) is already computed.

All dependencies are satisfied. Therefore, Algorithm B1 is correct.

B2 Algorithm Correctness Analysis

Algorithm B2 processes entries based on the sum of indices, $s = i+j$:

$For s = 2 to 2n
  For i = 1 to n
    For j = 1 to n
      If (i + j == s)
        T[i][j] = 2T[i-1][j] + 3T[i][j-1]$

For any $T[i][j]$ computed when the sum is $s$:

  • The required values $T[i-1][j]$ and $T[i][j-1]$ have index sums of $s-1$.
  • The outer loop iterates $s$ in increasing order. Thus, all entries with sum $s-1$ are guaranteed to be computed before entries with sum $s$.

All dependencies are satisfied. Therefore, Algorithm B2 is correct.

Correctness Conclusion

Since both Algorithm B1 and Algorithm B2 correctly compute all required dependencies, both algorithms are correct according to the recursive formulation.

Was this answer helpful?

Important Questions from Dynamic Programming

  1. Consider the problem of a chain <A1, A2, A3> of three matrices. Suppose that the dimensions of the matrices are 10 × 100, 100 × 5 and 5 × 50 respectively. There are two different ways of parenthesization : (i) ((A1 A2)A3) and (ii) (A1(A2 A3)). Computing the product according to the first parenthesization is ________ times faster in comparison to the second parenthesization.

  2. Let \(n = 4\) and \((a_{1}, a_{2}, a_{3}, a_{4}) = (\text{do, if, int, while})\). Let \(p(1:4) = \left(\frac{3}{8}, \frac{3}{8}, \frac{1}{8}, \frac{1}{8}\right)\) and \(q(1:4) = \left(\frac{2}{8}, \frac{3}{8}, \frac{1}{8}, \frac{1}{8}, \frac{1}{8}\right)\) where p(i) and q(i) denotes the probability with which we search \(a_{i}\) and the identifier x being searched satisfy \(a_{i} \lt x \lt a_{i+1}\) respectively. The optimal search tree is given by :

  3. Given two sequences X and Y :

    \(X = \langle a, b, c, b, d, a, b \rangle\)

    \(Y = \langle b, d, c, a, b, a \rangle\).

    The longest common subsequence of X and Y is :

  4. The travelling salesman problem can be solved in :

  5. A triangulation of a polygon is a set of T chords that divide the polygon into disjoint triangles. Every triangulation of n-vertex convex polygon has _____ chords and divides the polygon into _____ triangles.

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App