Gate Cse
Gate Cse
Searching, sorting, hashing. Asymptotic worst case time and space complexity. Algorithm design techniques: greedy,
dynamic programming and divide‐and‐conquer. Graph traversals, minimum spanning trees, shortest paths.
Chapter
Paper Ch.1 Ch.2 Ch.3 Ch.4 Ch.5
Ch.6
Year
2008 6 4 2 3 8 3
2009 2 3 2 0 5 0
2010 1 2 0 4 0 0
2011 1 1 2 6 5 0
2012 1 2 0 2 1 1
2013 2 2 0 0 1 1
2014 (P1) 0 3 0 2 0 0
2014 (P2) 1 0 2 1 2 1
2014 (P3) 1 2 0 1 0 2
2015 (P1) 4 3 2 2 0 4
2015 (P2) 2 1 0 0 0 3
2015 (P3) 3 2 0 0 0 0
2016 (P1) 0 1 5 3 0 0
2016 (P2) 2 3 0 1 3 0
2017 (P1) 1 0 0 1 1 0
2017 (P2) 2 0 2 0 0 1
2018 0 2 2 2 4 0
2019 0 4 0 0 0 0
2020 1 2 2 1 0 5
2021 (P1) 4 3 1 2 0 0
2021 (P2) 0 3 2 1 0 3
2022 1 0 0 0 0 4
2023 4 0 0 2 0 0
Design of springs
CHAPTER
1
Asymptotic Notation 4. [MCQ] [GATE-2021 : 1M]
1. [MSQ] [GATE-2023 : 2M] Which of the given options provides the increasing
Let f and g be functions of natural numbers given order of asymptotic complexity of functions f 1, f2, f3
and f4?
by f(n) = n and g(n) = n2.
F1(n) = 2n F2(n) = n3/2
Which of the following statements is/are TRUE?
F3(n) = n log2n F4(n) = n log2 n
(a) f O(g) (b) f (g)
(a) f3, f2, f4, f1 (b) f3, f2, f1, f4
(c) f o(g) (d) f (g) (c) f2, f3, f1, f4 (d) f2, f3, f4, f1
6.1
GATE Wallah CS & IT Topic wise PYQs
Algorithms
6.2
GATE Wallah CS & IT Topic wise PYQs
Asymptotic Analysis
6.3
GATE Wallah CS & IT Topic wise PYQs
Algorithms
(a) T(n) = (n log n) (b) T(n) = (n5/2) 23. [MCQ] [GATE-2014 : 1M]
(c) T(n) = (n) (d) T(n) = (logn) 5/2
Which one of the following correctly determines the
solution of the recurrence relation with T(1) = 1?
21. [MCQ] [GATE-2020 : 1M] n
For parameters a and b, both of which are (1), T(n) T(n) = 2T + log n
2
= T(n1/a) + 1, and T(b) = 1. Then T(n) is
(a) (n) (b) (n log n)
(a) Θ(log2 log2 n) (b) Θ(loga logb n)
(c) (n )2
(d) (log)
(c) Θ(logb logan) (d) Θ(logab n)
T(n) =
( )
2T n + 1 n2
(a) n ( log n + 1)
2 0n 2
(b) n log n
Then T(n) in terms of notation is?
(a) (log log n) (b) (log n) (c) n log n
(c) ( n ) (d) (n) (d) n log n
❑❑❑
6.4
GATE Wallah CS & IT Topic wise PYQs
Asymptotic Analysis
1. (a, c) 3. (a)
2
Given f is ‘n’ and g is “n ” we can conclude that f(n2) = ((f(n)2)
f(n) = O(g) that is f(n) can be smaller than or equal
f(n) = n
to O(g).
f(n2) = n2
(f(n))2 = n2
f(n) = 2n, (f(n))2 = (2n)2 = 22n
f(n2) = f ( n 2 ) = 2n ;22n
2
2. (a, d)
n2; 2n.log2
n2 > 2n.
n n n n
f1(n) = n + + + + ... k
2 4 8 2
1
k 1
= n i = n 1 − k 4. (a)
i =0 2 2
n n. log < n3/2 < nlogn < 2n
= n – k = n – 1 = O(n)
2 f3 f 2 f 4 < f1
f1 ( n ) = O ( n ) ;f 2 ( n ) = O ( n )
6.5
GATE Wallah CS & IT Topic wise PYQs
Algorithms
5. (d) 9. (a)
n
10 ; n log n
;n n 1. Find: log n;
2. Insert: N
n.log10 ;(log n)2 ; n.log n
3. Delete: log n
f2 < f3 < f1
4. Decrement key: log n
7. (d)
f(n) = n; g(n) = n1+ sin n 10. (c)
sin value ranges from –1 to +1 (using trichotomy B(n) A(n) W(n)
property) A(n) = O(W(n))
6.6
GATE Wallah CS & IT Topic wise PYQs
Asymptotic Analysis
6.7
GATE Wallah CS & IT Topic wise PYQs
Algorithms
T ( n1/a ) = T ( n1/a ) +1
2
Master method: …(2)
0–
= T ( n1/a ) + 3
3
III. cn is it (n0 + ) fn = 1 …(4)
= T ( n1/a ) + K
K
a. f(n/b) .f(n) satisfied …(5)
cn 1
1⋅ ≤ .cn; 𝛿 = < 1 = T(b) + loga (logbn)
3 3
= 1 + loga . logbn
T(n) is (f(n))
K
(n) n1/a = b
1
.log 2 n = log 2 b
aK
log 2 n
= a K a K = log b n
log 2 b
K = loga logbn
20. (c) T(n) = (loga . logbn)
T(n) = T(n/2) + T(2n/5) + 7n, n > 1
T(n/2) = T(n/4) + T(2n/10) + 7n/2
T(2n/5) = T(2n/10) + T(4n/25) + 7.2n/5
Recursive Tree
22. (b)
T(n) = 2.T ( n ) +1
T(n) = 2.T(n1/2) + 1 …(1)
T(n1/2) = 2.T(n1/4) + 1 …(2)
T(n) = 2[2.T(n1/4) + 1] +1
= 4.T(n1/4) + 3 …(3)
= 22.T ( n1/2 ) + ( 22 – 1)
2
…(3)
2K.T ( n1/2 ) + ( 2K – 1)
K
= …(4)
K
n1/2 = 2
Total time = an + b = O(n) = (n) = (n). 1
.log n = log 2 2
2K
2K = log n
T(n) = logn.T(2) + log n – 1
T(n) = 2.logn + log n – 1
6.8
GATE Wallah CS & IT Topic wise PYQs
Asymptotic Analysis
T(n) = (log n) n
= 21.T + 2 n + 21 n …..(3)
4
1 n n 2
1
= 2 2 T +
2 +2 n
8 2
3 n
23. (a) = 2 2 T + 3 n …..(4)
8
a = 2; b = 2; f(n) = log n
n
log n is it O(n1 – ) , yes, here = 0.5 T ( n ) = 22.T + 4 n …..(5)
16
T(n) is (n) n
= 22 T 2.2 + 2.2 n
2
n
= 2k T 2.k + 2.k n ……(6)
2
n
=1
24. (a) 22k
n = 22k; k ≥ 0 ⇒ n = 22k
n 2k = logn
T(n) = 2 T + n ; T(1) = 1
2 log n
⇒k=
1 n 1 2
T(n) = 2 2 T + n 2 ….(1)
2 1
log n
= ( 2 2 ) .T(1) + 2 .
log n 2
. n
1 2
n 1 n n 2
T
= 2 T +
2 …..(2)
2 4 2 n 1 + n log n
T ( n ) = n (1 + log n )
1
n 1 n n 2
T
= 2 T +
2
4 8 4
1 n 2
1
2 2 T + 1 + n 2
1 n 1
T(n) = 2 2
4 2 2
❑❑❑
6.9
GATE Wallah CS & IT Topic wise PYQs
Design of springs
CHAPTER
2
Basics of Divide and Conquer 3. [MCQ] [GATE-2019 : 2M]
1. [MCQ] [GATE-2021 : 1M] There are n unsorted arrays: A1, A2,..., An. Assume
Let P be an array containing n integers. Let t be the that n is odd. Each of A1, A2, ..., An contains n
lowest upper bound on the number of comparisons distinct elements. There are no common elements
of the array elements, required to find the minimum between any two arrays. The worst-case time
and maximum values in an arbitrary array of n complexity of computing the median of the medians
elements. Which one of the following choices is of A1, A2, ..., An is
correct? (a) O(n log n) (b) O(n2)
n
(a) t > n and t 3 (c) O(n) (d) (n2 logn)
2
(b) t > n n and t 2n – 2 4. [MCQ] [GATE-2008 : 2M]
2 The minimum number of comparisons required to
(c) t > 2n – 2 determine if an integer appears more than n/2 times
in a sorted array of n integers is
(d) t > log2 (n) and t n
(a) Θ (n) (b) Θ (logn)
(c) Θ (log* n) (d) Θ (1)
2. [MCQ] [GATE-2021 : 2M]
For Constants a 1 and b > 1, consider the following
recurrence defined on the non-negative integers: Maximum Subarray Problem
5. [NAT] [GATE-2019 : 1M]
n
T(n) = a.T + f (n) Consider a sequence of 14 elements: A = [–5, –10, 6,
b 3, –1, –2, 13, 4, –9, –1, 4, 12, –3, 0]. The
Which one of the following options is correct about subsequence sum S(i, j) = kj = i A[k] . Determine
the recurrence T(n)?
the maximum of S(i, j), where 0 < i < j < 14. (Divide
(a) If f (n) is (n
log b (a)
) then T(n) is ( n log b (a) ) and conquer approach may be used.)
logb (a)–e
(b) If f (n) is O(n ) for some e 0, then 6. [MCQ] [GATE-2015 : 1M]
T(n) is ( n )
log b (a) An unordered list contains n distinct elements. The
number of comparisons to find an element in this list
n
(c) If f (n) is , then T(n) is (n log 2 (n)) that is neither maximum nor minimum is?
log 2 (n)
(a) (n log n) (b) (n)
(d) If f(n) is nlog2(n), then T(n) is (n log2(n)) (c) (log n) (d) (1)
6.10
GATE Wallah CS & IT Topic wise PYQs
Divide and Conquer
6.11
GATE Wallah CS & IT Topic wise PYQs
Algorithms
6.12
GATE Wallah CS & IT Topic wise PYQs
Divide and Conquer
❑❑❑
6.13
GATE Wallah CS & IT Topic wise PYQs
Algorithms
1. If f (n) = ( n b
log a − )
1. (a) for some constant > 0,
then T(n) = ( n b ) .
log a
n
T ( n ) = 2T + 2, n > 2
2 2. If f(n) = ( n b ) , then
log a
=1, n = 2
T(n) = ( n log b a log n ) .
3n
T(n) = 3. If f(n) = ( n b ) for some constant > 0,
–2 log a + c
2
and if f(n/b) cf(n) for some constant c < 1 and
all sufficiently large n, then T(n) = (f(n)).
2. (b) 3. (b)
According to standard case of master’s theorem • To compute the median of an array (n) = O(n)
option (b) is correct. • To compute All medians of n – array(n)
= n × O(n)
Master Theorem = O(n2)
Let a 1 and b > 1 be constants, let f(n) be a • To compute median of all n-medians = O(n)
function, and let T(n) be defined on the non-
negative integers by the recurrence
( )
Total time =O n 2 + O ( n ) = O n 2 ( )
T(n) = aT(n/b) + f(n),
where we interpret n/b to mean either n/b or n/b.
Then T(n) has the following asymptotic bounds:
4. (c)
6.14
GATE Wallah CS & IT Topic wise PYQs
Divide and Conquer
8. (b)
a1 a 2 a
[a0, a1 …..an–1] x = a0 + + + ...... + n–1
2 4 2n–1
5. (29 to 29) a a a
[a1, a2 …..an–1] y = a1 + 2 + 3 + ...... + n–1
[Maximum sum sub-array] 2 4 2n–1
[By Kadane’s algorithm] y
X = max y, a 0 +
S(2, 11) = (6 + 3 – 1 – 2 + 13 + 4 – 9 – 1 + 4 + 12) 2
= 29 a a a
a 0 + 1 + 2 + .... + n–1
2 4 2n–1
a1 a 2 4
a0 + + + .... a 0 +
2 4 2
6. (d)
1 2 3 4 5 6
A: 10 5 8 12 3 9
Consider first three elements: 10, 5, 8 from the 9. (0.08 to 0.08)
array, the element that is neither maximum nor In the worst case the pivot can get fixed up at
minimum is 8. The time required to find it requires either the first or last position;
only three comparisons that is constant time
1 1 2
complexity. ∴ Probability = + = = 0.08
25 25 25
7. (148 to 148)
10. (b)
6.15
GATE Wallah CS & IT Topic wise PYQs
Algorithms
14. (b)
In the given scenario the list is divided into two
sublists with n/5 and 4n/5 elements and n is
common so the answer is T(n) ≤ T (n/5) + T (4n/5)
11. (a) + n.
The pivot can get fixed up at the first or last place
in the list at all levels leading to the worst case
behaviour = O(n2).
15. (b)
30 sec → 64 elements
6 minutes → n = ?
12. (c) Time for merge sort = n∙logn
1 2 3 4 5 = 64∙log64
t1 is O(n2) 64.6 units
Sorted list
30 sec – 64.6 units
t1 ? – 1 unit
30
4 1 5 3 2 1 unit = s
64 6
t2 is O(n log n)
Unsorted list 30
1 unit = s
t2 64 6
(n) ? – 360 s
O(n2) > O(n log n)
12
360 64 6
unit ___'n 'elements
30
12 6 64 ____ n log2 n
4608 = n∙logn
n 3n
T(n) = O(n) + O(n) + T + T
4 4
By recursion tree method = Θ(nlogn)
6.16
GATE Wallah CS & IT Topic wise PYQs
Divide and Conquer
16. (b)
Merg sort (n) = n log n
(n log n) n = n2 log n
a b x y
p q r t 20. (b)
l m c d In selection sort the maximum number of swaps can
y z k q be of order of n that is selecting the smallest element
among all other elements and placing it to the
correct position.
17. (d)
Quicksort using the last element as pivot = n2
21. (a)
Selection Sort = n2
Mergesort = n log n Considering the worst case, there will be one swap
Insertion sort = n in each loop except the last one, that is n-1 swaps
for 1 to n. Hence the number of swaps in the worst
case is (n).
18. (d)
• For insertion sort, we need to run two loops
both of order n, so worst case time complexity
is (n2). 22. (b)
Heap with ‘n’ element
• Merge sort takes (n log n) in all cases.
• Quick sort takes (n2) in worst case if the
array is sorted or almost sorted.
19. (d)
• If the input is already sorted then running
quick sort will give worst case time complexity
of (n2).
• Bubble sort runs in constant time complexity.
• Merge sort runs in (n log n).
• Insertion sort will run in (n) time only.
6.17
GATE Wallah CS & IT Topic wise PYQs
Algorithms
6.18
GATE Wallah CS & IT Topic wise PYQs
Divide and Conquer
log n log n
Time = log
log log n log log n
log n
A: 40, 35, 20, 10, 30, 16, 17, 8, 4, 15 = log n – log log log n = ( log n )
log log n
30. (b)
The max heap property is only satisfied by option
(b), as in option (a) the heap is not a complete binary
tree or an almost complete binary tree.
In option (c) max heap property is not satisfied as
node value 8 is the leaf node and a child of a value
that is smaller than itself, that is 5.
In option (d) max heap property is violated as node
value 10 is in leaf node and a child of a value that is
smaller than itself.
However, in option (b) the max heap property is
satisfied.
❑❑❑
6.19
GATE Wallah CS & IT Topic wise PYQs
Design of springs
CHAPTER
3
Basics of Greedy Character Probability
1. [NAT] [GATE-2014 : 2M]
P 0.22
Suppose P, Q, R, S, T are sorted sequences having
lengths 20, 24, 30, 35, 50 respectively. They are to Q 0.34
be merged into a single sequence by merging R 0.17
together two sequences at a time. The number of
comparisons that will be needed in the worst case by S 0.19
the optimal algorithm for doing this is______.
T 0.08
Total 1.00
Huffman Code Problem
2. [MSQ] [GATE-2021: 2M] If a message of 100 characters over X is encoded
using Huffman coding, then the expected length of
Consider the string abbccddeee. Each letter in the
the encoded message in bits is _____.
string must be assigned a binary code satisfying the
following properties:
Kruskal's Algorithm
1. For any two letters, the code assigned to one
letter must not be a prefix of the code assigned 4. [NAT] [GATE-2015 : 2M]
to the other letter. The graph shown below has 8 edges with distinct
2. For any two letters of the same frequency, the integer edge weights. The minimum spanning tree
letter which occurs earlier in the dictionary order (MST) is of weight 36 and contains the edges: {(A,
is assigned a code whose length is at most the C), (B, C), (B, E), (E, F), (D, F)}. The edge weights
length of the code assigned to the other letter. of only those edges which are in the MST are given
Among the set of all binary code assignments which in the figure shown below. The minimum possible
satisfy the above two properties, what is the sum of weights of all 8 edges of this graph is
minimum length of the encoded string? ________.
(a) 23 (b) 25
(c) 21 (d) 30
6.20
GATE Wallah CS & IT Topic wise PYQs
Greedy Techniques
6.21
GATE Wallah CS & IT Topic wise PYQs
Algorithms
❑❑❑
6.22
GATE Wallah CS & IT Topic wise PYQs
Greedy Techniques
1. (358 to 358)
L1: n L2: m: The number of comparisons needed
to merge them into a single sorted list = (n + m – 1)
P, Q, R, S, T = 20, 24, 30, 35, 50
<abbccddeee> : 3 + (3 + 3) + (2 + 2) + (2 + 2) +
(2 + 2 + 2)
a = 100 (3)
Total comparisons: 158 + 93 + 64 + 43 = 358.
b = 101 (3)
c = 00 (2)
d = 01 (2)
e = 11 (2)
2. (a)
<abbccddeee> : 10010110100000101111111.
a→1
b→2
c→2
d→2
e→3
6.23
GATE Wallah CS & IT Topic wise PYQs
Algorithms
6. (a)
P → 01 (2 bits)
Q → 11 (2 bits) For the above graph statement, I follow but for the
same graph statement II fails. The shortest path
R → 101 (3 bits) between node 2 and node 3 is of weight 3 but it is
S → 00 (2 bits) not unique. There are two paths:
T → 100 (3 bits) • Node 2 to Node 3 via Node 1.
• Node 2 to Node 3.
Average number of bits
= in= 1 di.qi
char
= 2 * 0.22 + 2 * 0.34 + 3 * 0.17 + 2 * 0.19 + 3 * 0.08
= 0.44 + 0.68 + 0.51 + 0.38 + 0.24 = 2.25
7. (99 to 99)
Number of bits for 100 char’s = 100 × 2.25 = 225.
1 1 1 1 1
V1 ⎯⎯
→ V2 ⎯⎯
→ V3 ⎯⎯
→ V4 ⎯⎯
→....V99 ⎯⎯
→ V100
Number of edges needed = 99 × 1 = 99.
4. (69 to 69)
8. (4 to 4)
If x = 5, we can have 2 possible options
6.24
GATE Wallah CS & IT Topic wise PYQs
Greedy Techniques
9. (a)
11. (b)
Given that V = a, b, c, d and E = ab, bc, cd, da, ac,
let G = (V, E) be a graph.
10. (7 to 7)
6.25
GATE Wallah CS & IT Topic wise PYQs
Algorithms
13. (d)
{a, b, e} – 1 3 –2 0
{a, b, e, f} – 1 3 –2 0 3
12. (d)
A Tree having ‘n’ vertices will always have (n – 1) {a, b, e, f, g} – 1 3 –2 0 3 5
edges.
|V| = |E| + 1 {a, b, e, f, g, c} – 1 3 6 –2 0 3 5
{a, b, e, f, g, c, h} – 1 3 6 –2 0 3 5
Thus, the above graph computes the correct shorest
path distance two all the vertices.
G T
❑❑❑
6.26
GATE Wallah CS & IT Topic wise PYQs
Design of springs
CHAPTER
4
Basics of Graph (a) for every f: V → R
1. [MCQ] [GATE-2012 : 2M] (b) if and only if ∀u V, f(u) is positive
Consider the directed graph shown in the figure
(c) if and only if ∀u V, f(u) is negative
below. There are multiple shortest paths between
vertices S and T. Which one will be reported by (d) if and ony if f(u) is the distance from s to u in the
Dijkstra’s shortest path algorithm? Assume that, in graph obtained by adding a new vertex s to G
any iteration, the shortest path to a vertex v is and edges of zero weights from s to every vertex
updated only when a strictly shorter path to v is of G.
discovered
3. [NAT] [GATE-2016: 2M]
Consider the weighted undirected graph given with
4 vertices, where the weight of edge {i, j} is given
0 2 8 5
2 0 5 8
by the entry Wij in the W =
8 5 0 x
5 8 x 0
The largest possibel integer value of x, for which at
least one shrotest path between some pair of vertices
(a) SDT (b) SBDT will contian the edge with weights x is ___.
(c) SACDT (d) SACET
Common data for next two Question
Single Source Shortest Path An undirected graph G(V, E) contains n(n > 2) nodes
2. [MCQ] [GATE-2020 : 1M] named v1, v2, ..., vn. Two nodes vi, vj are connected if and
Let G = (V, E) be a directed, weighted graph with only if 0 < |i – j|< 2. Each edge (vi, vj) is assigned a weight
weight function w: E → R. For some function f: i + j. A sample graph with n = 4 is shown below.
6.27
GATE Wallah CS & IT Topic wise PYQs
Algorithms
6.28
GATE Wallah CS & IT Topic wise PYQs
Graph Based Algorithm
M N O
6.29
GATE Wallah CS & IT Topic wise PYQs
Algorithms
a e f (a) MNOPQR
(b) NQMPOR
d (c) QMNPRO
(d) QMNPOR
g
❑❑❑
6.30
GATE Wallah CS & IT Topic wise PYQs
Graph Based Algorithm
1. (d) 2. (a)
3. (12 to 12)
S→A →C→E →T
6.31
GATE Wallah CS & IT Topic wise PYQs
Algorithms
4. (b)
n=4
n=3 n=4 n=5 ‘n’
:3 + 4 + 6 :3 + 4 + 6 + 8 :3 + 2 (2 + 3
:3 + 2 (2 + :3 + 2 (2 + 3 + 4 +….+ n –
3) + 4) 1)
3+4
n2 – n – 2
: 3 + 2 n (
n – 1)
– 1 = 3 + 2
2 2
The weight of minimum spanning tree is n2 – n + 1.
6.32
GATE Wallah CS & IT Topic wise PYQs
Graph Based Algorithm
7. (d) 1 – 0 – 4 – 2 =1+ 4 + 3 = 8
0 1 2 3 4
Minimum possible weight of a path P from vertex
0 0 1 8 1 4 1 to vertex 2 is 8.
1 1 0 12 4 9
W=
2 8 12 0 7 3
3 1 4 7 0 2
4 4 9 3 2 0
9. (5040 to 5040)
( x, y, z )
y, z, x
1 2 3 4 z, x, y
3!
x, z, y
1 0 12 4 9
y, x, z
2 12 0 7 3 z, y, x
3 4 7 0 2 U = {1, 2, 3,}; 24 = V = {, {1}, {2}, {1, 2},
{1, 3}, {2, 3}, {1, 2, 3}}
4 9 3 2 0 B() = 71
= 5040
Vertex set 0 1 2 3 4
{1, 0} 1 – 9 2 5
{1, 0, 3} 1 – 9 2 4 {1,2,3}
{1, 0, 3, 4} 1 – 8 2 4
Dijkstra’s algorithm
1–0–3–2=1+1+7=9
6.33
GATE Wallah CS & IT Topic wise PYQs
Algorithms
k y 12. (a)
K • No edge of G is a cross edge with respect to TD.
(A cross edge in G is between two nodes neither
l x u d l of which is an ancestor of the other in T D).
TRUE
b c
x • For every edge (u, υ) of G, if u is at depth i and
υ is at depth j in TB, then |i –j| = 1. FALSE
y d
11. (1 to 1)
13. (d)
Using breadth first search algorithm the order of
visiting nodes will be: POQNMR.
6.34
GATE Wallah CS & IT Topic wise PYQs
Graph Based Algorithm
14. (6 to 6)
a_, _,_,_f
4! 4 3 2
= = =6
( 2!)( 2!) 2 2
s s 1
3 3
u v u v
x 4 x
15. (31 to 31)
G T1
s s 1
: 21+1 –1 = 3 3 2
: 22 –1 = 3. u x u r
6.35
GATE Wallah CS & IT Topic wise PYQs
Algorithms
18. (c)
A=
n×n
( n2 )
For an adjacency matrix the DFS algorithm will take 21. (b)
Θ(n2).
19. (b)
Shortest paths (unit distance) from ‘w’ to all other The possible outputs are: a b e f c g d
vertices in the graph
The topological orderings PSRQ and SPRQ are The possible order of visiting the node is
identical. QMNPRO.
Select P or S as the starting vertices for DFS.
As soon as the vertex has finished, add it to the head
of a linked list.
The needed topological ordering is the linked list.
❑❑❑
6.36
GATE Wallah CS & IT Topic wise PYQs
Design of springs
CHAPTER
5
Basics of Dynamic Programming 5. [MCQ] [GATE-2011 : 1M]
1. [NAT] [GATE-2014: 2M] An algorithm to find the length of the longest
Consider two strings A = “qpqri”B = “pqprqrp”. Let monotonically increasing sequence of number in an
x be the length of the longest common subsequence array A[0: n – 1] is given below.
(not necessarily contiguous) between A and B and let Let Li denotes the length of the longest
be the number of such longest common subsequences monotonicaly increasing sequence starting at index
between A and B.Then x + 10 y = _____. i in the array
Initialize Ln–1 = 1
2. [MCQ] [GATE-2013 : 1M] For all i such that 0 ≤ i ≤ n – 2
What is the time complexity of Bellman-Ford single-
1 + L i +1 If A i A i + 1
source shortest path algorithm on a complete graph Li =
of n vertices? 1 otherwise
(a) Θ(n2) (b) Θ(n2 log n) Finally the length of the longest monotonically
(c) Θ(n3) (d) Θ(n3 log n) increasing sequcnce is Max(L0, L1, …..Ln–1).
Which of the followign statements is TURE?
3. [MCQ] [GATE-2012 : 1M] (a) The alogrithm uses dynamic programming
The recurrence relation capturing the optimal paradigm.
execution time of the Tower of Hanoi problem with
(b) The alogrithm has a linear complexity and uses
n discs is
branch and bound paradigm
(a) T(n) = 2T(n – 2) + 2 (b) T(n) = 2T(n – 1) + n
(c) The algorithm has non-linear polynomial
(c) T(n) = 2T(n/2) + 1 (d) T(n) = 2T(n – 1) + 1
4. [MSQ] [GATE-2011 : 2M] complexity and uses brach and bound paradigm.
Define Rn to be the maximum amount earned by (d) The algorithm uses divide and conquer paradigm
cutting a rod of length n meters into one or more
pieces of integer length and selling them. For i > 0, 6. [MCQ] [GATE-2009 : 1M]
let p[i] denote the selling price of a rod whose length Which of the following statement(s) is / are correct
is i meters. Consider the array of prices: regarding Bellman-Ford shortest path algorithm?
p[1] = 1, p[2] = 5, p[3] = 8, p[4] = 9, P: Always finds a negative weighted cycle, if one
p[5] = 10, p[6] = 17, p[7] = 18 exists.
Which of the following statements is/are correct Q: Finds whether any negative weighted cycle is
about R7? reachable from the source.
(a) R7 = 18 (a) P only
(b) R7 = 19
(b) Q only
(c) R7 is achieved by three different solutions.
(c) both P and Q
(d) R7 cannot be achieved by a solution consisting
(d) Neither P nor Q
of three pieces.
6.37
GATE Wallah CS & IT Topic wise PYQs
Algorithms
6.38
GATE Wallah CS & IT Topic wise PYQs
Dynamic Programming
(d) The subset sum problem in NP−hard. The value of Vopt – Vgreedy is _______.
6.39
GATE Wallah CS & IT Topic wise PYQs
Algorithms
(a) F1F2 and F3 F4 only (b) F2F3 only in several ways with different number of total scalar
(c) F3F4 only (d) F1F2 and F4F5 only multiplications. For example when multiplied as
((M1 × M2) × (M3 × M4)), the total number of scalar
17. [NAT] [GATE-2016 : 2M]
multiplications is pqr + rst + prt. When multiplied as
Let A1, A2, A3, and A4 be four matrices of
(((M1 × M2) × M3) × M4), the total number of scalar
dimensions 10 × 5, 5 × 20, 20 × 10, and 10 × 5,
multiplications is pqr + prs + pst. If p = 10, q = 100,
respectively. The minimum number of scaler
r = 20, s = 5 and t = 80, then the minimum number
multiplication required to find the product
of scalar multiplications needed is?
A1A2A3A4 using the basic matrix multiplication
method is_________. (a) 248000
18. [MCQ] GATE-2011 : 2M] (b) 44000
Four matrices M1, M2, M3 and M4 of dimensions p × (c) 19000
q, q × r, r × s and s × t respectively can be multiplied (d) 25000
❑❑❑
6.40
GATE Wallah CS & IT Topic wise PYQs
Dynamic Programming
1. (34 to 34)
3. (d)
T ( n ) = 2.T ( n–1) + C
= 2.T ( n–1) + 1
6.41
GATE Wallah CS & IT Topic wise PYQs
Algorithms
4. (a, c) 7. (c)
P[1] = 1; P[2] = 5; P[3] = 8; P[4] = 9; P[5] = 10; When the currently compared elements doesn't
P[6] = 17; P[7] = 18; match, we have two possibilities for the LCS, one
including Y[j] but not X[i] and including X[i] but
No. of Combination Selling not Y[j].
pieces Price
1 1(7) 18
2 2(6, 1) 18 Max (R7)
3 3(2, 2, 3) 18 8. (b)
4 4(2, 2, 2,1) 16 The previously discovered LCS is saved using
dynamic programming. Therefore, all smaller ones
5 5(2, 2, 1, 1, 1) 13 for any index should have been computed earlier.
6 6(2, 1, 1, 1, 1, 1) 10
7 7(1, 1, 1, 1, 1, 1, 1) 7
9. (b)
{ T(n) = c, n = 0,
if(n = = 0||n = = 1) return n; 1
else = T(n–1) + T(n–
5. (a) return (2* f1(n – 1) + 3* f1 (n – 2)
2)); + a, n>1
The algorithm storts the optimal solutions to
subproblems at every point, and then uses it to } = T(n) = O(2n)
derive the optimal solution for a bigger problem and
which is dynamic programming approach. The
{
program runs in linear time complexity.
int i;
int X[N], Y[N], Z[N];
X[0] = Y[0] Z [0] = 0;
X[1] = 1; Y[1] = 2; Z[1] = 3;
T(n) = O(n)
for (i = 1; i ≤n; i ++)
6. (b)
X[i] = Y[i – 1] + Z [i – 2];
The Bellman-Ford algorithm may not always find Y[i] = 2* X[i];
the negative weighted cycle. However, it finds any
Z[i] = 3*X[i];
negative weighted cycle that is reachable from the
}
given source.
The running time of f1(n) is O(2n) and f2(n) is O(n).
6.42
GATE Wallah CS & IT Topic wise PYQs
Dynamic Programming
10. (c)
X[0 …. n, …..]
X[1 …. n, …..]
Data set: n = 5; S: 2, 8, 4, 11, 9; W = 6
12. (b)
Subset of Sum is a NP Complete problem.
Subset sum problem NP
Subset sum problem is NP-hard problem.
6.43
GATE Wallah CS & IT Topic wise PYQs
Algorithms
1 10 60 6
2 7 28 4
3 4 20 5
4 2 24 12
5 * 20 * 5 + 0 + 20 * 10 * 5 = 1500.
Greedy approach:
Weight 2 + 4
⸫ Value = 44
VGreedy = 44
18. (c)
Vopt – VGreedy = 60 – 44 = 16.
M1 = P × Q, M2 = Q × R, M3 = R × S
M4 = S × T, P = 10; Q = 100;
R = 20;
S = 5;
T = 80;
16. (c)
( F ( F ( F F )( F ) )
1 2 3 4 5
❑❑❑
6.44
GATE Wallah CS & IT Topic wise PYQs
Design of springs
CHAPTER
6
Graph 3. [MCQ] [GATE-2021 : 1M]
1. [MSQ] [GATE-2022: 2M] Let G be a connected undirected weigthed graph.
Consider a simple undirected weighted graph G, all Consider the following two statements.
of whose edge weights are distinct. Which of the S1: There exists a minimum weight edge in G which
following statements about the minimum spanning is present in every minimum spanning tree of G.
trees of G is/are TRUE? S2: If every edge in G has distinct weight, then G has
a unique minimum spanning treed.
(a) The edge with the second smallest weight is
always part of any minimum spanning tree of G. Which one of the following options is correct?
(a) S1 is false and S2 is true
(b) One or both of the edges with the third smallest
(b) Both S1 and S2 are flase
and the fourth smallest weights are part of any
(c) S1 is true and S2 is false
minimum spanning tree of G .
(d) Both S1 and S2 are true.
(c) Suppose S V be such that S ≠ ϕ and S ≠ V .
Consider the edge with the minimum weight 4. [MSQ] [GATE-2021: 2M]
such that one of its vertices is in S and the other Consider the following directed graph:
in V \ S. Such an edge will always be part of any Which of the following is/are correct about the
minimum spanning tree of G . graph?
(d) G can have multiple minimum spanning trees.
6.45
GATE Wallah CS & IT Topic wise PYQs
Algorithms
6.46
GATE Wallah CS & IT Topic wise PYQs
Miscellaneous Topics
is the time complexity of the most efficient algorithm 14. [NAT] [GATE-2015 : 2M]
to set the twin pointer in each entry in each adjacency Let G be a connected undirected graph of 100
list? vertices and 300 edges. The weight of a minimum
(a) Θ(n2) (b) Θ(n + m) spanning tree of G is 500. When the weight of each
edge of G is increased by five, the weight of a
(c) Θ(m2) (d) Θ(n4)
minimum spanning tree becomes______.
6.47
GATE Wallah CS & IT Topic wise PYQs
Algorithms
❑❑❑
6.48
GATE Wallah CS & IT Topic wise PYQs
Miscellaneous Topics
1. (a, b, c)
n=4
6.49
GATE Wallah CS & IT Topic wise PYQs
Algorithms
3. (a)
The Kruskals algorithm can be used to discover the
Minimum Spanning Tree on a graph G.
While doing that, we sort the edges according to
their weight and begin choosing the smallest edge.
5. (929 to 929)
Problem with S1: The Kruskals method may not
always choose a particular weighted edge if there Let q(s) denote the quality score of v, then:
are numerous copies of it. q(s) = 1; q(d) = 9; q(b) = 9;
S2 is True, the Kruskals method will always choose q(a) = 9; q(f) = 9; q(c) = 81;
a distinct set of edges, resulting in a distinct minimal q(c) = 1; q(g) = 81; q(t) = 729;
spanning tree, if the sorted order of the edges 9
4. (a, b)
6. (3 to 3)
7. (c)
List-I List -II
A: Prim’s algrithm for 2. Greedy
minimum spanning tree Method
B: Floyd-Warshall algorithm 3. Dynamic
for all pairs shortest paths programming
C: Mergesort 4. Divide and
conquer
D: Hamiltonian circuit 1. Backtraking
6.50
GATE Wallah CS & IT Topic wise PYQs
Miscellaneous Topics
8. (c)
List-I List-II
A Dijkstra’s Shortest 3. Greedy Design
path
B Floyd-Warshall 2. Dynamic 10. (c)
algorithm to compute programming The connected components of an undirected graph
all pairs shortest can be found by applying DFS in (n + m).
path.
C Binary search on a 1. Divide and
sorted array Conquer
D Backtracking search 4. Depth-first
on a graph search
11. (b)
To, insert one element into an AVL – tree(n) = log n
Total time = (n2.logn) to insert n2 elements.
9. (b)
Using BFS, you can get all twin pts
1, 2
12. (c)
1, 3
2, 1 • Time of find ‘a’ and
‘b’ in binary search
2, 3
tree
= O(logn)
• Time of sum up
elements between a, b
in binary search tree,
using O(k) inorder sorting.
Total time = O ( log n + k )
13. (a)
For both insertion and deletion the worst case time
complexity in a binary search tree is (logn).
6.51
GATE Wallah CS & IT Topic wise PYQs
Algorithms
15. (a)
18. (c)
Time = log(n.2n) = log n + log 2n
T(n) = (log n + n) = O(n) = (n) = (n).
19. (c)
Tower of Hanoi with n disks takes (2n)
Binary search given n sorted numbers takes (log n)
Heap sort given n numbers at the worst case takes
16. (110 to 110)
(n.log n)
1. To locate ‘L” and ‘H’ in Binary Search Tree,
Addition to two n × n matrices takes (n2)
time taken = O(log n)
2. Time to sum up all numbers between L and H,
using
In order sorting will be O(m)
Total time: O(log n + m)
20. (c)
a = 0; b = 1
Time of radix sort: O(d(n +b))
c = 1; d = 0
d: Number of bits in the representation of highest
(10 + 100 + 0) = 110 number.
n: number of elements
b: base (radix)
d = lognk = k.logn
= k.logn.n + k.logn.b
= Θ(n.logn).
❑❑❑
6.52
GATE Wallah CS & IT Topic wise PYQs