Dynamic Programming · Algorithms
15 questions · asked in 10 of 27 years (2000–2026)
MCQ 12 · NAT 31-mark 4 · 2-mark 11
2000Questions per year2026
Questions 1–5 of 15
Consider a table , where the elements , 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:
Consider the following two algorithms to compute entries of . Assume that for both the algorithms, for all has been initialized to .
Algorithm is said to be correct if and only if it calculates the correct values of , for all , (as per the recursive formulation) at the end of the execution of the algorithm .
Which one of the following statements is true?
Consider the following two algorithms to compute entries of . Assume that for both the algorithms, for all has been initialized to .
Algorithm is said to be correct if and only if it calculates the correct values of , for all , (as per the recursive formulation) at the end of the execution of the algorithm .
Which one of the following statements is true?
Answer: (A)
To correctly compute , both dependencies — (the cell directly above) and (the cell directly to the left) — must already hold their final, correct values before is computed.
Algorithm B1 (row-major: outer loop over , inner over ): when computing , was already finalized in the previous (fully completed) row, and was already finalized earlier in the same row (since and the inner loop moves left-to-right). Both dependencies are satisfied in time. Correct.
Algorithm B2 (processes cells in order of increasing anti-diagonal ): for a cell with , its dependency lies on diagonal and also lies on diagonal — both are processed in the strictly earlier iteration (all cells of a smaller diagonal are done before any cell of a larger diagonal begins). Both dependencies are also satisfied in time. Correct.
Answer: Both algorithms B1 and B2 are correct.
Algorithm B1 (row-major: outer loop over , inner over ): when computing , was already finalized in the previous (fully completed) row, and was already finalized earlier in the same row (since and the inner loop moves left-to-right). Both dependencies are satisfied in time. Correct.
Algorithm B2 (processes cells in order of increasing anti-diagonal ): for a cell with , its dependency lies on diagonal and also lies on diagonal — both are processed in the strictly earlier iteration (all cells of a smaller diagonal are done before any cell of a larger diagonal begins). Both dependencies are also satisfied in time. Correct.
Answer: Both algorithms B1 and B2 are correct.
Assume that multiplying a matrix G1 of dimension pxq with another matrix G2 of dimension qxr requires pqr scalar multiplications. Computing the product of n matrices G1G2G3...Gn can be done by parenthesizing in different ways. Define Gi Gi+1 as an explicitly computed pair for a given paranthesization if they are directly multiplied. For example, in the matrix multiplication chain G1G2G3G4G5G6 using parenthesization (G1(G2G3))(G4(G5G6)), G2G3 and G5G6 are the only explicitly computed pairs. Consider a matrix multiplication chain F1F2F3F4F5, where matrices F1, F2, F3, F4 and F5 are of dimensions 2x25, 25x3, 3x16, 16x1 and 1x1000, respectively. In the parenthesization of F1F2F3F4F5 that minimizes the total number of scalar multiplications, the explicitly computed pairs is/are
Answer: (C)
The dimensions are 2, 25, 3, 16, 1, 1000. Multiplying an matrix by a matrix costs .
is . Its left partner is some product of size , and that last step costs . For this is 2000. For the sizes are , so the step costs at least 3000, which is already more than the total below. So is multiplied last with the product of , which is a matrix, costing 2000.
Now find the cheapest way to multiply :
- : , then , then . Total 173.
- : .
- : .
- Any grouping with first costs at least .
So the minimum for the first four is 173, and the total is . For example, costs 2204, which is more.
In the best grouping, the only pair of original matrices multiplied directly is .
Answer: (C)
is . Its left partner is some product of size , and that last step costs . For this is 2000. For the sizes are , so the step costs at least 3000, which is already more than the total below. So is multiplied last with the product of , which is a matrix, costing 2000.
Now find the cheapest way to multiply :
- : , then , then . Total 173.
- : .
- : .
- Any grouping with first costs at least .
So the minimum for the first four is 173, and the total is . For example, costs 2204, which is more.
In the best grouping, the only pair of original matrices multiplied directly is .
Answer: (C)
The Floyd-Warshall algorithm for all-pair shortest paths computation is based on
Answer: (C)
The Floyd-Warshall algorithm works by using , and it reuses the answers of smaller subproblems. This is the dynamic programming approach.
Answer: Dynamic Programming paradigm
Answer: Dynamic Programming paradigm
Let A1,A2,A3, and A4 be four matrices of dimensions 10x5, 5x20, 20x10, and 10x5, respectively. The minimum number of scalar multiplications required to find the product A1A2A3A4 using the basic matrix multiplication method is __________.
Answer: 1500
The dimensions are 10, 5, 20, 10, 5. Let be the least cost of multiplying . A product of an and a matrix costs .
Pairs: , , .
Three matrices:
- : ; . So .
- : ; . So .
All four:
-
-
-
The minimum is 1500.
Answer: 1500
Pairs: , , .
Three matrices:
- : ; . So .
- : ; . So .
All four:
-
-
-
The minimum is 1500.
Answer: 1500
A Young tableau is a 2D array of integers increasing from left to right and from top to bottom. Any unfilled entries are marked with , and hence there cannot be any entry to the right of, or below a . The following Young tableau consists of unique entries.
When an element is removed from a Young tableau, other elements should be moved into its place so that the resulting table is still a Young tableau (unfilled entries may be filled in with a ). The minimum number of entries (other than 1) to be shifted, to remove 1 from the given Young tableau is _______.
When an element is removed from a Young tableau, other elements should be moved into its place so that the resulting table is still a Young tableau (unfilled entries may be filled in with a ). The minimum number of entries (other than 1) to be shifted, to remove 1 from the given Young tableau is _______.
Answer: 5
Taking out 1 leaves a hole. Keep filling the hole with the smaller of the entry on its right and the entry below it:
- Hole at (1,1): min(2, 3) = 2, so 2 moves left
- Hole at (1,2): min(5, 4) = 4, so 4 moves up
- Hole at (2,2): min(6, 12) = 6, so 6 moves left
- Hole at (2,3): min(23, 18) = 18, so 18 moves up
- Hole at (3,3): min(25, ) = 25, so 25 moves left
- Hole at (3,4) is filled with
Entries shifted: 2, 4, 6, 18, 25. That is 5 entries.
Answer: 5
- Hole at (1,1): min(2, 3) = 2, so 2 moves left
- Hole at (1,2): min(5, 4) = 4, so 4 moves up
- Hole at (2,2): min(6, 12) = 6, so 6 moves left
- Hole at (2,3): min(23, 18) = 18, so 18 moves up
- Hole at (3,3): min(25, ) = 25, so 25 moves left
- Hole at (3,4) is filled with
Entries shifted: 2, 4, 6, 18, 25. That is 5 entries.
Answer: 5
Keep practising