## Algorithms

 Question 1

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
Algorithms       GATE 2019
Question 1 Explanation:
First understand the subsequence is an array is
Ex: {A,B,C,D}
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 2

An array of 25 distinct elements is to be sorted using quicksort. Assume that the pivot element is chosen uniformly at random. The probability that the pivot element gets placed in the worst possible location in the first round of partitioning (rounded off to 2 decimal places) is _____.

 A 0.08 B 0.01 C 1 D 8
Algorithms       GATE 2019
Question 2 Explanation:
Step-1: Given, 25 distinct elements are to be sorted using quicksort.
Step-2: Pivot element = uniformly random.
Step-3: Worst case position in the pivot element is either first (or) last.
Step-4: So total 2 possibilities among 25 distinct elements
= 2/25
= 0.08
 Question 3
Consider the following statements:
```  I. The smallest element in a max-heap is always at a leaf node
II. The second largest element in a max-heap is always a child of the root node
III. A max-heap can be constructed from a binary search tree in Θ(n) time
IV. A binary search tree can be constructed from a max-heap in Θ(n) time
```
Which of the above statements are TRUE?
 A I, II and III B II, III and IV C I, III and IV D I, II and IV
Algorithms       GATE 2019
Question 3 Explanation:
i) TRUE: The smallest element in heap is always a leaf node but depends upon the graph, it may be left or right side of the graph.
(ii) TRUE: The second smallest element in a heap is always a child of root node. (iii) TRUE: Converting from binary search tree to max heap will take O(n) time as well as O(n) space complexity.
(iv) FALSE: We can’t convert max heap to binary search tree in O(n) time.
 Question 4

There are n unsorted arrays: A1, A2, ..., An. Assume that n is odd. Each of A1, A2, ..., An contains n distinct elements. There are no common elements between any two arrays. The worst-case time complexity of computing the median of the medians of A1, A2, ..., An is

 A O(n) B O(n log n) C Ω(n2 log n) D O(n2)
Algorithms       GATE 2019
Question 4 Explanation:
Finding the median in an unsorted array is O(n).
But it is similar to quicksort but in quicksort, partitioning will take extra time.
→ Find the median will be (i+j)/2
1. If n is odd, the value is Ceil((i+j)/2)
2. If n is even, the value is floor((i+j)/2)
-> Here, total number of arrays are
⇒ O(n)*O(n)
⇒ O(n2)
Note:
They are clearly saying that all are distinct elements.
There is no common elements between any two arrays.
 Question 5
Let G be any connected, weighted, undirected graph.
```I. G  has a unique minimum spanning tree, if no two edges of G have the same weight.
II. G  has a unique minimum spanning tree, if, for every cut of G, there is a unique minimum-weight edge crossing the cut.
```
Which of the above statements is/are TRUE?
 A I only B II only C Both I and II D Neither I nor II
Algorithms       GATE 2019
Question 5 Explanation:
Given G be a connected, weighted and undirected graph,
I. TRUE: G Graph is unique, no two edges of the graph is same. Step-1: Using Kruskal's algorithm, arrange each weights in ascending order.
17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
Step-2: Step-3: 17 + 18 + 20 + 21 + 22 + 23 + 26 = 147
Step-4: Here, all the elements are distinct. So, the possible MCST is 1.
II. TRUE: As per the above graph, if we are cut the edge, that should the be the minimum edge.
Because we are already given, all minimum edge weights if graph is distinct.
 Question 6
 A B C D Algorithms       Recurrences       GATE 2020
Question 6 Explanation: Question 7
Let G = (V,E) be a directed, weighted graph with weight function w:E--> R. For some function f:V--> R, for each edge (u,v)E, define w'(u,v) as w(u,v)+f(u)-f(v).
Which one of the options completes the following sentence so that it is TRUE?
“The shortest paths in G under w are shortest paths under w’ too, _______”.
 A if and only if f(u) is the distance from s to u in the graph obtained by adding a new vertex s to G and edges of zero weight from s to every vertex of G B C D for every f:V--> R
Algorithms       Graphs-and-Tree       GATE 2020
Question 7 Explanation: Question 8
Let G = (V,E) be a weighted undirected graph and let T be a Minimum Spanning Tree (MST) of G maintained using adjacency lists. Suppose a new weighted edge (u,v) ε V x V is added to G. The worst case time complexity of determining if T is still an MST of the resultant graph is
 A B C D Algorithms       Minimum-Spanning-Tree       GATE 2020
Question 8 Explanation:
Method-1:
• As T is a minimum spanning tree and we need to add a new edge to existing spanning tree.
• Later we need to check still T is a minimum spanning tree or not, So we need to check all vertices whether there is any cycle present after adding a new edge .
• All vertices need to traverse to confirm minimum spanning tree after adding new edge then time complexity is O(V)
Method-2:
Time Complexity:
Total vertices: V, Total Edges : E
• O(logV) – to extract each vertex from the queue. So for V vertices – O(VlogV)
• O(logV) – each time a new pair object with a new key value of a vertex and will be done at most once for each edge. So for total E edge – O(ElogV)
• So overall complexity: O(VlogV) + O(ElogV) = O((E+V)logV) = O(ElogV)
Note: Method-1 is the most appropriate answer for giving a question.
 Question 9
Consider a graph G = (V, E), where V = {v1, v2, …, v100}, E = {(vi, vj) | 1 ≤ i < j ≤ 100}, and weight of the edge (vi, vj) is |i - j|. The weight of the minimum spanning tree of G is ______.
 A 99
Algorithms       Minimum-Spanning-Tree       GATE 2020
Question 9 Explanation:
• If there are n vertices in the graph, then each spanning tree has n − 1 edges.
• N =100
• Edge weight is |i-j| for Edge (vi,vj) {1<=i<=100}
• The weight of edge(v1,v2) is 1 , edge(v5,v6) is 1.
• So 99 edges of weight is 99
 Question 10

Consider the following undirected graph G: 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 _________.

 A 4 B 5 C 6 D 7
Algorithms       Minimum-Spanning-Tree       Gate 2018
Question 10 Explanation:
Here, x = 5 because it is having maximum number of spanning trees.
If x = 5 then the total number of MWSTs are 4.
If r = 1 If r = 2 If r = 3 If r = 4 If r = 5  Question 11

Consider the weights and values of items listed below. Note that there is only one unit of each item. 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 Vopt. 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 Vgreedy.

The value of Vopt − Vgreedy is ______ .

 A 16 B 17 C 18 D 19
Algorithms       0/1-Knapsack-and-fractional-knapsack       Gate 2018
Question 11 Explanation:
First sort value/weight in descending order as per the question: Vopt is clearly = 60
For Vgreedy use the table (Do not take the fraction as per the question),
Item 4 picked,
Profit = 24
Remaining weight = 11 – 2 = 9
Next item 3 picked (item 1 cannot be picked since its capacity is greater than available capacity),
Profit = 24 + 20 = 44
Remaining capacity = 9 – 4 = 5
Now no item can be picked with available capacity.
So Vgreedy = 44
∴ Vopt – Vgreedy = 60 – 44 = 16
 Question 12

Consider the following functions from positives integers to real numbers

The CORRECT arrangement of the above functions in increasing order of asymptotic complexity is:

 A B C D Algorithms       Asymptotic-Complexity       Gate 2017 set-01
Question 12 Explanation:
In this problem, they are expecting to find us “increasing order of asymptotic complexity”.
Step-1: Take n=2048 or 211 (Always take n is very big number)
Step-2: Divide functions into 2 ways
1. Polynomial functions
2. Exponential functions
Step-3: The above functions are belongs to polynomial. So, simply substitute the value of n,
First compare with constant values.
→ 100 / 2048 = 0.048828125
→ 10 > 100/ 2048
→ log2 2048 =11
→ √n = 45.25483399593904156165403917471
→ n = 2048
So, Option B is correct
 Question 13

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)
Algorithms       Gate 2017 set-01
Question 13 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 14

Let G = (V, E) be any connected undirected edge-weighted graph. The weights of the edges in E are positive and distinct. Consider the following statements:

