Dynamic-Programming

Question 1
 Define Rn< >to be the maximum amount earned by cutting a rod of length n metres into one or more pieces of integer length and selling them. For i > 0, let p[i] denote the selling price 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 R7?
A
R7is achieved by three different solutions.
B
R7=18
C
R7=19
D
R7cannot be achieved by a solution consisting of three pieces.
Question 1 Explanation: 
The rod length of R7 is 18.

There are 3 different possible ways to get the maximum amount.
P[6] + P[1] → 17+1 = 18
P[2] + P[2] + P[3] → 5+5+8 = 18
P[7] → 18 = 18
Question 2

Consider the following table

Match the algorithm to design paradigms they are based on:

A
(P)↔(ii), Q↔(iii), (R)↔(i)
B
(P)↔(iii), Q↔(i), (R)↔(ii)
C
(P)↔(ii), Q↔(i), (R)↔(iii)
D
(P)↔(i), Q↔(ii), (R)↔(iii)
Question 2 Explanation: 
(P) Kruskal’s and Prim’s algorithms for finding Minimum Spanning Tree(MST). To find MST we are using greedy technique.
(Q) QuickSort is a Divide and Conquer algorithm.
(R) Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem using Dynamic Programming.
Some important points regarding Greedy Vs Dynamic Programming
Greedy: →
It always gives polynomial time complexity
→ It is not an optimal
→ It always selects only either minimum or maximum among all possibilities
→ Ex: Dijkstra’s algorithm for SSSP, Optimal Merge Pattern, Huffman coding, Fractional knapsack problem, etc..,
Dynamic Programming:
→ It gives either polynomial or exponential time complexity.
→ It gives always an optimal result.
→ It checks all possibilities of a problem.
→ Ex: Longest Common sequence, Matrix chain Multiplication, Travelling sales Problem, etc.
Question 3

Consider a sequence of 14 elements: A = [-5, -10, 6, 3, -1, -2, 13, 4, -9, -1, 4, 12, -3, 0]. The subsequence sum . Determine the maximum of S(i,j), where 0 ≤ i ≤ j < 14. (Divide and conquer approach may be used)

A
19
B
39
C
29
D
09
Question 3 Explanation: 
First understand the subsequence is an array is
Ex:
{A, B, C, D}
{A, AB, AC, AD, ABC, ABD, ACD, B, BC, BD, BCD, C, CD, D }
Step-1: Array of elements A = [-5, -10, 6, 3, -1, -2, 13, 4, -9, -1, 4, 12, -3, 0 ]
Step-2: As per the given question, if they want to find maximum subsequence means
{6,3,13,4,4,12}
= 42
Step-3: But according to given recurrence relation, the sequence should be continuous. {6,3,13,4,4,12}.
This is not continuous subsequence.
Step-4: The continuous sequence is {6, 3, -1, -2, 13, 4, -9, -1, 4, 12}
Total is {29}.
Note: We can't get more than 29 maximum subsequence sum.
Question 4

The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1,a2,a3,...,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]
Question 4 Explanation: 
X[i, j] (2≤i≤n and ai≤j≤W) is true if any of the following is true.
1) Sum of weights excluding ai is equal to j, i.e., if X[i-1, j] is true.
2) Sum of weights including ai is equal to j, i.e., if X[i-1, j-ai] is true so that we get (j – ai) + ai as j.
Question 5

The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1,a2,a3,...,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]
Question 5 Explanation: 
The last row and last column given the value is 1 then subset of whose elements sum to W.
Note: As per present GATE syllabus, this concept is not required.
Question 6

A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences X[m] and Y[n] of lengths m and n respectively, with indexes of X and Y starting from 0.

We wish to find the length of the longest common sub-sequence(LCS) of X[m] and Y[n] as l(m,n), where an incomplete recursive definition for the function l(i,j) to compute the length of The LCS of X[m] and Y[n] is given below:

l(i,j) = 0, if either i=0 or j=0
       = expr1, if i,j>0 and X[i-1]=Y[j-1]
       = expr2, if i,j>0 and X[i-1]!=Y[j-1] 

Which one of the following options is correct?

A
expr1 ≡ l(i-1, j) + 1
B
expr1 ≡ l(i, j-1)
C
expr2 ≡ max(l(i-1, j), l(i, j-1))
D
expr2 ≡ max(l(i-1,j-1), l(i,j))
Question 6 Explanation: 
The recursive definition for the function l(i, j) to compute the length of the LCS of X[m] and Y[n] is given below,
Question 7

A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences X[m] and Y[n] of lengths m and n respectively, with indexes of X and Y starting from 0.

The values of l(i,j) could be obtained by dynamic programming based on the correct recursive definition of l(i,j) of the form given above, using an array L[M,N], where M = m+1 and N=n+1, such that L[i,j] = l(i,j).

Which one of the following statements would be TRUE regarding the dynamic programming solution for the recursive definition of l(i,j)?

A
All elements L should be initialized to 0 for the values of l(i,j) to be properly computed.
B
The values of l(i,j) may be computed in a row major order or column major order of L(M,N).
C
The values of l(i,j) cannot be computed in either row major order or column major order of L(M,N).
D
L[p,q] needs to be computed before L[r,s] if either p
Question 7 Explanation: 
LCS procedure can followed by either row major or column major order. We can get the same solution by any order. The above question looks very big but they are explaining the procedure of LCS.
Question 8

