Test 1 Algorithms | Computer Science

Description: GATE Previous year Topic Wise Questions and Answers | Algorithms
Number of Questions: 20
Created by:
Tags: GATE CS Algorithms
Attempted 0/20 Correct 0 Score 0

In a heap with n elements with the smallest element at the root, the 7th smallest element can be found in time

  1. O(n log n)

  2. O(n)

  3. O(log n)

  4. O(1)


Correct Option: C
Explanation:

Here we can follow simple procedure, we can rum heap sort for 7 iterations. In each iteration the top most element is smallest, we note & then replace it with the last element, then we run min heapify algorithm, which brings next smallest element on top. This procedure take 0 (log n) time. We need to run it for 7 times. So tight bound O(7 log n) = O(log n)

Consider the following three claims I. (n + k)m = O (nm) where k and m are constants II. 2n+1 = O(2n) III. 22n+1 = O(2n)

Which of these claims are correct?

  1. I and II

  2. I and III

  3. II and III

  4. I, II, and III


Correct Option: A
Explanation:

(1) $(n + k)^m$ if we exapnd it. It is $(n^m + .....)$ During tight bound $\theta (n^m)$ correct (2) $ 2^{n+1} = 2.2^n \Rightarrow O(2.2^n) \Rightarrow O(2^n) \quad correct $

$ 2^{2n+1} = 2.2^2n \Rightarrow O(2.2^2n) $ $ O(2^{2^n}) \neq O(2^n) \quad incorrect $

The usual O(n2) implementation of Insertion Sort to sort an array uses linear search to identify the position where an element is to be inserted into the already sorted part of the array. If, instead, we use binary search to identify the position, the worst case running time will

  1. remain O (n2)

  2. become O (n (log n)2)

  3. become O (n log n)

  4. become O (n)


Correct Option: C
Explanation:

Binary search is efficient when the sorted sequence is there, but the worst case scenario for insertion sort would not be sorted sequence so even using binary search instead of linear search the complexity of comparisons will remain O (n2)

Consider the following graph

Among the following sequences I a b e g h f II a b f e h g III a b f h g e IV a f g h b e

Which are depth first traversals of the above graph?

  1. I, II and IV only

  2. I and IV only

  3. II, III and IV only

  4. I, III and IV only


Correct Option: D
Explanation:

In a permutation a1 ... an, of n distinct integers, an inversion is a pair (ai, aj) such that i < j and ai > aj.

If all permutations are equally likely, what is the expected number of inversions in a randomly chosen permutation of 1. . . n?

  1. $\frac{n(n-1)}{2}$

  2. $\frac{n(n-1)}{4}$

  3. $\frac{n(n+1)}{4}$

  4. $2n[\log_2n]$


Correct Option: B
Explanation:

Let $a_1....a_n$ be 1.....3 here n =3 consider all permutation 123, 132, 231,213, 312, 321. Let us consider 312 here the inversions are {(3,1), (3,2)} So in a randomly chosen permutation we need to select two no. following inversion property on an average $no.\ of\ inverstions\ \dfrac{1}{2}\ nC_2$ $$ = \dfrac{1}{2} \dfrac{n!}{2!(n-2)!} \Rightarrow \dfrac{1}{2}\dfrac{n(n-1)}{2}$$ $$ = \dfrac{n(n-1)}{4}$$

if $n=3 \Rightarrow \dfrac{3(3-1)}{4} = [1.5] \cong 2$

Solving this question taking a random example would be much easier

In a permutation a1 ... an, of n distinct integers, an inversion is a pair (ai, aj) such that i < j and ai > aj.

What would be the worst case time complexity of the Insertion Sort algorithm, if the inputs are restricted to permutations of 1. . . n with at most n inversions?

  1. O(n2)

  2. O (n log n)

  3. O (n1.5)

  4. O (n)


Correct Option: B
Explanation:

Let $G= (V,E)$ be a directed graph with $n$ vertices. A path from $v_i$ to $v_j$ in $G$ is a sequence of vertices ($v_{i},v_{i+1}, \dots , v_j$) such that $(v_k, v_k+1) \in E$ for all $k$ in $i$ through $j-1$. A simple path is a path in which no vertex appears more than once.

Let $A$ be an $n \times n$ array initialized as follows.

$$A[j,k] = \begin{cases} 1 \text { if } (j,k) \in E \\ 0 \text{ otherwise} \end{cases}$$

Consider the following algorithm.

for i=1 to n
    for j=1 to n
        for k=1 to n
            A[j,k] = max(A[j,k], A[j,i] + A[i,k]);  

Which of the following statements is necessarily true for all j and k after termination of the above algorithm?

  1. A[j,k] $\le$ n

  2. If A[j,j] $\ge$ n - 1, then G has a Hamiltonian cycle

  3. If there exists a path from j to k, A[j,k] contains the longest path length from j to k

  4. If there exists a path from j to k, every simple path from j to k contains at most A[j,k} edges


Correct Option: B
Explanation:

What is the weight of a minimum spanning tree of the following graph?