(I) Minimum Spanning Tree of G is always unique.
(II) Shortest path between any two vertices of G is always unique.

Which of the above statements is/are necessarily true?

 A (I) only B (II) only C both (I) and (II) D neither (I) nor (II)
Algorithms       Minimum-Spanning-Tree       Gate 2017 set-01
Question 14 Explanation:
If the graph has all positive and distinct (unique values no duplicates) then Statement-I definitely correct because if we are using either prim’s or kruskal’s algorithm it gives the unique spanning tree.
Let us take an example Step 1:
Using kruskal’s algorithm, arrange each weights in ascending order.
17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
Step 2: Step 3:
17+18+20+21+22+23+26 = 147
Step 4:
Here, all the elements are distinct. So the possible MCST is 1.
Statement-II: May or may not happen, please take an example graph and try to solve it. This is not correct always.
So, we have to pick most appropriate answer.
 Question 15

Let A be an array of 31 numbers consisting of a sequence of 0’s followed by a sequence of 1’s. The problem is to find the smallest index i such that A[i] is 1 by probing the minimum number of locations in A. The worst case number of probes performed by an optimal algorithm is _________.

 A 5 B 6 C 7 D 8
Algorithms       Searching       Gate 2017 set-01
Question 15 Explanation:
→ If we apply binary search to find the first occurrence of 1 in the list, it will give us the smallest index i where 1 is stored.
→ As in this array sequence of 0’s is followed by sequence of 1’s, the array is sorted. We can apply binary search directly without sorting it.
So number of probes = ceil(log2 31) = 4.954196310386876
⇒ here we are using ceiling so it becomes 5
 Question 16

Match the algorithms with their time complexities:

```      Algorithm                                     Time complexity
(P) Towers of Hanoi with n disks                       (i) Θ(n2)
(Q) Binary search given n stored numbers              (ii) Θ(n log⁡ n)
(R) Heap sort given n numbers at the worst case      (iii) Θ(2n)
(S) Addition of two n×n matrices                      (iv) Θ(log⁡ n)
```
 A P→(iii), Q→(iv), R→(i), S→(ii) B P→(iv), Q→(iii), R→(i), S→(ii) C P→(iii), Q→(iv), R→(ii), S→(i) D P→(iv), Q→(iii), R→(ii), S→(i)
Algorithms       Match-the-Following       GATE 2017(set-02)
Question 16 Explanation:
In this problem, we have to find Average case of different algorithms
→ Tower of Hanoi with n disks takes θ(2n) time
It is a mathematical game or puzzle.
It consists of three rods and a number of disks of different sizes, which can slide onto any rod.
The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack.
3. No disk may be placed on top of a smaller disk.
With 3 disks, the puzzle can be solved in 7 moves.
The minimal number of moves required to solve a Tower of Hanoi puzzle is 2n-1, where n is the number of disks.
→ Binary Search given n sorted numbers takes Ɵ(log2 n)
→ Heap sort given n numbers of the worst case takes Ɵ(n log n)
→ Addition of two n×n matrices takes Ɵ(n2)
 Question 17
 A Θ(log⁡log⁡n) B Θ(log⁡n) C Θ(√n) D Θ(n)
