Sunteți pe pagina 1din 35

GATE QUESTIONS

UNIT-I

Stack
Gate 2016 - CSE

1. Let Q denote a queue containing sixteen numbers and S be an empty stack. Head(Q) returns
the element at the head of the queue Q without removing it from Q. Similarly Top(S) returns
the element at the top of S without removing it from S. Consider the algorithm given below.

The maximum possible number of iterations of the while loop in the algorithm is______
[This Question was originally a Fill-in-the-Blanks question]
(A) 16
(B) 32
(C) 256
(D) 64

Answer: (C)

Explanation: The worst case happens when the queue is sorted in decreasing order. In worst
case, loop runs n*n times.
Queue: 4 3 2 1
Stack: Empty

321
4

3214
Empty

214
3

2143
Empty

143
2
1432
Empty

432
1

32
14

324
1

24
13

243
1

43
12

3
124

34
12

4
123

Empty
1234

Gate 2014 - CSE


2. The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is
(A) 284
(B) 213
(C) 142
(D) 71

Answer: (C)

Following is algorithm for evaluation postfix expressions.


1) Create a stack to store operands (or values).
2) Scan the given expression and do following for every scanned element.
…..a) If the element is a number, push it into the stack
…..b) If the element is a operator, pop operands for the operator from stack. Evaluate the
operator and push the result back to the stack
3) When the expression is ended, the number in the stack is the final answer
Linked List
Gate 2017-CSE
3. Consider the C code fragment given below.
typedefstruct node
{
int data;
node* next ;
} node;

void join(node* m, node* n)


{
node* p = n;
while (p->next != NULL)
{
p = p->next;
}
p–>next = m;
}
Assuming that m and n point to valid NULL- terminated linked lists, invocation of join will
(A) append list m to the end of list n for all inputs
(B) either cause a null pointer dereference or append list m to the end of list n
(C) cause a null pointer dereference for all inputs.
(D) append list n to the end of list m for all inputs.

Answer: (B)

Explanation: As it is stated in the question, that m and n are valid Lists but not explicitly
specified if the lists are empty or not. We can have two cases:
1. Case 1: If lists are not NULL : Invocation of join will append list m to the end of list n
if the lists are not NULL. For Example:Before join operation :
m =1->2->3->4->5->null
n =6->7->8->9->nullAfter join operation :
6->7->8->9->1->2->3->4->5->null
2. Case 2: If lists are NULL : If the list n is empty and itself NULL, then joining and
referencing would obviously create NULL pointer issue.

Gate 2017-CSE
4. 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________.
Note: This questions appeared as Numerical Answer Type.
(A) 2
(B) 3
(C) 4
(D) 5

Answer: (D)
Explanation: The best way to solve such a problem is by using Binary Search. Search the 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.
Find mid element
 Is mid = 1 ?
 Is mid >1?(not possible here)
 Is mid <1 ?
Proceed accordingly, Worst case of this problem will be 1 at the end of the array i.e 00000…..1
OR 1…….0000. It will take log n time worst case.
n=31, Hence log 231 = 5.
Therefore, option D is correct.

Gate 2016-CSE

5. N items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided
to the record to be deleted. For a decrease-key operation, a pointer is provided to the record
on which the operation is to be performed. An algorithm performs the following operations
on the list in this order:
Θ(N) delete, O(log N) insert, O(log N) find, and Θ(N) decrease-key
What is the time complexity of all these operations put together
(A) O(Log2N)
(B) O(N)
(C) O(N2)
(D) Θ(N2 Log N)

Answer: (C)

Explanation: The time complexity of decrease-key operation is Θ(1) since we have the pointer
to the record where we have to perform the operation. However, we must keep the doubly linked
list sorted and after the decrease-key operation we need to find the new location of the key. This
step will take Θ(N) time and since there are Θ(N) decrease-key operations, the time complexity
becomes O(N²).
Note that the other three operations have a lower bound than this one.
Gate 2015-CSE

6. An algorithm performs (logN)1/2 find operations, N insert operations, (logN)1/2 operations,


and (logN)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

Answer: (A)

Explanation: The time complexity of insert in unsorted array is O(1), O(Logn) in Min-Heap,
O(n) in sorted array and sorted DLL.
1. For unsorted array, we can always insert an element at end and do insert in O(1) time
2. For Min Heap, insertion takes O(Log n) time. Refer Binary Heap operations for details.
3. For sorted array, insert takes O(n) time as we may have to move all elements worst case.
4. For sorted doubly linked list, insert takes O(n) time to find position of element to be
inserted.
Since number of insertion operations is asymptotically higher, unsorted array is preferred.

Gate 2015-CSE

7. An unordered list contains n distinct elements. The number of comparisons to find an


element in this list that is neither maximum nor minimum is
(A) Θ(nlogn)
(B) Θ(n)
(C) Θ(logn)
(D) Θ(1)

Answer: (D)

Explanation: We only need to consider any 3 elements and compare them. So the number of
comparisons is constants, that makes time complexity as Θ(1)
The catch here is, we need to return any element that is neither maximum not minimum.
Let us take an array {10, 20, 15, 7, 90}. Output can be 10 or 15 or 20
Pick any three elements from given liar. Let the three elements be 10, 20 and 7.
Using 3 comparisons, we can find that the middle element is 10.

UNIT-II

Queue

Gate 2019 - CSE

1. A queue is implemented using a non-circular singly linked list. The queue has a head pointer
and a tail pointer, as shown in the figure. Let n denote the number of nodes in the queue. Let
'enqueue' be implemented by inserting a new node at the head, and 'dequeue' be implemented
by deletion of a node from the tail.

Which one of the following is the time complexity of the most time-efficient implementation of
'enqueue' and 'dequeue, respectively, for this data structure?

A.Θ(1), Θ(1)

