Algorithm GATE 2018 Solved Questions

Ques 1:Assume that multiplying a matrix G1 of dimension 𝑝 × 𝑞 with another matrix G2 of dimension 𝑞 × 𝑟 requires 𝑝𝑞𝑟 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 2×25, 25×3, 3×16, 16×1 and 1×1000, respectively. In the parenthesization of F1F2F3F4F5 that minimizes the total number of scalar multiplications, the explicitly computed pairs is/are

GATE 2018 Q no 31

(A) F1F2 and F3F4 only

(B) F2F3 only

(C) F3F4 only

(D) F1F2 and F4F5 only

Ans: (C) F3F4 only

Solution: If we multiply anything with F5 we will get much greater multiplication cost because F5 is 1∗1000 matrix so 1000 will play vital role in cost. So we will multiply F5 at very last step.

So, here is the sequence giving minimal cost:

(F1(F2(F3F4)))(F5)=48+75+50+2000=2173

Explicitly computed pairs is F3F4

Ques 2:Consider the following undirected graph G

GATE 2018 Q no 47

Choose a value for x that will maximize the number of minimum weight spanning trees (MWSTs) of G. The number of MWSTs of G for this value of x is __.

Ans: 4

Solution: We can use Kruskal algo. i.e. keep selecting min weight edges and if a cycle occurs then ignore that edge.

Do this until you select n−1 edges.

Total= 2x 2 = 4 choices

Ques 3: Consider the weights and values of items listed below. Note that there is only one unit of each item.

GATE 2018 Q no 48

Item number
(in Kgs)
WeightValue
(in Rupees)
11060
2728
3420
4224

The task is to pick a subset of these items such that their total weight is no more than 11 Kgs and their total value is maximized. Moreover, no item may be split. The total value of items picked by an optimal algorithm is denoted by 𝑉opt. A greedy algorithm sorts the items by their value-to-weight ratios in descending order and packs them greedily, starting from the first item in the ordered list. The total value of items picked by the greedy algorithm is denoted by 𝑉greedy.

The value of 𝑉opt − 𝑉greedy is __.

Ans: 16

Solution: Vopt is clearly 60.

You can go for brute force or by normal intuition you can get it.

Now solving for Vgreedy

Item number
(in Kgs)
WeightValue
(in Rupees)
Value/Weight
110606
27284
34205
422412

Sort them in descending order of Value/Weight as per the question.

Item number
(in Kgs)
WeightValue
(in Rupees)
Value/Weight
422412
110606
34205
27284
  • Item 4 is picked. Weight remaining = 11−2=9
  • Item 1 cannot be picked as 10kg >9kg.
  • Item 3 can be picked as 4kg < 9kg. Weight Remaining = 9−4=5kg
  • Item 2 cannot be picked as 7kg >5kg.

So, item 4 and Item 3 are picked. Their values are 24 and 20 respectively.

Vgreedy=24+20=44.

Voptimal−Vgreedy=60−44=16.

error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.