Algorithms       Time-Complexity       GATE 2017(set-02)
Question 17 Explanation:
T(n) = 2T(√n) + 1
(or)
T(n) = 2T(n(1⁄2)) + 1
Here, Assume n = 2k
T(2k) = 2T(2k)(1⁄2) + 1
T(2k) = 2T(2(k/2) ) + 1
Assume T(2k) = S(k)
S(k) = 2S(k/2) + 1
Apply Master’s theorem Case-1
a=2, b=2
S(k) = k(log22 )
S(k) = θ(k')
but S(k) = T(2k)
T(2k) = θ(k')
but n = 2k
k = log⁡n
T(n) = θ(logn)
 Question 18

Consider the following C function.

```int fun (int n)   {
int i, j;
for (i = 1; i <= n; i++)   {
for (j = 1; j < n; j += i)   {
printf("%d %d",i,j);
}
}
}
```

Time complexity of fun in terms of Θ notation is

 A Θ(n√n) B Θ(n2 ) C Θ(n log⁡n) D Θ(n2 logn)
Algorithms       Time-Complexity       GATE 2017(set-02)
Question 18 Explanation:
We can solve iterative programs time complexity with the help of rollback method.
int fun(int n)
{
int i, j;
for (i = 1; i <= n ; i++)    /* It is independent loop. So, it takes O(n) time */
{
for (j = 1; j < n; j += i)     /* It is dependent loop, It always incrementing with the help of i. It will take approximately O(log n) times*/
{
printf("%d %d", i, j);     /* It takes O(1) time */
}
}
}
So, the overall complexity of the program is θ(n log⁡n) times.
 Question 19

A message is made up entirely of characters from the set X = {P, Q, R, S, T}. The table of probabilities for each of the characters is shown below: If a message of 100 characters over X is encoded using Huffman coding, then the expected length of the encoded message in bits is __________.

 A 225 B 226 C 227 D 228
Algorithms       Huffman-Coding       GATE 2017(set-02)
Question 19 Explanation: General procedure to solve Huffman coding problem
Step-1: Arrange into either descending/ ascending order according to that profits.
Step-2: Apply optimal merge pattern procedure.
Step-3: Make left sub-tree value either 0 or 1, for right sub-tree, vice-versa.    = 2 × 0.34 + 2 × 0.22 + 2 × 0.19 + 3 × 0.17 + 3 × 0.08
= 2.25
∴ So, for 100 characters, 2.25 * 100 = 225
 Question 20

The worst case running times of Insertion sort, Merge sort and Quick sort, respectively, are:

 A Θ(nlogn), Θ(nlogn), and Θ(n2) B Θ(n2 ), Θ(n2 ), and Θ(nlogn) C Θ(n2), Θ(nlogn), and Θ(nlogn) D Θ(n2), Θ(nlogn), and Θ(n2)
Algorithms       Sorting       2016 set-01
Question 20 Explanation: Question 21

Let G be a complete undirected graph on 4 vertices, having 6 edges with weights being 1, 2, 3, 4, 5, and 6. The maximum possible weight that a minimum weight spanning tree of G can have is __________.

 A 7 B 8 C 9 D 10
Algorithms       Minimum-Spanning-Tree       2016 set-01
Question 21 Explanation:
Let G be a complete undirected graph with 4 vertices & 6 edges so according to graph theory, if we use Prim’s / Kruskal’s algorithm, the graph looks like Now consider vertex A to make Minimum spanning tree with Maximum weights.
As weights are 1, 2, 3, 4, 5, 6. AB, AD, AC takes maximum weights 4, 5, 6 respectively.
Next consider vertex B, BA = 4, and minimum spanning tree with maximum weight next is BD & BC takes 2, 3 respectively.
And the last edge CD takes 1.
So, 1+2+4 in our graph will be the Minimum spanning tree with maximum weights.
 Question 22

G = (V,E) is an undirected simple graph in which each edge has a distinct weight, and e is a particular edge of G. Which of the following statements about the minimum spanning trees (MSTs) of G is/are TRUE?

I. If e is the lightest edge of some cycle in G, then every MST of G includes e
II. If e is the heaviest edge of some cycle in G, then every MST of G excludes e
 A I only B II only C both I and II D neither I nor II
Algorithms       Minimum-Spanning-Tree       2016 set-01
Question 22 Explanation:
Statement-1: False
The MSTs of G may or may not include the lightest edge.
Take rectangular graph labelled with P,Q,R,S.
Connect with P-Q = 5, Q-R = 6, R-S = 8, S-P = 9, P-R = 7.
When we are forming a cycle R-S-P-R. P-R is the lightest edge of the cycle.
The MST abcd with cost 11
P-Q + Q-R + R-S does not include it.
Statement-2: True
Suppose there is a minimum spanning tree which contains e. If we add one more edge to the spanning tree we will create a cycle.
Suppose we add edge e to the spanning tree which generated cycle C.
We can reduce the cost of the minimum spanning tree if we choose an edge other than e from C for removal which implies that e must not be in minimum spanning tree and we get a contradiction.
 Question 23

Assume that the algorithms considered here sort the input sequences in ascending order. If the input is already in ascending order, which of the following are TRUE?

I. Quicksort runs in Θ(n2) time
II. Bubblesort runs in Θ(n2) time
III. Mergesort runs in Θ(n) time
IV. Insertion sort runs in Θ(n) time
 A I and II only B I and III only C II and IV only D I and IV only
Algorithms       Sorting       GATE 2016 set-2
Question 23 Explanation:
If input sequence is already sorted then the time complexity of Quick sort will take O(n2) and Bubble sort will take O(n) and Merge sort will takes O(nlogn) and insertion sort will takes O(n).
→ The recurrence relation for Quicksort, if elements are already sorted,
T(n) = T(n-1)+O(n) with the help of substitution method it will take O(n2).
→ The recurrence relation for Merge sort, is elements are already sorted,
T(n) = 2T(n/2) + O(n) with the help of substitution method it will take O(nlogn).
We can also use master's theorem [a=2, b=2, k=1, p=0] for above recurrence.
 Question 24

The Floyd-Warshall algorithm for all-pair shortest paths computation is based on

Algorithms       Floyd-Warshall-Algorithm       GATE 2016 set-2
Question 24 Explanation:
→ All Pair shortest path algorithm is using Dynamic Programming technique.
It takes worst case time complexity is O(|V|3) and worst case space complexity is O(|V|2).
→ The Floyd–Warshall algorithm is an algorithm for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles).
→ A single execution of the algorithm will find the lengths (summed weights) of the shortest paths between all pairs of vertices.
Although it does not return details of the paths themselves, it is possible to reconstruct the paths with simple modifications to the algorithm.
 Question 25

Let A1, A2, A3 and A4 be four matrices of dimensions 10 × 5, 5 × 20, 20 × 10, and 10 × 5, respectively. The minimum number of scalar multiplications required to ﬁnd the product A1A2A3A4 using the basic matrix multiplication method is _________.

 A 1500 B 1501 C 1502 D 1503
Algorithms       Matrix-Chain-Multiplication       GATE 2016 set-2
Question 25 Explanation:
→ The minimum number of scalar multiplications required is 1500.
The optimal parenthesized sequence is A1((A2A3)A4) out of many possibilities, the possibilities are
1. ((A1A2)A3)A4
2. ((A1(A2A3))A4)
3. (A1A2)(A3A4)
4. A1((A2A3)A4)
5. A1(A2(A3A4))
→ A1((A2A3)A4) = (5 x 20 x 10) + (5 x 10 x 5) + (10 x 5 x 5) = 1000 + 250 + 250 = 1500
 Question 26

The given diagram shows the ﬂowchart for a recursive function A(n). Assume that all statements, except for the recursive calls, have O(1) time complexity. If the worst case time complexity of this function is O(nα), then the least possible value (accurate upto two decimal  positions) of α is _________. A 2.32193 B 2.32193 C 2.32193 D 2.32193
Algorithms       Time-Complexity       GATE 2016 set-2
Question 26 Explanation:
This type of problem, we have to consider worst case time complexity, it mean that all possibilities.
According to flow chart total 5 worst case possibilities of function calls. The remaining function calls/return statements will execute only constant amount of time.
So, total function calls 5.
The Recurrence will be
A(n) = 5A(n/2) + O(1)
Apply Master’s theorem,
A=5, b=2, k=0, p=0
a > bk case
A(n) = n(logba ) = n(log25) = n2.3219280
∴α = 2.3219280
 Question 27

Which one of the following is the recurrence equation for the worst case time complexity of the Quicksort algorithm for sorting n(≥ 2) numbers? In the recurrence equations given in the options below, c is a constant.

 A T(n) = 2T(n/2) + cn B T(n) = T(n – 1) + T(1) + cn C T(n) = 2T(n – 1) + cn D T(n) = T(n/2) + cn
Algorithms       Quick-Sort       GATE 2015 (Set-01)
Question 27 Explanation:
When the pivot is the smallest (or largest) element at partitioning on a block of size n the result yields one empty sub-block, one element (pivot) in the correct place and sub block of size n-1.
Hence recurrence relation T(n) = T(n - 1) + T(1) + cn.
 Question 28
 A P-iii, Q-ii, R-iv, S-i B P-i, Q-ii, R-iv, S-iii C P-ii, Q-iii, R-iv, S-i D P-ii, Q-i, R-iii, S-iv
Algorithms       Algorithms       GATE 2015 (Set-01)
Question 28 Explanation:
Prim’s algorithm always select minimum distance between two of its sets which is nothing but greedy method.
Floyd-warshall always changes it distance at each iteration which is nothing but dynamic programming.
Merge sort in merge sort first we always divide and merge to perform sorting hence divide and conquer.
Hamiltonian circuit used to reach the entire vertex once, if some vertex is repeating in its path it will backtrack.
 Question 29

An algorithm performs (log N)1/2 find operations, N insert operations, (log N)1/2 delete operations, and (log N)1/2 decrease-key operations on a set of data items with keys drawn from a linearly ordered set. For a delete operation, a pointer is provided to the record that must be deleted. For the decrease-key operation, a pointer is provided to the record that has its key decreased. Which one of the following data structures is the most suited for the algorithm to use, if the goal is to achieve the best total asymptotic complexity considering all the operations?

 A Unsorted array B Min-heap C Sorted array D Sorted doubly linked list
Algorithms       Time-Complexity       GATE 2015 (Set-01)
Question 29 Explanation: Question 30
The graph shown below 8 edges with distinct integer edge weights. The minimum spanning tree (MST) is of weight 36 and contains the edges: {(A, C), (B, C), (B, E), (E, F), (D, F)}. The edge weights of only those edges which are in the MST are given in the figure shown below. The minimum possible sum of weights of all 8 edges of this graph is ______________. A 69 B 70 C 71 D 72
Algorithms       Minimum-Spanning-Tree       GATE 2015 (Set-01)
Question 30 Explanation: ⇒ Total sum = 10 + 9 + 2 + 15 + 7 + 16 + 4 + 6 = 69
 Question 31
 A 1-i, 2-iii, 3-i, 4-v. B 1-iii, 2-iii, 3-i, 4-v. C 1-iii, 2-ii, 3-i, 4-iv. D 1-iii, 2-ii, 3-i, 4-v.
Question 31 Explanation:
(1) Dijkstra’s Shortest Path using to find single source shortest path. It is purely based on Greedy technique because it always selects shortest path among all.
(2) → All Pair shortest path algorithm is using Dynamic Programming technique. It takes worst case time complexity is O(|V|3) and worst case space complexity is O(|V|2).
→ The Floyd–Warshall algorithm is an algorithm for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles).
→ A single execution of the algorithm will find the lengths (summed weights) of the shortest paths between all pairs of vertices. Although it does not return details of the paths themselves, it is possible to reconstruct the paths with simple modifications to the algorithm.
(3) Binary search on a sorted array:
Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
(4) Backtracking search on a graph uses Depth-first search
 Question 32
 A (a, left_end, k) and (a + left_end + 1, n – left_end – 1, k – left_end – 1) B (a, left_end, k) and (a, n – left_end – 1, k – left_end – 1) C (a + left_end + 1, n – left_end – 1, k – left_end – 1) and (a, left_end, k) D (a, n – left_end – 1, k – left_end – 1) and (a, left_end, k)
Algorithms       Partitioning-Algorithm       GATE 2015 -(Set-2)
Question 32 Explanation:
QuickSort is used as a sorting algorithm.In QuickSort, we pick a pivot element, then move the pivot element to its correct position and partition the array around it. The idea is, not to do complete quicksort, but stop at the point where pivot itself is k’th smallest element. Also, not to recur for both left and right sides of pivot, but recur for one of them according to the position of pivot.
 Question 33