B.Θ(1), Θ(n)

C.Θ(n), Θ(1)
D.Θ(n), Θ(n)

Answer : Option (B)

Solution:
For Enqueue operation, performs in constant amount of time (i.e., Θ(1)), because it modifies
only two pointers, i.e.,Create a Node P.
P-->Data = Data
P-->Next = Head
Head = P
For Dequeue operation, we need address of second last node of single linked list to make NULL
of its next pointer. Since we can not access its previous node in singly linked list, so need to
traverse entire linked list to get second last node of linked list, i.e.,temp = head;
while( temp-Next-->Next != NULL){
temp = temp-Next;
}
temp-->next = NULL;
Tail = temp;
Since, we are traversing entire linked for each Dequeue, so time complexity will be Θ(n). Option
(B) is correct.

Gate 2016 - CSE

2. A queue is implemented using an array such that ENQUEUE and DEQUEUE operations are
performed efficiently. Which one of the following statements is CORRECT (n refers to the
number of items in the queue)?
(A) Both operations can be performed in O(1) time
(B) At most one operation can be performed in O(1) time but the worst case time for the other
operation will be Ω(n)
(C) The worst case time complexity for both operations will be Ω(n)
(D) Worst case time complexity for both operations will be Ω(log n)

Answer: (A)

Explanation: We can use circular array to implement both in O(1) time. See below article for
details.

Gate 2014 - CSE

3. Suppose implementation supports an instruction REVERSE, which reverses the order of


elements on the stack, in addition to the PUSH and POP instructions. Which one of the
following statements is TRUE with respect to this modified stack?
(A) A queue cannot be implemented using this stack.
(B) A queue can be implemented where ENQUEUE takes a single instruction and DEQUEUE
takes a sequence of two instructions.
(C) A queue can be implemented where ENQUEUE takes a sequence of three instructions and
DEQUEUE takes a single instruction.
(D) A queue can be implemented where both ENQUEUE and DEQUEUE take a single
instruction each.

Answer: (C)

Explanation: To DEQUEUE an item, simply POP.


To ENQUEUE an item, we can do following 3 operations
1) REVERSE
2) PUSH
3) REVERSE

UNIT-III
Sorting

Gate 2019 - CSE

1. 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.0016
(C) 0.04
(D) 0.0008

Answer: (A)

Explanation: Given an array of 25 distinct elements, and pivot element is chosen uniformly
randomly. So, there are only 2 worst case position in the pivot element is either first (or) last.
Therefore, required probability is,
= 2/25
= 0.08
So, option (A) is correct.

Gate 2019-CSE
2. There are n unsorted arrays: A1, A2, ….,An. Assume that n is odd. Each of A1, A2,
….,Ancontains 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, ….,Anis
________ .
(A) Ο(n log n)
(B) Ο(n2)
(C) Ο(n)
(D) Ω(n2log n)

Answer: (B)
Explanation: Since given arrays are not sorted, so the time complexity to find median is O(n) in
an unsorted array. You need to apply this apgorithm n time to find all medians and again once to
find median of all these medians, so total time complexity is,
= O(n)*O(n) + O(n)
= O(n2) + O(n)
≈ O(n2)
So, option (B) is correct.

Gate 2017-CSE
3. Match the algorithms with their time complexities:

(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)

Answer: (C)

Explanation:
 Tower of Hanoi – Ɵ(2n)
 Heap sort worst case – Ɵ(n log n)
 Binary Search – Ɵ(log n)
 Addition of two nxn matrices – Ɵ (n2)
Therefore Option C is correct

Gate 2016-CSE
4. 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

Answer: (D)
Explanation: I. Given an array in ascending order, Recurrence relation for total number of
comparisons for quicksort will be
T(n) = T(n-1)+O(n) //partition algo will take O(n) comparisons in any case.
= O(n^2)

II. Bubble Sort runs in Θ(n^2) time


If an array is in ascending order, we could make a small modification in Bubble Sort Inner for
loop which is responsible for bubbling the kth largest element to the end in kth iteration.
Whenever there is no swap after the completion of inner for loop of bubble sort in any iteration,
we can declare that array is sorted in case of Bubble Sort taking O(n) time in Best Case.
III. Merge Sort runs in Θ(n) time
Merge Sort relies on Divide and Conquer paradigm to sort an array and there is no such worst or
best case input for merge sort. For any sequence, Time complexity will be given by following
recurrence relation

T(n) = 2T(n/2) + Θ(n) // In-Place Merge algorithm will take Θ(n) due to copying an entire array.
= Θ(nlogn)
IV. Insertion sort runs in Θ(n) time
Whenever a new element which will be greater than all the elements of the intermediate sorted
sub-array ( because given array is sorted) is added, there won’t be any swap but a single
comparison. In n-1 passes we will be having 0 swaps and n-1 comparisons.
Total time complexity = O(n) // N-1 Comparisons

For an array already sorted in ascending order,


Quicksort has a complexity Θ(n2) [Worst Case]
Bubblesort has a complexity Θ(n) [Best Case]
Mergesort has a complexity Θ(n log n) [Any Case]
Insertsort has a complexity Θ(n) [Best Case]

Gate 2015-CSE
5. 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(0) + cn
(C) T(n) = 2T (n – 2) + cn
(D) T(n) = T(n/2) + cn

Answer: (B)
Explanation: In worst case, the chosen pivot is always placed at a corner position and recursive
call is made for following.
a) forsubarray on left of pivot which is of size n-1 in worst case.
b) forsubarray on right of pivot which is of size 0 in worst case.

Gate 2015-CSE
6. 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(nLogn)
(D) O(n^2)
Answer: (B)

Explanation: To sort elements in increasing order, selection sort always picks the maximum
element from remaining unsorted array and swaps it with the last element in the remaining
array. So the number of swaps, it makes in n-1 which is O(n)

