Test 1 Algorithms | Computer Science
GATE Previous year Topic Wise Questions and Answers | Algorithms
Questions
In a heap with n elements with the smallest element at the root, the 7th smallest element can be found in time
- O(n log n)
- O(n)
- O(log n)
- O(1)
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?
- I and II
- I and III
- II and III
- I, II, and III
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
- remain O (n2)
- become O (n (log n)2)
- become O (n log n)
- become O (n)
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?
- I, II and IV only
- I and IV only
- II, III and IV only
- I, III and IV only
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?
- $\frac{n(n-1)}{2}$
- $\frac{n(n-1)}{4}$
- $\frac{n(n+1)}{4}$
- $2n[\log_2n]$
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?
- O(n2)
- O (n log n)
- O (n1.5)
- O (n)
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?
- A[j,k] $\le$ n
- If A[j,j] $\ge$ n - 1, then G has a Hamiltonian cycle
- If there exists a path from j to k, A[j,k] contains the longest path length from j to k
- If there exists a path from j to k, every simple path from j to k contains at most A[j,k} edges
What is the weight of a minimum spanning tree of the following graph?

- 29
- 31
- 38
- 41
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?
- The number of edges in the shortest paths from v1 to all vertices of G
- G1 is connected
- V1 forms a clique in G
- G1 is a tree
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?
- 3
- 4
- 5
- 6
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 :
- 3
- 4
- 6
- 9
In a binary max heap containing n numbers, the smallest element can be found in time
- O (n)
- O (log n)
- O (log log n)
- O (1)
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:
- Queue
- Stack
- Heap
- B-Tree
Which one of the following in place sorting algorithms needs the minimum number of swaps?
- Quick sort
- Insertion sort
- Selection sort
- Heap sort
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:
- n − 1
- 2n − 2
- $\begin{pmatrix} n \\\\ 2 \end{pmatrix}$
- n2
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
- log2 n
- n
- 2n + 1
- 2n - 1
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
- Solves it in linear time using a left to right pass of the array
- Solves it in linear time using a right to left pass of the array
- Solves it using divide and conquer in time O(n log n)
- Solves it in time O(n2)
Consider the following C-program fragment in which i, j and n are integer variables.
for( i = n, j = 0; i > 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?
- $val(j)=\Theta(\log n)$
- $val(j)=\Theta (\sqrt{n})$
- $val(j)=\Theta( n)$
- $val(j)=\Theta (n\log 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?
- (a − b), (d − f ) , (b − f ), (d − c) , (d − e)
- (a − b), (d − f ) , (d − c) , (b − f ) , (d − e)
- (d − f ), (a − b) , (d − c) , (b − f ) , (d − e)
- (d − f ), (a − b) , (b − f ), (d − e) , (d − c)
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
- O(n) but not O(n0.5)
- O(n0.5) but not O((log n)k) for any constant k > 0
- O((log n)k) for some constant k > 0, but not O((log log n)m) for any constant m > 0
- O((log log n)k) for some constant k > 0.5, but not O((log log n)0.5)

