Consider a typical disk that rotates at 15000 rotations per minute (RPM) and has a transfer rate of 50×106 bytes/sec. If the average seek time of the disk is twice the average rotational delay and the controller’s transfer time is 10 times the disk transfer time, the average time (in milliseconds) to read or write a 512-byte sector of the disk is _____________.
 A 6 B 6.1 C 6.2 D 6.3
Algorithms       Secondary-Storage       GATE 2015 -(Set-2)
Question 33 Explanation:
15000 rotations → 60 sec
1 rotation → 60 /15000 sec = 4 ms
∴ Average rotational delay = 1/2 × 4 ms = 2 ms
Average seek time = 2 × Average rotational delay
= 2 × 2 ms = 4 ms
Time to transfer 512 byte,
50 × 106B ------- 1s
512 B ------- 512B/ 50×106B/s = 0.01 ms
∴ Controllers transfer time
= 10 × Disk transfer time
= 10 × 0.01 ms
= 0.01 ms
∴ Avg. time = (4 + 2 + 0.1 + 0.01)ms
= 6.11 ms
 Question 34
Assume that a mergesort algorithm in the worst case takes 30 seconds for an input of size 64. Which of the following most closely approximates the maximum input size of a problem that can be solved in 6 minutes?
 A 256 B 512 C 1024 D 2048
Algorithms       Merge-Sort       GATE 2015(Set-03)
Question 34 Explanation:
Time complexity of merge sort = O(n log n) = an log n
where c is some constant
It is given that for n = 64,
cn log n = 30
c × 64 log 64 = 30
c × 64 × 6 = 30
c = 5/64
Now, the value of n for
c n log n = 360
5/64 × n log n = 360
n log n = 360×64/5
= 72 × 64 = 29 × 9
So for n = 29, it satisfies.
So, n = 29 = 512
 Question 35
Let G be a connected undirected graph of 100 vertices and 300 edges. The weight of a minimum spanning tree of G is 500. When the weight of each edge of G is increased by five, the weight of a minimum spanning tree becomes __________.
 A 995 B 996 C 997 D 998
Algorithms       Minimum-Spanning-Tree       GATE 2015(Set-03)
Question 35 Explanation:
G has 100 vertices ⇒ spanning tree contain 99 edges given, weight of a minimum spanning tree of G is 500 since, each edge of G is increased by five. ∴ Weight of a minimum spanning tree becomes 500 + 5 ⨯ 99= 995
 Question 36
There are 5 bags labeled 1 to 5.  All the coins in a given bag have the same weight.  Some bags have coins of weight 10 gm, others have coins of weight 11 gm.  I pick 1, 2, 4, 8, 16 coins respectively from bags 1 to 5.  Their total weight comes out to 323 gm.  Then the product of the labels of the bags having 11 gm coins is _______.
 A 12 B 13 C 14 D 15
Algorithms       General       GATE 2014(Set-01)
Question 36 Explanation:
Bags: 1 2 3 4 5
No. of coins picked: 1 2 4 8 16
There are two types of weights of coin, i.e., 10gm and 11gm. And total weight of picked coins are 323gm.
Let there be x number of 10gm coins and y number of 11gm coins.
So, 10x + 11y = 323 ------ (1)
And we also know that total number of coins picked is
1 + 2 + 4 + 8 + 16 = 31 which is equal to x + y, so,
= 31 ------ (2)
Solving equation (1) and (2), we get
y = 13
Means total there are 13 coins of 11gm.
Now we can chose bag number 1, 3 and 4, we will get a total sum of 13 coins picked from them.
So product of labelled bag number = 1×3×4 = 12
 Question 37
Suppose a polynomial time algorithm is discovered that correctly computes the largest clique in a given graph. In this scenario, which one of the following represents the correct Venn diagram of the complexity classes P, NP and NP Complete (NPC)?
 A B C D Algorithms       P-NP       GATE 2014(Set-01)
Question 37 Explanation:
The most important open question in complexity theory is whether the P = NP, which asks whether polynomial time algorithms actually exist for NP-complete and all NP problems (since a problem “C” is in NP-complete, iff C is in NP and every problem in NP is reducible to C in polynomial time). In the given question it is given that some polynomial time algorithm exists which computes the largest clique problem in the given graph which is known NP-complete problem. Hence P=NP=NP-Complete.
 Question 38
The minimum number of comparisons required to find the minimum and the maximum of 100 numbers is _________________.
 A 148 B 149 C 150 D 151
Algorithms       General       GATE 2014(Set-01)
Question 38 Explanation:
The minimum number of comparisons required to find the minimum and the maximum of 100 numbers is 3n/2 – 2 = 148. (∵ T(n) = T(floor(n/2))+T(ceil(n/2))+2)
 Question 39
Which one of the following correctly determines the solution of the recurrence relation with T(1) = 1?
`T(n) = 2T(n/2) + Logn`
 A Θ(n) B Θ(nlog n) C Θ(n2) D Θ(log⁡n)
Algorithms       Time-Complexity       Gate 2014 Set -02
Question 39 Explanation:
T(n)=2T(n/2)+logn
Apply Master’s theorem,
a=2,b=2,k=0,p=1
According to Master’s theorem, we can apply Case-I
(I) If a>bk, then T(n)=θ(n(logba ) ) =θ(n(log22 ) )= θ (n)
 Question 40
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
Algorithms       Dynamic-Programming       Gate 2014 Set -02
Question 40 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
 Question 41
Suppose P, Q, R, S, T are sorted sequences having lengths 20, 24, 30, 35, 50 respectively.  They are to be merged into a single sequence by merging together two sequences at a time.  The number of comparisons that will be needed in the worst case by the optimal algorithm for doing this is ______.
 A 358 B 359 C 360 D 361
Algorithms       Greedy-Algorithm       Gate 2014 Set -02
Question 41 Explanation:
The implementation of optimal algorithm for merging sequences is as follows. In the above implementation, total number of comparisons is
(44-1) + (94-1) + (65-1) + (159-1) = 358
Hint: The number of comparisons for merging two sorted sequences of length m and n is m+n-1.
 Question 42
The number of distinct minimum spanning trees for the weighted graph below is ____ A 6 B 7 C 8 D 9
Algorithms       Minimum-Spanning-Tree       Gate 2014 Set -02
Question 42 Explanation: Minimum Spanning Tree: From the diagram, CFDA gives the minimum weight so will not disturb them, but in order to reach BE=1 we have 3 different ways ABE/ DBE/ DEB and we have HI=1, the shortest weight, we can reach HI=1 through GHI/ GIH.
So 3*2=6 ways of forming Minimum Spanning Tree with sum as 11.
 Question 43
Consider the following rooted tree with the vertex la beled P as t he root: The order in which the nodes are visited during an i n-order trave rsal of the tree is
 A SQPTRWUV B SQPTUWRV C SQPTWUVR D SQPTRUWV
Algorithms       Tree Traversal       Gate 2014 Set -03
Question 43 Explanation:
The tree can be redrawn as Inorder Traversal: Left, Root, Middle, Right.
So, durig inorder traversal whenever we visit the node second time then print it.
So, output will be,
S Q P T R W U V
 Question 44
Suppose depth first search is executed on the graph below starting at some unknown vertex. Assume that a recursive call to visit a vertex is made only after first checking that the vertex has not been visited earlier. Then the maximum possible recursion depth (including the initial call) is _________. A 19 B 20 C 21 D 22
Algorithms       Graphs       Gate 2014 Set -03
Question 44 Explanation: Note: We should not consider backtrack edges, it reduces recursion depth in stack.
So the maximum possible recursive depth will be 19. Question 45
You have an array of n elements. Suppose you implement quicksort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is
 A O(n2) B O(n log n) C Θ(n log⁡n) D O(n3)
Algorithms       Sorting       Gate 2014 Set -03
Question 45 Explanation:
The Worst case time complexity of quick sort is O (n2). This will happen when the elements of the input array are already in order (ascending or descending), irrespective of position of pivot element in array.
 Question 46
Suppose we have a balanced binary search tree T holding n numbers.  We are given two numbers L and H and wish to sum up all the numbers in T that lie between L and H. Suppose there are m such numbers in T. If the tightest upper bound on the time to compute the sum is O(nalogbn + mclogdn), the value of a + 10b + 100c + 1000d is ____.
 A 110 B 111 C 112 D 113