Gate 2014-CSE
7. 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

Answer: (B)

Explanation: Time complexity of merge sort is Θ(nLogn)

c*64Log64 is 30
c*64*6 is 30
c is 5/64
For time 6 minutes
5/64*nLogn = 6*60
nLogn = 72*64 = 512 * 9
n = 512.
UNIT-IV
Binary Search Tree
Gate 2019 CSE
1. Let T be a full binary tree with 8 leaves. (A full binary tree has every level full.) Suppose two
leaves a and b of T are chosen uniformly and independently at random. The expected value
of the distance between a and b in T (i.e., the number of edges in the unique path between a
and b) is (rounded off to 2 decimal places) ___________ .
Note: This was Numerical Type question.
(A) 5.71 to 5.73
(B) 4.85 to 4.86
(C) 2.71 to 2.73
(D) 4.24 to 4.26

Answer: (D)
Explanation: Full binary tree with 8 leaf nodes,

Two leaf nodes can be selected in 8*8 = 64 ways.


Where, X is length between two nodes selected.

The expected value of the length between a and b in T,


= E[X]
= X * P[X]
= 0*(8/64) + 2*(8/64) + 4*(16/64) + 6*(32/64)
= 272/64
= 4.25
So, answer is 4.25.
Alternative way:
Sum of distances from a particular leaf to the remaining 7 leaves is 34. The sum would remain
the same for each leaf node. Therefore total sum of distance of all the leaf nodes = 34*8.
Two leaf nodes can be selected in 8*8 = 64 ways.
Therefore, the expected value of the length between a and b in T,
= (34*8) / (8*8)
= 34 / 8
= 4.25
Gate 2019 CSE
2. Which one of the following statements is NOT correct about the B+ tree data structure used
for creating an index of a relational database table?
(A) B+ Tree is a height-balanced tree
(B) Non-leaf nodes have pointers to data records
(C) Key values in each node are kept in sorted order
(D) Each leaf node has a pointer to the next leaf node

Answer: (B)

Explanation: B+ tree is height balance search tree, where key values in each node are kept in
sorted order.
All leaf nodes are at same level and connected to next leaf node.
Each non-leaf (i.e., internal) node is of the form:
<P1, K1, P2, K2, ….., Pc-1, Kc-1, Pc>
where c <= a and each Pi is a tree pointer (i.e points to another node of the tree) and, each Ki is a
key value. That means each non-leaf (i.e., internal) nodes has only block (i.e., node or tree
pointers) and keys. These internal (i.e., non-leaf) node do not contain data record pointers. So,
option (B) is not correct.

Gate 2018 CSE

3. The post order traversal of a binary tree is 8, 9, 6, 7, 4, 5, 2, 3, 1. The inorder traversal of the
same tree is 8, 6, 9, 4, 7, 2, 5, 1, 3. The height of a tree is the length of the longest path from
the root to any leaf. The height of the binary tree above is ________

A.2
B.3
C.4
D.5
Answer: (C)
Solution:
Explanation: Given, post-order – 8, 9, 6, 7, 4, 5, 2, 3, 1
and in-order – 8, 6, 9, 4, 7, 2, 5, 1, 3
Construct a binary tree from postorder and inordertraversal :

The height of the binary tree above is 4.

Gate 2018-CSE

4. The number of possible min-heaps containing each value from {1, 2, 3, 4, 5, 6, 7} exactly
once is _______.

(A) 80

(B) 8

(C) 20

(D) 210

Answer: (A)

Solution:
Explanation: Set minimum element as root (i.e 1), now 6 are remaining and left subtree will have
3 elements, each left subtree combination can be permuted in 2! ways.
Total ways to design min-heap with 7-elements = 6C_3 *2! * 2! = 20*2*2 = 80
Alternative approach – Total number of min or max heap tree with 1 to N elements are using
recurrence relation:
T(N) =(N-1)Ck * T(k) * T(N-k-1), where k = number of nodes on left subtree
T(1) = 1
T(2) = 1
T(3) = 2
T(4) = 3C2 * T(2) * T(1) = 3
T(5) = 4C3 * T(3) * T(1) = 8
T(6) = 5C3 * T(3) * T(2) = 20
T(7) = 5C3 * T(3) * T(3) = 80
So, answer is 80.

Gate 2017-CSE
5. Let T be a binary search tree with 15 nodes. The minimum and maximum possible heights of
T are:
Note: The height of a tree with a single node is 0.
(A) 4 and 15 respectively
(B) 3 and 14 respectively
(C) 4 and 14 respectively
(D) 3 and 15 respectively

Answer: (B)

Explanation:
 The minimum height of a binary search tree will be when tree is full complete tree:

Now, let h be the height of the binary tree, then,


2^{0}+2^{1}+2^{2}+2^{3}+…+2^{h}=2^{h+1}-1 <= n
So, Min height of a binary search tree = log2(n+1) – 1 = log2(15+1) – 1 = 4 – 1 = 3
 The maximum height of a binary search tree will be when the tree is fully skewed: (like
below)

Max height of the binary search tree = n-1 = 15 – 1 = 14, where the tree is Skewed tree
Gate 2017-CSE
6. Let T be a tree with 10 vertices. The sum of the degrees of all the vertices in T is _____.

(A) 18
(B) 19
(C) 20
(D) 21

Answer: (A)

Explanation: Given, v= Total vertices = 10


e = v – 1 =9
Degree = 2 * e = 18
Therefore, option A is correct

Gate 2016-CSE
7. B+ Trees are considered BALANCED because
(A) the lengths of the paths from the root to all leaf nodes are all equal.
(B) the lengths of the paths from the root to all leaf nodes differ from each other by at most 1.
(C) the number of children of any two non-leaf sibling nodes differ by at most 1.
(D) the number of records in any two leaf nodes differ by at most 1.

