Consider a table 𝑇, where the elements 𝑇[𝑖][𝑗], 0 ≀ 𝑖,𝑗 ≀ 𝑛, represent…

2026

Consider a table 𝑇, where the elements 𝑇[𝑖][𝑗], 0 ≀ 𝑖,𝑗 ≀ 𝑛, 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:
𝑇[0][π‘˜] = 𝑇[π‘˜][0] = 1 for π‘˜ = 0,1,2, … , 𝑛
𝑇[𝑖][𝑗] = 2𝑇[𝑖 βˆ’ 1][𝑗] + 3𝑇[𝑖][𝑗 βˆ’ 1] for 1 ≀ 𝑖,𝑗 ≀ n
Consider the following two algorithms to compute entries of 𝑇. Assume that for both the algorithms, for all 0 ≀ 𝑖,𝑗 ≀ 𝑛, 𝑇[𝑖][𝑗] has been initialized to 1.
Algorithm 𝐡1: For 𝑖 = 1, 2, … , 𝑛
For 𝑗 = 1, 2, … , 𝑛
𝑇[𝑖][𝑗] = 2𝑇[𝑖 βˆ’ 1][𝑗] + 3𝑇[𝑖][𝑗 βˆ’ 1]

Algorithim B2 :for S:2,3......2n
For 𝑖 = 1, 2, … , 𝑛
For 𝑗 = 1, 2, … , 𝑛
If (𝑖 + 𝑗 == 𝑠)
𝑇[𝑖][𝑗] = 2𝑇[𝑖 βˆ’ 1][𝑗] + 3𝑇[𝑖][𝑗 βˆ’ 1]

Algorithm π΅π‘˜, π‘˜ ∈ {1,2} is said to be correct if and only if it calculates the correct values of 𝑇[𝑖][𝑗], for all 0 ≀ 𝑖,𝑗 ≀ 𝑛, (as per the recursive formulation) at the end of the execution of the algorithm π΅π‘˜.
Which one of the following statements is true?

  1. A.

    Both algorithms B1 and B2 are correct

  2. B.

    Algorithm B1 correct, B2 incorrect

  3. C.

    Algorithm B2 correct, B1 incorrect

  4. D.

    Both incorrect

Attempted by 58 students.

Show answer & explanation

Correct answer: A

The recurrence relation depends on T[i-1][j] and T[i][j-1]. Both algorithms must ensure these values are computed before T[i][j].

Algorithm B1 iterates i from 1 to n, then j from 1 to n. When computing T[i][j], T[i-1][j] is from the previous row (already computed) and T[i][j-1] is from the current row (already computed). Thus, B1 is correct.

Algorithm B2 iterates S from 2 to 2n. Dependencies T[i-1][j] and T[i][j-1] have index sums S-1. Since S increases, values for S-1 are computed before S. Thus, B2 is also correct.

Conclusion: Both algorithms respect the dependency structure and compute the table correctly.

Explore the full course: Gate Guidance By Sanchit Sir