GATE 2003 Paper

  1. 29

  2. 31

  3. 38

  4. 41


Correct Option: B
Explanation:

$\text{Sum}\ 1+2+3+2+8+4+2+4+5=31$

Let G = (V,E) be an undirected graph with a subgraph G1 = (V1, E1). Weights are assigned to edges of G as follows.

$$G= \begin{cases} 0 \text { if } e \in E_1 \\ 1 \text{ otherwise} \end{cases}$$

A single-source shortest path algorithm is executed on the weighted graph (V,E,w) with an arbitrary vertex v1 of V1 as the source. Which of the following can always be inferred from the path costs computed?

  1. The number of edges in the shortest paths from v1 to all vertices of G

  2. G1 is connected

  3. V1 forms a clique in G

  4. G1 is a tree


Correct Option: B
Explanation:

The following are the starting and ending times of activities A, B, C, D, E, F, G and H respectively in chronological order: “as bs cs ae ds ce es fs be de gs ee fe hs ge he”. Here, xs denotes the starting time and xe denotes the ending time of activity X. We need to schedule the activities in a set of rooms available to us. An activity can be scheduled in a room only if the room is reserved for the activity for its entire duration. What is the minimum number of rooms required?

  1. 3

  2. 4

  3. 5

  4. 6


Correct Option: B
Explanation:

Consider the polynomial p(x) = a0 + a1x + a2x^2 +a3x^3, where ai != 0, for all i. The minimum number of multiplications needed to evaluate p on an input x is :

  1. 3

  2. 4

  3. 6

  4. 9


Correct Option: A
Explanation:

In a binary max heap containing n numbers, the smallest element can be found in time

  1. O (n)

  2. O (log n)

  3. O (log log n)

  4. O (1)


Correct Option: A
Explanation:

To implement Dijkstra's shortest path algorithm on un weighted graphs so that it runs in linear time, the data structure to be used is:

  1. Queue

  2. Stack

  3. Heap

  4. B-Tree


Correct Option: C
Explanation:

Which one of the following in place sorting algorithms needs the minimum number of swaps?

  1. Quick sort

  2. Insertion sort

  3. Selection sort

  4. Heap sort


Correct Option: D
Explanation:

Consider a weighted complete graph G on the vertex set {v1, v2,............., vn} such that the weight of the edge (vi, vj) is 2 |i - j| . The weight of a minimum spanning tree of G is:

  1. n − 1

  2. 2n − 2

  3. $\begin{pmatrix} n \\ 2 \end{pmatrix}$

  4. n2


Correct Option: B
Explanation:

A scheme for storing binary trees in an array X is as follows. Indexing of X starts at 1 instead of 0. the root is stored at X[1]. For a node stored at X [i], the left child, if any, is stored in X [2i] and the right child, if any, in X [2i+1]. To be able to store any binary tree on n vertices the minimum size of X should be

  1. log2 n

  2. n

  3. 2n + 1

  4. 2n - 1


Correct Option: B
Explanation:

An element in an array X is called a leader if it is greater than all elements to the right of it in X. The best algorithm to find all leaders in an array

  1. Solves it in linear time using a left to right pass of the array

  2. Solves it in linear time using a right to left pass of the array

  3. Solves it using divide and conquer in time O(n log n)

  4. Solves it in time O(n2)


Correct Option: C
Explanation:

Consider the following C-program fragment in which i, j and n are integer variables.
for( i = n, j = 0; i &gt; 0; i /= 2, j +=i ); Let val ( j ) denote the value stored in the variable j after termination of the for loop. Which one of the following is true?

  1. $val(j)=\Theta(\log n)$

  2. $val(j)=\Theta (\sqrt{n})$

  3. $val(j)=\Theta( n)$

  4. $val(j)=\Theta (n\log n)$


Correct Option: C
Explanation:

$j = n/2+n/4+n/2+\ldots +1$

number of iteration will be $2^k = n$ or $k = \log n$

this is in GP find sum till $\log n $,= $\Theta(n)$

Consider the following graph:

Which one of the following cannot be the sequence of edges added, in that order, to a minimum spanning tree using Kruskal's algorithm?

  1. (a − b), (d − f ) , (b − f ), (d − c) , (d − e)

  2. (a − b), (d − f ) , (d − c) , (b − f ) , (d − e)

  3. (d − f ), (a − b) , (d − c) , (b − f ) , (d − e)

  4. (d − f ), (a − b) , (b − f ), (d − e) , (d − c)


Correct Option: D
Explanation:

The cube root of a natural number n is defined as the largest natural number m such that m3 $\le$n. The complexity of computing the cube root of n (n is represented in binary notation) is

  1. O(n) but not O(n0.5)

  2. O(n0.5) but not O((log n)k) for any constant k > 0

  3. O((log n)k) for some constant k > 0, but not O((log log n)m) for any constant m > 0

  4. O((log log n)k) for some constant k > 0.5, but not O((log log n)0.5)


Correct Option: C
Explanation:

- Hide questions