Answer: (A)

Explanation: In both B Tree and B+ trees, depth (length of root to leaf paths) of all leaf nodes is
same. This is made sure by the insertion and deletion operations.
In these trees, we do insertions in a way that if we have increase height of tree after insertion, we
increase height from root. This is different from BST where height increases from leaf nodes.
Similarly, if we have to decrease height after deletion, we move the root one level down. This is
also different from BST which shrinks from bottom.
The above ways of insertion and deletion make sure that depth of every leaf node is same.

Gate 2016-CSE
8. A complete binary min-heap is made by including each integer in [1, 1023] exactly once. The
depth of a node in the heap is the length of the path from the root of the heap to that node.
Thus, the root is at depth 0. The maximum depth at which integer 9 can appear is
_____________
(A) 6
(B) 7
(C) 8
(D) 9

Answer: (C)
Explanation: here node with integer 1 has to be at root only. Now for maximum depth of the
tree the following arrangement can be taken. Take root as level 1.
make node 2 at level 2 as a child node of node 1.
make node 3 at level 3 as the child node of node 2.
..
.. and so on for nodes 4,5,6,7
..
make node 8 at level 8 as the child node of node 7.
make node 9 at level 9 as the child node of node 8.
Putting other nodes properly, this arrangement of the the complete binary tree will follow the
property of min heap.
So total levels are 9.node 9 is at level 9 and depth of node 9 is 8 from the root.

Gate 2016-CSE
9. Consider the following New-order strategy for traversing a binary tree:
Visit the root;
Visit the right subtree using New-order
Visit the left subtree using New-order
The New-order traversal of the expression tree corresponding to the reverse polish expression 3 4
* 5 – 2 ˆ 6 7 * 1 + – is given by:
(A) + – 1 6 7 * 2 ˆ 5 – 3 4 *
(B) – + 1 * 6 7 ˆ 2 – 5 * 3 4
(C) – + 1 * 7 6 ˆ 2 – 5 * 4 3
(D) 1 7 6 * + 2 5 4 3 * – ˆ –

Answer: (C)

Explanation:
Reverse Polish expression is derived through Post-Order i.e.
1) Visit Left Node
2) Visit Right Node (L R N)
3) Visit Root Node
— Acc. to Ques. New Order algorithm is :
1) Visit Root Node
2) Visit Right Node (N R L)
3) Visit Left Node
ie. New Order Expression will be a total reverse of the Post-Order algorithm
Post-Order Expression : 34*5–2ˆ67*1+–
Hence , New Order Expression : – + 1 * 7 6 ^ 2 – 5 * 4 3

Gate 2016-CSE
10. The number of ways in which the numbers 1, 2, 3, 4, 5, 6, 7 can be inserted in an empty
binary search tree, such that the resulting tree has height 6, is _____________
Note: The height of a tree with a single node is 0.
(A) 2
(B) 4
(C) 64
(D) 32

Answer: (C)

Explanation: To get height 6, we need to put either 1 or 7 at root.


So count can be written as T(n) = 2*T(n-1) with T(1) = 1

7
/
[1..6]

1
\
[2..7]
Therefore count is 26 = 64

Another Explanation:
Consider these cases,
1234567
1234576
1765432
1765423
7654321
7654312
7123456
7123465
For height 6, we have 2 choices. Either we select the root as 1 or 7.
Suppose we select 7.
Now, we have 6 nodes and remaining height = 5.
So, now we have 2 ways to select root for this sub-tree also.
Now, we keep on repeating the same procedure till remaining height = 1
For this last case also, we have 2 ways.
Therefore, total number of ways = 26= 64

Gate 2015-CSE
Which of the following is/are correct inorder traversal sequence(s) of binary search tree(s)?
1. 3, 5, 7, 8, 15, 19, 25
2. 5, 8, 9, 12, 10, 15, 25
3. 2, 7, 10, 8, 14, 16, 20
4. 4, 6, 7, 9, 18, 20, 25
(A) 1 and 4 only
(B) 2 and 3 only
(C) 2 and 4 only
(D) 2 only

Answer: (A)

Explanation: An Inorder traversal of a Binary Search Tree must be in increasing order.

Gate 2015-CSE
11. What are the worst-case complexities of insertion and deletion of a key in a binary search
tree?
(A) Θ(logn) for both insertion and deletion
(B) Θ(n) for both insertion and deletion
(C) Θ(n) for insertion and Θ(logn) for deletion
(D) Θ(logn) for insertion and Θ(n) for deletion

Answer: (B)

Explanation: The time taken by search, insert and delete on a BST is always proportional to
height of BST. Height may become O(n) in worst case.

Gate 2015-CSE
12. The height of a tree is the length of the longest root-to-leaf path in it. The maximum and
minimum number of nodes in a binary tree of height 5 are
(A) 63 and 6, respectively
(B) 64 and 5, respectively
(C) 32 and 6, respectively
(D) 31 and 5, respectively

Answer: (A)

Explanation:
Number of nodes is maximum for a perfect binary tree.
A perfect binary tree of height h has 2h+1 - 1 nodes

Number of nodes is minimum for a skewed binary tree.


A perfect binary tree of height h has h+1 nodes.

Gate 2015-CSE
13. Consider a max heap, represented by the array: 40, 30, 20, 10, 15, 16, 17, 8, 4. Now consider
that a value 35 is inserted into this heap. After insertion, the new heap is
(A) 40, 30, 20, 10, 15, 16, 17, 8, 4, 35
(B) 40, 35, 20, 10, 30, 16, 17, 8, 4, 15
(C) 40, 30, 20, 10, 35, 16, 17, 8, 4, 15
(D) 40, 35, 20, 10, 15, 16, 17, 8, 4, 30
Answer: (B)