Algorithms       Binary-Search-Tree       Gate 2014 Set -03
Question 46 Explanation:
It takes (log n) time to determine numbers n1 and n2 in balanced binary search tree T such that
1. n1 is the smallest number greater than or equal to L and there is no predecessor n1' of >n1 such that n1' is equal to n1.
2. n22' of n2 such that is equal to n2.
Since there are m elements between n1 and n2, it takes ‘m’ time to add all elements between n1 and n2.
So time complexity is O(log n+m)
So the given expression becomes O(n0log'n+m'log0n)
And a+10b+100c+1000d=0+10*1+100*1+1000*1=10+100=110
Because a=0, b=1, c=1 and d=0
 Question 47
Consider a hash table with 100 slots. Collisions are resolved using chaining. Assuming simple uniform hashing, what is the probability that the first 3 slots are unfilled after the first 3 insertions?
 A (97×97×97)/1003 B (99×98×97)/1003 C (97×96×95)/1003 D (97×96×95)/(3!×1003)
Algorithms       Hashing       Gate 2014 Set -03
Question 47 Explanation:
Given Hash table consists of 100 slots.
They are picked with equal probability of Hash function since it is uniform hashing.
So to avoid the first 3 slots to be unfilled
=97/100*97/100*97/100=((97*97*97))⁄1003
 Question 48
Consider the pseudocode given below. The function DoSomething() takes as argument a pointer to the root of an arbitrary tree represented by the leftMostChild-rightSibling representation. Each node of the tree is of type treeNode. typedef struct treeNode* treeptr; struct treeNode { treeptr leftMostChild, rightSibling; }; int DoSomething (treeptr tree) { int value=0; if (tree != NULL) { if (tree->leftMostChild == NULL) value = 1; else value = DoSomething(tree->leftMostChild); value = value + DoSomething(tree->rightSibling); } return(value); } When the pointer to the root of a tree is passed as the argument to DoSomething, the value returned by the function corresponds to the
 A number of internal nodes in the tree. B height of the tree. C number of nodes without a right sibling in the tree. D number of leaf nodes in the tree.
Algorithms       Tree Traversals       Gate 2014 Set -03
Question 48 Explanation:
The key to solving such questions is to understand or detect where/by what condition the value (or the counter) is getting incremented each time.
Here, that condition is if (tree → leftMostchild = = Null)
⇒ Which means if there is no left most child of the tree (or the sub-tree or the current node as called in recursion)
⇒ Which means there is no child to that particular node (since if there is no left most child, there is no child at all).
⇒ Which means the node under consideration is a leaf node.
⇒ The function recursively counts, and adds to value, whenever a leaf node is encountered. ⇒ The function returns the number of leaf nodes in the tree.
 Question 49
Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order. int ProcessArray(int *listA, int x, int n) { int i, j, k; i = 0; = n-1; do { k = (i+j)/2; if (x <= listA[k]) j = k-1; if (listA[k] <= x) i = k+1; }while (i <= j); if (listA[k] == x) return(k); else return -1; } Which one of the following statements about the function ProcessArray is CORRECT?
 A It will run into an infinite loop when x is not in listA. B It is an implementation of binary search. C It will always find the maximum element in listA. D It will return −1 even when x is present in listA.
Algorithms       Searching       Gate 2014 Set -03
Question 49 Explanation:
From the above code, we can identify
k = (i+j)/2;
where k keeps track of current middle element & i, j keeps track of left & right children of current subarray.
So it is an implementation of Binary search.
 Question 50
Which one of the following is the tightest upper bound that represents the number of swaps required to sort n numbers using selection sort?
 A O(log n) B O(n) C O(n log n) D O(n2)
Algorithms       Sorting       Gate 2013
Question 50 Explanation:
Best, Average and worst case will take maximum O(n) swaps.
Selection sort time complexity O(n2) in terms of number of comparisons. Each of these scans requires one swap for n-1 elements (the final element is already in place).
 Question 51
Which one of the following is the tightest upper bound that represents the time complexity of inserting an object into a binary search tree of n nodes?
 A O(1) B O(log n) C O(n) D O(n log n)
Algorithms       Sorting       Gate 2013
Question 51 Explanation:
For skewed binary search tree on n nodes, the tightest upper bound to insert a node is O(n).
 Question 52
Consider the languages L1 = ϕ and L= {a}. Which one of the following represents L1L2* ∪ L1*?
 A {є} B ϕ C a* D {є,a}
Algorithms       Regular languages and Finite automata       Gate 2013
Question 52 Explanation:
As we know, for any regular expression R,
Rϕ = ϕR=ϕ
So L1 L2 * = ϕ
and L1 * = {ϕ}* = {ϵ}
So L1L2* ∪ L1* = {ϵ}
 Question 53
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)
Algorithms       Sorting       Gate 2013
Question 53 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 54
Consider an undirected random graph of eight vertices. The probability that there is an edge between a pair of vertices is 1/2. What is the expected number of unordered cycles of length three?
 A 1/8 B 1 C 7 D 8
Algorithms       Graph-Theory       Gate 2013
Question 54 Explanation:
Among available ‘8’ vertices, we need to identify the cycles of length ‘3’. The probability that there exists one edge between two vertices = 1/2 So, the total probability that all three edges of the above exists
= 1/2 × 1/2 × 1/2 (as they are independent events)
= 1/8
Total number of ways in which we can select ‘3’ such vertices among ‘8’ vertices = 8C3 = 56
Total number of cycles of length ‘3’ out of 8 vertices = 56 × 1/8 = 7
 Question 55
Which of the following statements is/are TRUE for undirected graphs?
```P: Number of odd degree vertices is even.
Q: Sum of degrees of all vertices is even.```
 A P only B Q only C Both P and Q D Neither P nor Q
Algorithms       Graph-Theory       Gate 2013
Question 55 Explanation:
Euler’s Theorem 3:
The sum of the degrees of all the vertices of a graph is an even number (exactly twice the number of edges).
In every graph, the number of vertices of odd degree must be even.
 Question 56
The line graph L(G) of a simple graph G is defined as follows: · There is exactly one vertex v(e) in L(G) for each edge e in G. · For any two edges e and e' in G, L(G) has an edge between v(e) and v(e'), if and only if e and e'are incident with the same vertex in G. Which of the following statements is/are TRUE?
```(P) The line graph of a cycle is a cycle.
(Q) The line graph of a clique is a clique.
(R) The line graph of a planar graph is planar.
(S) The line graph of a tree is a tree.```

 A P only B P and R only C R only D P, Q and S only
Algorithms       Graph-Theory       Gate 2013
Question 56 Explanation:
P) True. Because every edge in cycle graph will become a vertex in new graph L(G) and every vertex of cycle graph will become an edge in new graph.
R) False. We can give counter example. Let G has 5 vertices and 9 edges which is planar graph. Assume degree of one vertex is 2 and of all others are 4. Now, L(G) has 9 vertices (because G has 9 edges) and 25 edges. But for a graph to be planar,
|E| ≤ 3|v| - 6
For 9 vertices |E| ≤ 3 × 9 - 6
⇒ |E| ≤ 27 - 6
⇒ |E| ≤ 21. But L(G) has 25 edges and so is not planar.
As (R) is false, option (B) & (C) are eliminated.
 Question 57
The number of elements that can be sorted in Θ(log n) time using heap sort is
 A Θ(1) B Θ(√log⁡n) C Θ (log⁡n/log⁡log⁡n) D Θ(log n)
Algorithms       Sorting       Gate 2013
Question 57 Explanation:
Using heap sort to n elements it will take O(n log n) time. Assume there are log n/ log log n elements in the heap.
So, Θ((logn/ log log n)log(logn/log log n))
= Θ(logn/log logn (log logn - log log logn))
= Θ((log n/log logn) × log log n)
= Θ (log n)
Hence, option (C) is correct answer.
 Question 58