The weight of a sequence a0, a1, ..., an-1 of real numbers is defined as a0+a1/2+...+ an-1/2n-1. A subsequence of a sequence is obtained by deleting some elements from the sequence, keeping the order of the remaining elements the same. Let X denote the maximum possible weight of a subsequence of a0, a1, ...,an-1 and Y the maximum possible weight of a subsequence of a1, a2, ..., an-1. Then X is equal to

A
max(Y, a0+Y)
B
max(Y, a0+Y/2)
C
max(Y, a0+2Y)
D
a0+Y/2
Question 8 Explanation: 
Using concepts of Dynamic Programming, to find the maximum possible weight of a subsequence of X, we will have two alternatives:
1. Do not include a0 in the subsequence: then the maximum possible weight will be equal to maximum possible weight of a subsequence of {a1, a2,….an} which is represented by Y
2. Include a0: then maximum possible weight will be equal to : a0 + (each number picked in Y will get divided by 2) = a0 + Y/2.
Key point here is Y will itself pick optimal subsequence to maximize the weight. The value of a0 can be anything (negative or i∈R it is possible that Y>a0+Y/2.
Note: Y/2 meaning: Each number picked in Y will get divided by 2.
Question 9

An algorithm to find the length of the longest monotonically increasing sequence of numbers in an array A[0:n-1] is given below. Let Li denote the length of the longest monotonically increasing sequence starting at index i in the array

Initialize Ln-1=1.
For all i such that 0≤i≤n-2

Finally the length of the longest monotonically increasing sequence is Max(L0, L1, ..., Ln-1).

Which of the following statements is TRUE?

A
The algorithm uses dynamic programming paradigm
B
The algorithm has a linear complexity and uses branch and bound paradigm
C
The algorithm has a non-linear polynomial complexity and uses branch and bound paradigm
D
The algorithm uses divide and conquer paradigm
Question 9 Explanation: 
Key Note of Dynamic programming: 1. Dynamic programming is when you use past knowledge to make solving a future problem easier.
2. Dynamic programming is a technique used to avoid computing multiple time the same sub-problem in a recursive algorithm.
Note: It is neither backtracking nor branch and bound because we are not branching anywhere in the solution space. The algorithm is not divide and conquer as we are not dividing the problem and then merging the solution
Question 10

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. When multiplied as (((M1 X M2) X M3) X M4), the total number of scalar multiplications is pqr + prs + pst.

If p = 10, q = 100, r = 20, s = 5 and t = 80, then the number of scalar multiplications needed is:

A
248000
B
44000
C
19000
D
25000
Question 10 Explanation: 
Given matrices are,

p = 10
q = 100
r = 20
s = 5
t = 80
Total no. of matrix multiplication for (n+1) matrices,
2nCn/(n+1)
Here,
n+1 = 4
n = 3
So, total no. of matrix multiplication possible is,
6C3/4 = ∠6/∠3×∠3×4 = 5
So, total 5 cases are possible,
Case 1:
(((M1 × M2) × M3) × M4)

∴ Total no. of scalar multiplications needed is,
20000 + 4000 + 1000 = 25000
Case 2:
((M1 × M2) × (M3 × M4))

∴ Total no. of scalar multiplications needed is,
20000 + 8000 + 16000 = 44000
Case 3:
(M1 ×(M2 ×(M3 × M4)))

∴ Total scalar multiplications,
160000 + 80000 + 8000 = 248000
Case 4:
((M1 ×(M2 ×M 3)) × M4)

∴ Total scalar multiplications,
10000 + 5000 + 4000 = 19000
Case 5:
(M1 × ((M2 × M3) × M4))

∴ Total scalar multiplications,
10000 + 40000 + 80000 = 130000
Hence, the minimum number of scalar multiplications is, 19000.
Question 11

What is the time complexity of Bellman-Ford single-source shortest path algorithm on a complete graph of n vertices?

A
Θ(n2)
B
Θ(n2 log n)
C
Θ(n3)
D
Θ(n3 log n)
Question 11 Explanation: 
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is capable of handling graphs in which some of the edge weights are negative numbers.
Bellman–Ford runs in O(|V| * |E|) time, where |V| and |E| are the number of vertices and edges respectively.
Note: For complete graph: |E| = n(n-1)/2 = O(n2)
Question 12

Consider two strings A = "qpqrr" and B = "pqprqrp". Let x be the length of the longest common subsequence (not necessarily contiguous) between A and B and let y be the number of such longest common subsequences between A and B. Then x+10y = _________.

A
34
B
35
C
36
D
37
Question 12 Explanation: 
Given is
A = “qpqrr” B = “pqprqrp”
The longest common subsequence (not necessarily contiguous) between A and B is having 4 as the length, so x=4 and such common subsequences are as follows:
(1) qpqr
(2) pqrr
(3) qprr
So y = 3 (the number of longest common subsequences) hence x+10y = 4+10*3 = 34
There are 12 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access