Explanation: The array 40, 30, 20, 10, 15, 16, 17, 8, 4 represents following heap

40
/ \
30 20
/\ /\
10 15 16 17
/\
8 4
After insertion of 35, we get
40
/ \
30 20
/\ /\
10 15 16 17
/\ /
8 4 35
After swapping 35 with 15 and swapping 35 again
with 30, we get
40
/ \
35 20
/\ /\
10 30 16 17
/\ /
8 4 15

Gate 2015-CSE
14. A binary tree T has 20 leaves. The number of nodes in T having two children is
(A) 18
(B) 19
(C) 17
(D) Any number between 10 and 20
Answer: (B)

Explanation:
Sum of all degrees = 2 * |E|.
Here considering tree as a k-arytree :

Sum of degrees of leaves + Sum of degrees for Internal Node except root + Root's degree = 2 *
(No. of nodes - 1).
Putting values of above terms,
L + (I-1)*(k+1) + k = 2 * (L + I - 1)
L + k*I - k + I -1 + k = 2*L + 2I - 2
L + K*I + I - 1 = 2*L + 2*I - 2
K*I + 1 - I = L
(K-1)*I + 1 = L
Given k = 2, L=20
==> (2-1)*I + 1 = 20
==> I = 19
==> T has 19 internal nodes which are having two children.

Gate 2015-CSE
15. With reference to the B+ tree index of order 1 shown below, the minimum number of nodes
(including the root node) that must be fetched in order to satisfy the following query: ―Get all
records with a search key greater than or equal to 7 and less than 15‖ is ________

(A) 4
(B) 5
(C) 6
(D) 7

Answer: (B)

Explanation:
We can get all values in range from 7 to 59 by accessing 5 nodes.
1) First search 7 in a leaf node.
2) Once 7 is found, linearly traverse till 15 is found.

See following diagram


Gate 2014-CSE
16. While inserting the elements 71, 65, 84, 69, 67, 83 in an empty binary search tree (BST) in
the sequence shown, the element in the lowest level is
(A) 65
(B) 67
(C) 69
(D) 83

Answer: (B)

Explanation: Here is The Insertion Algorithm For a Binary Search Tree :


Insert(Root,key)
{
if(Root is NULL)
Create a Node with value as key and return
Else if(Root.key>= key)
Insert(Root.left,key)
Else
Insert(Root.right,key)
}
Creating the BST one by one using the above algorithm in the below given image :
Gate 2014-CSE
17. Consider the following array of elements. 〈89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100〉. The
minimum number of interchanges needed to convert it into a max-heap is
(A) 4
(B) 5
(C) 2
(D) 3

Answer: (D)

Explanation: 〈89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100〉

89
/ \
19 50
/ \ / \
17 12 15 2
/ \ /\ / \
5 7 11 6 9 100

Minimum number of swaps required to convert above tree


to Max heap is 3. Below are 3 swap operations.
Swap 100 with 15
Swap 100 with 50
Swap 100 with 89

100
/ \
19 89
/ \ / \
17 12 50 5
/ \ /\ / \
7 11 6 9 2 15

Gate 2014-CSE
18. Consider a binary tree T that has 200 leaf nodes. Then, the number of nodes in T that have
exactly two children are _________.
(A) 199
(B) 200
(C) Any number between 0 and 199
(D) Any number between 100 and 200
Answer: (A)
Gate 2014-CSE
19. Consider B+ tree in which the search key is 12 bytes long, block size is 1024 bytes, record
pointer is 10 bytes long and block pointer is 8 bytes long. The maximum number of keys that
can be accommodated in each non-leaf node of the tree is
(A) 49
(B) 50
(C) 51
(D) 52

Answer: (B)

Explanation:
Let m be the order of B+ tree

m(8)+(m-1)12 <= 1024


[Note that record pointer is not needed in non-leaf nodes]

m <= 51

Since maximum order is 51, maximum number of keys is 50.

Gate 2014-CSE
20. Consider a rooted Binary tree represented using pointers. The best upper bound on the time
required to determine the number of subtrees having having exactly 4 nodes O(naLogn b).
Then the value of a + 10b is ________
(A) 1
(B) 11
(C) 12
(D) 21

Answer: (A)

Explanation: We can find the subtree with 4 nodes in O(n) time. Following can be a simple
approach.
1) Traverse the tree in bottom up manner and find size of subtree rooted with current node
2) If size becomes 4, then print the current node.

Gate 2013-CSE

21. 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(Logn)
(C) O(n)
(D) O(nLogn)
Answer: (C)

Explanation: To insert an element, we need to search for its place first. The search operation
may take O(n) for a skewed tree like following.
To insert 50, we will have to traverse all nodes.
10
\
20
\
30
\
40

UNIT-V

Gate 2015: CSE


1. Which one of the following hash functions on integers will distribute keys most uniformly
over 10 buckets numbered 0 to 9 for i ranging from 0 to 2020?
(A) h(i) =i2 mod 10
(B) h(i) =i3 mod 10
(C) h(i) = (11 ∗ i2) mod 10
(D) h(i) = (12 ∗ i) mod 10

Answer: (B)
Explanation: Since mod 10 is used, the last digit matters.If you do cube all numbers from 0
to 9, you get following
Number Cube Last Digit in Cube
0 0 0
1 1 1
2 8 8
3 27 7
4 64 4
5 125 5
6 216 6
7 343 3
8 512 2
9 729 9
Therefore all numbers from 0 to 2020 are equally divided in 10 buckets. If we make a table for
square, we don‟t get equal distribution. In the following table. 1, 4, 6 and 9 are repeated, so
these buckets would have more entries and buckets 2, 3, 7 and 8 would be empty.
Number Square Last Digit in Cube
0 0 0
1 1 1
2 4 4
3 9 9
4 16 6
5 25 5
6 36 6
7 49 9
8 64 4
9 81 1
Alternative approach –
Using concept of power of cycle:
(a) (0,1,4,9,6,5,6,9,4,1,0) repeated
(b) (0,1,8,7,4,5,6,3,2,9) repeated
(c) (0,1,4,9,6,5,6,9,4,1,0) repeated
(d) (0,2,4,6,8) repeated
So, only h(i) =i3 mod 10 covers all the digits from 0 to 9.
Option (B) is correct.