What is the return value of f(p,p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
 `int` `f(``int` `&x, ``int` `c) {` `   ``c = c - 1;` `   ``if` `(c==0) ``return` `1;` `   ``x = x + 1;` `   ``return` `f(x,c) * x;` `}`
 A 3024 B 6561 C 55440 D 161051
Algorithms       Pass-by-value       Gate 2013
Question 58 Explanation:
Since it is f(p,p)
f(5,5)→f(x,4)*x
f(x,3)*x*x
f(x,2)*x*x*x ⇒x*x*x*x=x4
Now x=x+1, there are 4 different calls namely f(6,4),f(7,3),f(8,2),f(9,1)
Because of pass by reference, x in all earlier functions is replaced with 9.
⇒ 9*9*9*9=94=6561
 Question 59
The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?
 A 10, 20, 15, 23, 25, 35, 42, 39, 30 B 15, 10, 25, 23, 20, 42, 35, 39, 30 C 15, 20, 10, 23, 25, 42, 35, 39, 30 D 15, 10, 23, 25, 20, 35, 42, 39, 30
Algorithms       Tree Traversals       Gate 2013
Question 59 Explanation: From the data, The Postorder traversal sequence is:
→ 15, 10, 23, 25, 20, 35, 42, 39, 30.
 Question 60
Consider the following operation along with Enqueue and Dequeue operations on queues, where k is a global parameter.
```MultiDequeue(Q){
m = k
while (Q is not empty and m  > 0) {
Dequeue(Q)
m = m - 1
}
}```
What is the worst case time complexity of a sequence of n MultiDequeue() operations on an initially empty queue? (GATE CS 2013)
 A Θ(n) B Θ(n + k) C Θ(nk) D Θ(n2)
Algorithms       Time-Complexity       Gate 2013
Question 60 Explanation:
There are three possible operations on queue- Enqueue, Dequeue and MultiDequeue. MultiDequeue is calling Dequeue multiple times based on a global variable k. Since, the queue is initially empty, whatever be the order of these operations, there cannot be more no. of Dequeue operations than Enqueue operations. Hence, the total no. operations will be n only.
 Question 61
Assuming P ≠ NP, which of the following is TRUE?
 A NP-complete = NP B NP-complete ∩ P = ∅ C NP-hard = NP D P = NP-complete
Algorithms       P-NP       Gate 2012
Question 61 Explanation:
Note: Out of syllabus.
The definition of NP-complete is,
A decision problem p is NP-complete if:
1. p is in NP, and
2. Every problem in NP is reducible to p in polynomial time.
It is given that assume P ≠ NP , hence NP-complete ∩ P = ∅ .
This is due to the fact that, if NP-complete ∩ P ≠ ∅ i.e. there are some problem (lets say problem P1) which is in P and in NP-complete also, then it means that P1 (NP-complete problem) can be solved in polynomial time also (as it is also in P class) and this implies that every NP problem can be solve in polynomial time, as we can convert every NP problem into NP-complete problem in polynomial time.
Which means that we can convert every NP problem to P1 and solve in polynomial time and hence P = NP, which is contradiction to the given assumption that P ≠ NP.
 Question 62
Let W(n) and A(n) denote respectively, the worst case and average case running time of an algorithm executed on an input of size n.  Which of the following is ALWAYS TRUE?
 A A(n) = Ω (W(n)) B A(n) = Θ (W(n)) C A(n) = O (W(n)) D A(n) = o (W(n))
Algorithms       Time-Complexity       Gate 2012
Question 62 Explanation:
The average case time can be lesser than or even equal to the worst case.
So, A(n) would be upper bounded by W(n) and it will not be strict upper bound as it can even be same (e.g. Bubble Sort and merge sort).
A(n)=O(W(n))
Note: Option A is wrong because A(n) is not equal to Ω(w(n)) .
 Question 63
Consider the Quicksort algorithm. Suppose there is a procedure for finding a pivot element which splits the list into two sub-lists each of which contains at least one-fifth of the elements. Let T(n) be the number of comparisons required to sort n elements. Then
 A T(n) ≤ 2T(n/5) + n B T(n) ≤ T(n/5) + T(4n/5) + n C T(n) ≤ 2T(4n/5) + n D T(n) ≤ 2T(n/2) + n
Algorithms       Sorting       Gate-2008
Question 63 Explanation:
Consider the case where one subset of set of n elements contains n/5 elements and another subset of set contains 4n/5 elements.
So, T(n/5) comparisons are needed for the first subset and T(4n/5) comparisons needed for second subset.
Now, suppose that one subset contains more than n/5 elements then another subset will contain less than 4n/5 elements. Due to which time complexity will be less than
T(n/5) + T(4n/5) + n
Because recursion tree will be more balanced.
 Question 64
The subset-sum problem is defined as follows: Given a set S of n positive integers and a positive integer W, determine whether there is a subset of S Whose elements sum to An algorithm Q solves this problem in O(nW) time. Which of the following statements is false?
 A Q solves the subset-sum problem in polynomial time when the input is encoded in unary B Q solves the subset-sum problem in polynomial time when the input is encoded in binary C The subset sum problem belongs to the class NP D The subset sum problem is NP-hard
Algorithms       P-NP       Gate-2008
Question 64 Explanation:
Note: Out of syllabus.
 Question 65
Dijkstra’s single source shortest path algorithm when run from vertex a in the above graph, computes the correct shortest path distance to A only vertex a B only vertices a, e, f, g, h C only vertices a, b, c, d D all the vertices
Algorithms       Shortest-Path       Gate-2008
Question 65 Explanation:
The single source shortest path algorithm(Dijkstra’s) is not work correctly for negative edge weights and negative weight cycle. But as per the above graph it works correct. It gives from shortest path between ‘a’ vertex to all other vertex. The Dijkstra’s algorithm will follow greedy strategy.
A-B ⇒ 1
A-C⇒ 1+2=3
A-E⇒ 1-3 =-2
A-F⇒ 1 -3 +2 =0
A-G⇒ 1 -3 +2 +3 =3
A-C ⇒1 +2 =3
A-H⇒ 1+2 -5 =-2
 Question 66
The numbers 1, 2, .... n are inserted in a binary search tree in some order. In the resulting tree, the right subtree of the root contains p nodes. The first number to be inserted in the tree must be
 A p B p + 1 C n - p D n - p + 1
Algorithms       Tree Traversals       Gate 2005-IT
Question 66 Explanation:
Total element = n
RST contains elements = p
Root contains = 1 element
1st contains = n - (p + 1) element Root contains the value is n - p.
 Question 67
In a depth-first traversal of a graph G with n vertices, k edges are marked as tree edges. The number of connected components in G is
 A k B k + 1 C n - k - 1 D n - k
Algorithms       Graph-Traversal       Gate 2005-IT
Question 67 Explanation:
In a graph G with n vertices and p component then G has n - p edges(k).
In this question, we are going to applying the depth first search traversal on each component of graph where G is conected (or) disconnected which gives minimum spanning tree
i.e., k = n-p
p = n - k
 Question 68
In the following table, the left column contains the names of standard graph algorithms and the right column contains the time complexities of the algorithms. Match each algorithm with its time complexity.
 1. Bellman-Ford algorithm 2. Kruskal’s algorithm 3. Floyd-Warshall algorithm 4. Topological sorting A : O ( m log n) B : O (n3) C : O (nm) D : O (n + m)
 A 1→ C, 2 → A, 3 → B, 4 → D B 1→ B, 2 → D, 3 → C, 4 → A C 1→ C, 2 → D, 3 → A, 4 → B D 1→ B, 2 → A, 3 → C, 4 → D
Algorithms       General       Gate 2005-IT
Question 68 Explanation:
Bellman-ford algorithm → O(nm)
Krushkal's algorithm → O(m log n)
Floyd-Warshall algorithm → O(n3)
Topological sorting → O(n+m)
 Question 69

A hash table contains 10 buckets and uses linear probing to resolve collisions. The key values are integers and the hash function used is key % 10. If the values 43, 165, 62, 123, 142 are inserted in the table, in what location would the key value 142 be inserted?

 A 2 B 3 C 4 D 6
Algorithms       Sorting-and-Searching       Gate 2005-IT
Question 69 Explanation:
43%10 = 3 [occupy 3]
165%10 = 5 [occupy 5]
62%10 = 2 [occupy 2]
123%10 = 3 [3 already occupied, so occupies 4]
142%10 = 2 [2, 3, 4, 5 are occupied, so it occupies 6]
 Question 70

In a binary tree, for every node the difference between the number of nodes in the left and right subtrees is at most 2. If the height of the tree is h > 0, then the minimum number of nodes in the tree is:

 A 2h-1 B 2h-1 + 1 C 2h - 1 D 2h
Algorithms       Tree Traversals       Gate 2005-IT
Question 70 Explanation:
Let's take an example, Above tree satisfies the property given in the question. Hence, only option (B) satisfies it.
 Question 71
Let T(n) be a function defined by the recurrence T(n) = 2T(n/2) + √n for n ≥ 2 and T(1) = 1 Which of the following statements is TRUE?
 A T(n) = θ(log n) B T(n) = θ(√n) C T(n) = θ(n) D T(n) = θ(n log n)
Algorithms       Recursion       Gate 2005-IT
Question 71 Explanation:
Apply Master's theorem.
 Question 72

Let G be a weighted undirected graph and e be an edge with maximum weight in G. Suppose there is a minimum weight spanning tree in G containing the edge e. Which of the following statements is always TRUE?

 A There exists a cutset in G having all edges of maximum weight B There exists a cycle in G having all edges of maximum weight C Edge e cannot be contained in a cycle D All edges in G have the same weight
Algorithms       Graph-Theory       Gate 2005-IT
Question 72 Explanation:
(A) True, because if there is heaviest edge in MST, then there exist a cut with all edges with weight equal to heaviest edge. (B) False, because the cutset of heaviest edge may contain only one edge.
(C) False. The cutset may form cycle with other edge.
(D) False. Not always true.
 Question 73

A binary search tree contains the numbers 1, 2, 3, 4, 5, 6, 7, 8. When the tree is traversed in pre-order and the values in each node printed out, the sequence of values obtained is 5, 3, 1, 2, 4, 6, 8, 7. If the tree is traversed in post-order, the sequence obtained would be

 A 8, 7, 6, 5, 4, 3, 2, 1 B 1, 2, 3, 4, 8, 7, 6, 5 C 2, 1, 4, 3, 6, 7, 8, 5 D 2, 1, 4, 3, 7, 8, 6, 5
Algorithms       Tree Traversals       Gate 2005-IT
Question 73 Explanation:
Pre-order is given as
5, 3, 1, 2, 4, 6, 8, 7
In-order is the sorted sequence, i.e.,
1, 2, 3, 4, 5, 6, 7, 8
And we know that with given inorder and preorder, we can draw a unique BST.
So, the BST will be, Hence, postorder traversasl sequence is,
2, 1, 4, 3, 7, 8, 6, 5
 Question 74
Let G be a directed graph whose vertex set is the set of numbers from 1 to 100. There is an edge from a vertex i to a vertex j iff either j = i + 1 or j = 3i. The minimum number of edges in a path in G from vertex 1 to vertex 100 is
 A 4 B 7 C 23 D 99
Algorithms       Graph-Theory       Gate 2005-IT
Question 74 Explanation:
Edge set consists of edges from i to j, using either
j = i +1
(or)
j = 3i
Second option will help us reach from 1 to 100 rapidly. The trick to solve this question is to think in reverse way. Instead of finding a path from 1 to 100, try to find a path from 100 to 1.
So, the edge sequence with minimum number of edges is
1 → 3 → 9 → 10 → 11 → 33 → 99 → 100
which consists of 7 edges.
 Question 75
To carry out white box testing of a program, its flow chart representation is obtained as shown in the figure below: For basis path based testing of this program, its cyclomatic complexity is
 A 5 B 4 C 3 D 2
Algorithms       Cyclomatic-Complexity       Gate 2005-IT
Question 75 Explanation:
Note: Out of syllabus.
 Question 76

An array of integers of size n can be converted into a heap by adjusting the heaps rooted at each internal node of the complete binary tree starting at the node ⌊(n - 1) /2⌋, and doing this adjustment up to the root node (root node is at index 0) in the order ⌊(n - 1)/2⌋, ⌊(n - 3)/2⌋, ....., 0. The time required to construct a heap in this manner is

 A O(log n) B O(n) C O(n log log n) D O(n log n)
Algorithms       Binary-Trees       Gate 2004-IT
Question 76 Explanation:
The above statement is actually algorithm for building a heap of an input array A.
And we know that time complexity for building the heap is O(n)
 Question 77
Which one of the following binary trees has its inorder and preorder traversals as BCAD  and ABCD, respectively?
 A B C D Algorithms       Tree Traversals       Gate 2004-IT
Question 77 Explanation:
Inorder traversal is,
Left root right.
Preorder traversal is,
Root left right.
 Question 78
Let f(n), g(n) and h(n) be functions defined for positive inter such that f(n) = O(g(n)), g(n) ≠ O(f(n)), g(n) = O(h(n)), and h(n) = O(g(n)). Which one of the following statements is FALSE?
 A f(n) + g(n) = O(h(n)) + h(n)) B f(n) = O(h(n)) C fh(n) ≠ O(f(n)) D f(n)h(n) ≠ O(g(n)h(n))
