$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 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.
The recursive formulation for the table entries is:
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.
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:
All dependencies are satisfied. Therefore, Algorithm B1 is correct.
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$:
All dependencies are satisfied. Therefore, Algorithm B2 is correct.
Since both Algorithm B1 and Algorithm B2 correctly compute all required dependencies, both algorithms are correct according to the recursive formulation.
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.
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 :
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 :
The travelling salesman problem can be solved in :
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.