Gate 2014: CSE


2. Given a hash table T with 25 slots that stores 2000 elements, the load factor α for T is
__________
(A) 80
(B) 0.0125
(C) 8000
(D) 1.25

Answer: (A)
Explanation: load factor = (no. of elements) / (no. of table slots) = 2000/25 = 80

GRAPH

Gate 2019-CSE
1. 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) 2
(D) 3
Option (A) is correct.
Explanation: To maximize the number of minimum weight spanning trees of G, the value of x
will be 5 because it will have two more choices for corner vertex which will maximize maximum
number of MSTs.
Now, according to kruskal algorithm for MST:
1. Edges with weights 1 and 3 will be selected first,
2. Now bottom edge with weight 4 will not be selected as will cause cycle on MST,
3. both corner vertices have two-two choices to select the vertices, so these corner edges
with weights 4 and 5 will resultant 2*2 = 4 MSTs.
Therefore, total number of MSTs are 2*2 = 4, which is answer.
Gate 2019-CSE
2. 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 V 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 Vgreedy.
The value of Vopt − Vgreedy is ______ .

Note –This was Numerical Type question.


(A) 16
(B) 8
(C) 44
(D) 60
Answer: (A)
Explanation:

First we will pick item_4 (Value weight ratio is highest). Second highest is item_1, but cannot be
picked because of its weight. Now item_3 shall be picked. item_2 cannot be included because
of its weight.
Therefore, overall profit by Vgreedy = 20+24 = 44
Hence, Vopt – Vgreedy = 60-44 = 16
So, answer is 16.
Gate 2019-CSE
3. Let G be a simple undirected graph. Let T D be a depth first search tree of G. Let TB be a
breadth first search tree of G. Consider the following statements.
(I) No edge of G is a cross edge with respect to T D. (A cross edge in G is between two nodes
neither of which is an ancestor of the other in T D).
(II) For every edge (u, v) of G, if u is at depth i and v is at depth j in T B, then ∣i − j∣ = 1.
Which of the statements above must necessarily be true?
(A) I only
(B) II only
(C) Both I and II
(D) Neither I nor II

Answer: (A)

Explanation: There are four types of edges can yield in DFS. These are tree, forward, back,
and cross edges. In undirected connected graph, forward and back egdes are the same thing. A
cross edge in a graph is an edge that goes from a vertex v to another vertex u such that u is
neither an ancestor nor descendant of v. Therefore, cross edge is not possible in undirected
graph.
So, statement (I) is correct.
For statement (II) take counterexample of complete graph of three vertices, i.e., K3 with XYZ,
where X is source and Y and Z are in same level. Also,there is an edge between vertices Y and
Z, i.e., |i-j| = 0 ≠ 1 in BFS. So, statement became false.

Gate 2017-CSE
4. 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)

Answer: (C)
Explanation:
 Kruskal‟s is a greedy technique of Minimum Spanning Tree Algorithm to find an edge of
the least possible weight that connects any two trees in the forest.
 QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions
the given array around the picked pivot.
 Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem using
Dynamic Programming. The problem is to find shortest distances between every pair of
vertices in a given edge weighted directed Graph.

Gate 2017-CSE
5. Let G = (V, E) be any connected undirected edge-weighted graph. The weights of the edges
in E are positive any 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 and II
Answer: (A)
Explanation: I. Minimum Spanning Tree of G is always unique – MST will wlways be distinct if
the edges are unique so Correct
II. Shortest path between any two vertices of G is always unique – Shortest path between any
two vertices can be same so incorrect
Alternate solution:
We know that minimum spanning tree of a graph is always unique if all the weight are distinct,
so statement 1 is correct.
Now statement 2 , this might not be true in all cases. Consider the graph.

There are two shortest paths from a to b (one is direct and other via node c) So statement 2 is
false.

Gate 2017-CSE
6. Breath First Search(BFS) has been implemented using queue data structure.

Which one of the following is a possible order of visiting the nodes in the graph above.
(A) MNOPQR
(B) NQMPOR
(C) QMNROP
(D) POQNMR

Answer: (D)
Explanation: In BFS, we print a starting node, then its adjacent, then adjacent of adjacent, and
so on..
Option A : MNOPQR Wrong
We cannot visit "O" before "R" as we start from "M".
Note that "O" is adjacent of adjacent for "M" and
"R" is adjacent of "M".

Option B : NQMPOR Wrong


We cannot visit "P" before "O" as we start from "N".
Note that "P" is adjacent of adjacent for "N" and
"O" is adjacent.

Option C : QMNROP Wrong


We cannot visit "R" before "O" as we start from "Q".
Note that "R" is adjacent of adjacent for "Q" and
"O" is adjacent of "Q"

Option D : POQNMR Right


We visit "P", then its adjacent "O", "Q" and "N"
Finally we visit adjacent of adjacent "M" and "R"

Gate 2016-CSE
7. Consider the following directed graph.

The number of different topological orderings of the vertices of the graph is

Note : This question was asked as Numerical Answer Type.


(A) 1
(B) 2
(C) 4
(D) 6

Answer: (D)

Explanation: Following are different 6 Topological Sortings


a-b-c-d-e-f
a-d-e-b-c-f
a-b-d-c-e-f
a-d-b-c-e-f
a-b-d-e-c-f
a-d-b-e-c-f