Algorithms       Time-Complexity       Gate 2004-IT
Question 78 Explanation:
f(n) = O(h(n)) [Using transitivity]
So, f(n)g(n) = O(g(n)h(n)) is True.
Hence, (D) is false.
 Question 79
Consider the undirected graph below: Using Prim's algorithm to construct a minimum spanning tree starting with node A, which one of the following sequences of edges represents a possible order in which the edges would be added to construct the minimum spanning tree?
 A (E, G), (C, F), (F, G), (A, D), (A, B), (A, C) B (A, D), (A, B), (A, C), (C, F), (G, E), (F, G) C (A, B), (A, D), (D, F), (F, G), (G, E), (F, C) D (A, D), (A, B), (D, F), (F, C), (F, G), (G, E)
Algorithms       Greedy-Method       Gate 2004-IT
Question 79 Explanation:
(A) and (B) produce disconnected components with the given order in options which is never allowed by Prim's algorithm.
(C) produces connected component every instead a new edge is added but when first vertex is chosen (first vertex is chosen randomly) first edge must be minimum weight edge that is chosen. Therefore, (A, D) must be chosen before (A, B). Therefore (C) is false.
 Question 80
Consider a list of recursive algorithms and a list of recurrence relations as shown below. Each recurrence relation corresponds to exactly one algorithm and is used to derive the time complexity of the algorithm.
RECURSIVE ALGORITHM RECURRENCE RELATION
P. Binary search I. T(n) = T(n-k) + T(k) + cn
Q. Merge sort II. T(n) = 2T(n-1) + 1
R. Quick sort III. T(n) = 2T(n/2) + cn
S. Tower of Hanoi IV. T(n) = T(n/2) + 1
 A P-II, Q-III, R-IV, S-I B P-IV, Q-III, R-I, S-II C P-III, Q-II, R-IV, S-I D P-IV, Q-II, R-I, S-III
Algorithms       General       Gate 2004-IT
Question 80 Explanation:
Quick sort - T(n) = T(n-k) + T(k) + cn
Binary search - T(n/2) + 1
Merge sort - T(n) = 2T(n/2)+ cn
Tower of hanoi - T(n) = 2T(n-1) +1
 Question 81
Merge sort uses
 A Divide and conquer strategy B Backtracking approach C Heuristic search D Heuristic search
Algorithms       Sorting       Gate-1995
Question 81 Explanation:
Merge sort uses the divide and conquer strategy.
 Question 82
For merging two sorted lists of sizes m and n into a sorted list of size m+n, we required comparisons of
 A O(m) B O(n) C O(m+n) D O(logm+logn)