Gate 2016-CSE
8. The worst case running times of Insertion sort, Merge sort and Quick sort, respectively, are:
(A) Θ(n log n), Θ(n log n) and Θ(n 2)
(B) Θ(n2), Θ(n2) and Θ(n Log n)
(C) Θ(n2), Θ(n log n) and Θ(n log n)
(D) Θ(n2), Θ(n log n) and Θ(n2)

Answer: (D)
Explanation:
 Insertion Sort takes Θ(n2) in worst case as we need to run two loops. The outer loop is
needed to one by one pick an element to be inserted at right position. Inner loop is used
for two things, to find position of the element to be inserted and moving all sorted greater
elements one position ahead. Therefore the worst case recursive formula is T(n) = T(n-1)
+ Θ(n).
 Merge Sort takes Θ(n Log n) time in all cases. We always divide array in two halves, sort
the two halves and merge them. The recursive formula is T(n) = 2T(n/2) + Θ(n).
 QuickSort takes Θ(n2) in worst case. In QuickSort, we take an element as pivot and
partition the array around it. In worst case, the picked element is always a corner element
and recursive formula becomes T(n) = T(n-1) + Θ(n). An example scenario when worst
case happens is, arrays is sorted and our code always picks a corner element as pivot.

Gate 2016-CSE
9. Let G be a weighted connected undirected graph with distinct positive edge weights. If every
edge weight is increased by the same value, then which of the following statements is/are
TRUE?

P: Minimum spanning tree of G does not change


Q: Shortest path between any pair of vertices does not change
(A) P only
(B) Q only
(C) Neither P nor Q
(D) Both P and Q

Answer: (A)

Explanation: The shortest path may change. The reason is, there may be different number of
edges in different paths from s to t. For example, let shortest path be of weight 15 and has 5
edges. Let there be another path with 2 edges and total weight 25. The weight of the shortest
path is increased by 5*10 and becomes 15 + 50. Weight of the other path is increased by 2*10
and becomes 25 + 20. So the shortest path changes to the other path with weight as 45.
The Minimum Spanning Tree doesn‟t change. Remember the Kruskal‟s algorithm where we sort
the edges first. IF we increase all weights, then order of edges won‟t change.

Gate 2016-CSE

10. An operator delete(i) for a binary heap data structure is to be designed to delete the item in
the i-th node. Assume that the heap is implemented in an array and i refers to the i-th index
of the array. If the heap tree has depth d (number of edges on the path from the root to the
farthest leaf), then what is the time complexity to re-fix the heap efficiently after the removal
of the element?
(A) O(1)
(B) O(d) but not O(1)
(C) O(2d) but not O(d)
(D) O(d2d) but not O(2d)

Answer: (B)

Explanation:
For this question, we have to slightly tweak the delete_min() operation of the heap data
structure to implement the delete(i) operation. The idea is to empty the spot in the array at the
index i (the position at which element is to be deleted) and replace it with the last leaf in the
heap (remember heap is implemented as complete binary tree so you know the location of the
last leaf), decrement the heap size and now starting from the current position i (position that
held the item we deleted), shift it up in case newly replaced item is greater than the parent of old
item (considering max-heap). If it‟s not greater than the parent, then percolate it down by
comparing with the child‟s value. The newly added item can percolate up/down a maximum of d
times which is the depth of the heap data structure.
Thus we can say that complexity of delete(i) would be O(d) but not O(1).
Gate 2016-CSE

11. Consider the weighted undirected graph with 4 vertices, where the weight of edge {i, j} g is
given by the entry
W ij in the matrix W

The largest possible integer value of x, for which at least one shortest path between some
pair of vertices will contain the edge with weight x is ________

Note : This question was asked as Numerical Answer Type.


(A) 8
(B) 12
(C) 10
(D) 11

Answer: (B)

Explanation: Let vertices be 0, 1, 2 and 3.


x directly connects 2 to 3. The shortest path (excluding x) from 2 to 3 is of weight 12 (2-1-0-3).
Gate 2016-CSE

12. 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.
[This Question was originally a Fill-in-the-Blanks question]
(A) 6
(B) 7
(C) 8
(D) 9

Answer: (B)

Explanation: One graph that has maximum possible weight of spanning tree

Gate 2016-CSE
13. 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

Answer: (B)

Explanation:
I is NOT true.

Let G=(V, E) be a rectangular graph where V = {a, b, c, d} and E = {ab, bc, cd, da, ac}.
Let the edges have weights: ab = 1, bc = 2, cd = 4, da = 5, ac = 3. Then, clearly, ac is the
lightest edge of the cycle cdac, however, the MST abcd with cost 7 (= ab + bc + cd) does not
include it.
Let the edges have weights: ab = 6, bc – 7, cd = 4, da = 5, ac = 3. Then, again, ac is the lightest
edge of the cycle cdac, and, the MST bacd with cost 13 (= ba + ac + cd) includes it.
So, the MSTs of G may or may not include the lightest edge.
II is true
Let the heavies edge be e. Suppose the 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.

Gate: 2016-CSE
14. Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There
is a vertex t at a distance four from the root. If t is the n-th vertex in this BFS traversal, then
the maximum possible value of n is ________
(A) 15
(B) 16
(C) 31
(D) 32

Answer: (C)

Explanation: It would be node number 31 for given distance 4.


For example if we consider at distance 2, below highlighted node G can be the farthest node at
position 7.
A
/ \
B C
/\ /\
D E F G
Alternative Solution :
t is the n-th vertex in this BFS traversal at distance four from the root. So height of tree is 4.
Max number of nodes = 2^{h+1} − 1 = 2^{5} − 1 = 31
At distance four, last node is 31. option (C)
Gate: 2016-CSE
15. In an adjacency list representation of an undirected simple graph G = (V, E), each edge (u,
v) has two adjacency list entries: [v] in the adjacency list of u, and [u] in the adjacency list of
v. These are called twins of each other. A twin pointer is a pointer from an adjacency list
entry to its twin. If |E| = m and |V | = n, and the memory size is not a constraint, what is the
time complexity of the most efficient algorithm to set the twin pointer in each entry in each
adjacency list?
(A) Θ(n2)
(B) Θ(m+n)
(C) Θ(m2)
(D) Θ(n4)

Answer: (B)

Explanation: First you need to find twins of each node. You can do this using level order
traversal (i.e., BFS) once. Time complexity of BFS is Θ(m +n).
And you have to use linked list for representation which is extra space (but memory size is
not a constraint here).
Final, time complexity is Θ(m + n) to set twin pointer.
Option (B) is correct.

Gate 2015-CSE
16. Given below are some algorithms, and some algorithm design paradigms.
List-I
A. Dijkstra‟s Shortest Path
B. Floyd-Warshall algorithm to compute all pairs shortest path
C. Binary search on a sorted array
D. Backtracking search on a graph
List-II
1. Divide and Conquer
2. Dynamic Programming
3. Greedy design
4. Depth-first search
5. Breadth-first search
Match the above algorithms on the left to the corresponding design paradigm they follow
Codes:
ABCD
(a) 1 3 1 5
(b) 3 3 1 5
(c) 3 2 1 4
(d) 3 2 1 5
(A) a
(B) b
(C) c
(D) d

Answer: (C)

Explanation:
Dijkstra‟s Shortest Path is a Greedy Algorithm.
Floyd-Warshallalgorithm is Dynamic Programming.
Binary search is a Divide and Conquer.
Backtracking is Depth-first search
Gate 2015-CSE

17. Let G = (V, E) be a simple undirected graph, and s be a particular vertex in it called the
source. For x ∈ V, let d(x) denote the shortest distance in G from s to x. A breadth first
search (BFS) is performed starting at s. Let T be the resultant BFS tree. If (u, v) is an edge
of G that is not in T, then which one of the following CANNOT be the value of d(u) – d(v)?
(A) -1
(B) 0
(C) 1
(D) 2

Answer: (D)

Explanation: Note that the given graph is undirected, so an edge (u, v) also means (v, u) is
also an edge.
Since a shorter path can always be obtained by using edge (u, v) or (v, u), the difference
between d(u) and d(v) can not be more than 1.

Gate 2015-CSE
18. 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) 66
(B) 69
(C) 68
(D) 70

Answer: (B)

Explanation: In every cycle, the weight of an edge that is not part of MST must by greater than
or equal to weights of other edges which are part of MST.
Since all edge weights are distinct, the weight must be greater.
So the minimum possible weight of ED is 7, minimum possible weight of CD is 16 and minimum
possible weight of AB is 10.
Therefore minimum possible sum of weights is 69.
Gate 2014-CSE

19. The number of distinct minimum spanning trees for the weighted graph below is ____
(A) 4
(B) 5
(C) 6
(D) 7

Answer: (C)

Explanation: Below diagram shows a minimum spanning tree. Highlighted (in green) are
the edges picked to make the MST.

In the right side of MST, we could either pick edge „a‟ or „b‟. In the left side, we could either pick
„c‟ or „d‟ or „e‟ in MST.
There are 2 options for one edge to be picked and 3 options for another edge to be picked.
Therefore, total 2*3 possible MSTs.

Gate 2014-CSE

20. Consider the tree arcs of a BFS traversal from a source node W in an unweighted,
connected, undirected graph. The tree T formed by the tree arcs is a data structure for
computing.
(A) the shortest path between every pair of vertices.
(B) the shortest path from W to every vertex in the graph.
(C) the shortest paths from W to only those nodes that are leaves of T.
(D) the longest path in the graph

Answer: (B)
Explanation: BFS always produces shortest path from source to all other vertices in an
unweighted graph.

Question : 2014-CSE
21. Let G be 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) 1000
(B) 995
(C) 2000
(D) 1995

Answer: (B)
Explanation: Since there are 100 vertices, there must be 99 edges in Minimum Spanning
Tree (MST).
When weight of every edge is increased by 5, the increment in weight of MST is = 99 * 5 = 495
So new weight of MST is 500 + 495 which is 995
Gate 2014-CSE
22. Let G=(V,E) be a directed graph where V is the set of vertices and E the set of edges. Then
which one of the following graphs has the same strongly connected components as G ?
(A) A
(B) B
(C) C
(D) D

Answer: (B)

Explanation: If we reverse directions of all arcs in a graph, the new graph has same set of
strongly connected components as the original graph.

Gate 2014-CSE
23. Let G be a graph with n vertices and m edges. What is the tightest upper bound on the
running time on Depth First Search of G? Assume that the graph is represented using
adjacency matrix.
(A) O(n)
(B) O(m+n)
(C) O(n2)
(D) O(mn)

Answer: (C)
Explanation: Depth First Search of a graph takes O(m+n) time when the graph is
represented using adjacency list.
In adjacency matrix representation, graph is represented as an “n x n” matrix. To do DFS, for
every vertex, we traverse the row corresponding to that vertex to find all adjacent vertices (In
adjacency list representation we traverse only the adjacent vertices of the vertex). Therefore
time complexity becomes O(n2)

Question 1 : 2014-CSE
24. Consider the directed graph given below. Which one of the following is TRUE?

(A) The graph doesn‟t have any topological ordering


(B) Both PQRS and SRPQ are topological ordering
(C) Both PSRQ and SPRQ are topological ordering
(D) PSRQ is the only topological ordering

Answer: (C)

Explanation: The graph doesn‟t contain any cycle, so there exist topological ordering.
P and S must appear before R and Q because there are edges from P to R and Q, and from S
to R and Q.

S-ar putea să vă placă și