Algorithms       Sorting       Gate-1995
Question 82 Explanation:
In best case, no. of comparisons is Min(m,n).
In worst case, no. of comparisons is m+n-1.
Then we require O(m+n) comparisons to merging two sorted lists.
 Question 83
 A AB + CD + *F/D + E* B ABCD + *F/DE*++ C A *B + CD/F *DE++ D A + *BCD/F* DE++ E None of the above
Algorithms       Post-fix-and-Prefix       Gate-1995
Question 83 Explanation:
The postfix expression will be,
A B C D + * F / + D E * +
 Question 84
 A I and II B II and III C I and IV D I and III
Algorithms       General       Gate-1995
Question 84 Explanation:
Binary search using linked list is not efficient as it will not give O(log n), because we will not be able to find the mid in constant time. Fnding mid in linked list takes O(n) time.
Recursive program requires stack for storing the function state. Any recursive program can be rewritten as an iterative one. Advantage of recursion is "less programming effort", while it always lags behind ietrative one in terms of performance.
 Question 85
FORTRAN implementation do not permit recursion because
 A they use static allocation for variables B they use dynamic allocation for variables C stacks are not available on all machines D it is not possible to implement recursion on all machines
Algorithms       Recursions       Gate-1994
Question 85 Explanation:
FORTRAN implementation do not permit recursion because they use the static allocation for variables.
→ Recursion requires dynamic allocation of data.
 Question 86
The recurrence relation that arises in relation with the complexity of binary search is:
 A B C D Algorithms       Time-Complexity       Gate-1994
Question 86 Explanation:
In binary search, search for the half of the list and constant time for comparing. So, Question 87
Which of the following algorithm design techniques is used in the quicksort algorithm?
 A Dynamic programming B Backtracking C Divide and conquer D Greedy method
Algorithms       Quick-Sort       Gate-1994
Question 87 Explanation:
In quick sort, we use divide and conquer technique.
 Question 88
In which one of the following cases is it possible to obtain different results for call-by reference and call-by-name parameter passing methods?
 A Passing a constant value as a parameter B Passing the address of an array as a parameter C Passing an array element as a parameter D Passing an array following statements is true
Algorithms       Call-by-reference-and-Call-by-value       Gate-1994
Question 88 Explanation:
Passing an array element as a parameter then it gives different output values for the call-by-reference and call-by-name parameters.
{ ........
a[ ] = {1, 2, 3, 4}
i = 0
fun(a[i]);
print a;
}
fun(int x)
{
int i = 1;
x = 8;
}
O/p:
Call-by-reference = 8
Call-by-value = 1
 Question 89
Which one of the following statements is false?
 A Optimal binary search tree construction can be performed efficiently using dynamic programming. B Breadth-first search cannot be used to find converted components of a graph. C Given the prefix and postfix walks over a binary tree, the binary tree cannot be uniquely constructed. D Depth-first search can be used to find connected components of a graph.
Algorithms       Searching       Gate-1994
Question 89 Explanation:
In BFS algorithm, we can randomly select a source vertex and then run, after that whether we need to check distance to each and every vertex from source is still infinite (or) not. If we find any vertex having infinite distance then the graph is not connected.
 Question 90
 A g1(n) is O(g2(n)) B g1 (n) is O(3) C g2 (n) is O(g1 (n)) D g2 (n) is O(n) E Both A and B
Algorithms       Asymptotic-Notations       Gate-1994
Question 90 Explanation:
In asymptotic complexity, we assume sufficiently large n. So, g1(n) = n2 and g2(n) = n3.
Growth rate of g1 is less than that of g2 i.e., g1(n) = O(g2(n)) = O(n).
 Question 91
The root directory of a disk should be placed
 A at a fixed address in main memory B at a fixed location on the disk C anywhere on the disk D at a fixed location on the system disk E anywhere on the system disk
Algorithms       Gate-1993
Question 91 Explanation:
Root directory can points to the various user directories. Then they will be stored in a way that user can't be easily modify them. Then they should be at fixed location on the disk.
 Question 92
Consider a simple connected graph G with n vertices and n-edges (n>2). Then, which of the following statements are true?
 A G has no cycles. B The graph obtained by removing any edge from G is not connected. C G has at least one cycle. D The graph obtained by removing any two edges from G is not connected. E Both C and D.
Algorithms       Graphs       Gate-1993
Question 92 Explanation:
If a graph have n vertices and n edges (n>2) then it is to be cyclic graph. Then it have atleast one cycle and if we remove two edges then it is not connected.
For example let us consider, n=3
 Question 93
 A O(n) B O(n2) C O(n3) D O(3n2) E O(1.5n2) F B, C, D and E
Algorithms       Time-Complexity       Gate-1993
Question 93 Explanation: ⇒ In this 'n' is constant. So, n is added to n times itself which is O(n2).
Hence, (a) is wrong. And rest (B), (C), (D), (E) are correct.
 Question 94
Complexity of Kruskal’s algorithm for finding the minimum spanning tree of an undirected graph containing n vertices and m edges if the edges are sorted is __________
 A O(m log n)
Algorithms       Krushkal's-Algorithm       Gate-1992
Question 94 Explanation:
Though the edges are to be sorted still due to union find operation complexity is O(m log n).
 Question 95
Following algorithm(s) can be used to sort n integers in the range [1...n3] in O(n) time
 A Heapsort B Quicksort C Mergesort D Radixsort
Algorithms       Sorting       Gate-1992
Question 95 Explanation:
As no comparison based sort can ever do any better than nlogn. So option (A), (B), (C) are eliminated. O(nlogn) is lower bound for comparison based sorting.
As Radix sort is not comparison based sort (it is counting sort), so can be done in linear time.
 Question 96

Assume that the last element of the set is used as partition element in Quicksort. If n distinct elements from the set [1…..n] are to be sorted, give an input for which Quicksort takes maximum time.

 A O(n^2)
Algorithms       Quick-Sort       Gate-1992
Question 96 Explanation:
For n distinct elements the algorithm will take maximum time when:
→ The array is already sorted in ascending order.
→ The array is already sorted in descending order.
 Question 97
The minimum number of comparisons required to sort 5 elements is _____
 A 7
Algorithms       Sorting       Gate-1991
Question 97 Explanation:
Minimum no. of comparisons
= ⌈log(n!)⌉
= ⌈log(5!)⌉
= ⌈log(120)⌉
= 7
 Question 98
 A 144
Algorithms       Binary-Trees       Gate-1991
Question 98 Explanation: Question 99
 A 4, 1, 6, 7, 3, 2, 5, 8
Algorithms       Tree Traversals       Gate-1991
Question 99 Explanation:
Inorder traversal is
(Left, Root, Right)
So, the order will be
4, 1, 6, 7, 3, 2, 5, 8
 Question 101
 A O(n2) B O(mn) C O(m+n) D O(m logn) E O(m2) F B, D and E
Algorithms       Time-Complexity       Gate-1991
Question 101 Explanation:
Though the edges are sorted still due to finding union operation complexity is O(m log n).
→ Where m is no. of edges and n is number of vertices then n = O(m2)
→ O(m logn) < O(mn)
 Question 102
 A 20, 10, 20, 10, 20 B 20, 20, 10, 10, 20 C 10, 20, 20, 10, 20 D 20, 20, 10, 20, 10
Algorithms       Stacks       Gate-1991
Question 102 Explanation:
PUSH(10), PUSH(20), POP = 20 (i)
→ PUSH(10), PUSH(10), PUSH(20), POP = 20 (ii)
→ PUSH(10), PUSH(10), POP = 10 (iii)
→ PUSH(10), POP = 10 (iv)
→ PUSH(20)
→ PUSH(20), POP = 20 (v)
20, 20, 10, 10, 20
 Question 103
 A (a) - (r), (b) - (p), (c) - (s), (d) - (q)
Algorithms       Match-the-Following       Gate-1990
Question 103 Explanation:
Strassen's matrix multiplication algorithm - Divide and Conquer
Kruskal's minimum spanning tree algorithm - Greedy method
Biconnected components algorithm - Depth first search
Floyd's shortest path algorithm - Dynamic programming
There are 103 questions to complete.