0% found this document useful (0 votes)
11 views54 pages

Gate Cse

Uploaded by

Arun Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views54 pages

Gate Cse

Uploaded by

Arun Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

1. Asymptotic Analysis................................................................................................................... 6.1 – 6.

2. Divide and Conquer ................................................................................................................... 6.10 – 6.19

3. Greedy Techniques.................................................................................................................... 6.20 – 6.26

4. Graph Based Algorithm ............................................................................................................. 6.27 – 6.36

5. Dynamic Programming .............................................................................................................. 6.37 – 6.44

6. Miscellaneous Topics ................................................................................................................ 6.45 – 6.52


Design Against Static Load

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 wise Weightage Analysis

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

2. [MSQ] [GATE-2023 : 2M] 5. [MCQ] [GATE-2021 : 2M]


Consider functions Function_1 and Function_2
Consider the following three functions:
expressed in pseudocode as follows:
n
Function_1 Function_2 f1 = 10n, f2 = n(logn, f3 = n
while n > 1 do for i = 1 to 100 ∗ n Which one of the following options arranges the
for i = 1 to n do do functions in the increasing order of asymptotic
x = x + 1; x = x + 1; growth rate?
end for end for (a) f3, f2, f1 (b) f2, f1, f3
n = n/2 (c) f1, f2, f3 (d) f2, f3, f1
end while
Let f1(n) and f2(n) denote the number of times the 6. [MCQ] [GATE-2017 : 1M]
Consider the following functions from positive
statement “x = x + 1” is executed in Function_1 and
integers to real numbers:
Function_2, respectively.
100
Which of the following statement is/are TRUE? 10, n , n, log 2 n,
n
(a) f1(n)  (f2(n)) (b) f1(n)  o(f2(n))
The CORRECT arrangement of the above functions
(c) f1(n)  (f2(n)) (d) f1(n)  O(n)
in increasing order of asymptotic complexity is:
100
3. [MCQ] [GATE-2022 : 1M] (a) log 2 n, ,10, n , n
n
Which one of the following statements is TRUE for 100
all positive functions f(n)? (b) ,10, log 2 n, n , n
n
(a) f(n2) = (f(n)2), when 𝑓(𝑛) is a polynomial. 100
(b) f(n2) = o(f(n)2) (c) 10, , n , log 2 n, n
n
(c) f(n2) = O(f(n)2), when f(n) is an exponential 100
function. (d) , log 2 n,10, n , n
n
(d) f(n2) = (f(n)2)

6.1
GATE Wallah CS & IT Topic wise PYQs
Algorithms

7. [MCQ] [GATE-2015 : 2M] (a) A(n) = (W(n))} (b) A(n) =  (W(n))


(1 + sin n)
Let f(n) = n and g(n) = n , where n is a positive (c) A(n) = O(W(n)) (d) A(n) = o(W(n))"
integer. Which of the following statements is/are
correct?
11. [MCQ] [GATE-2008 : 2M]
I: f(n) = O(g(n)) II: f(n) = (g(n))
Consider the following functions:
(a) Only I (b) Only II • f(n) = 2n
(c) Both I and II (d) Neither I nor II • f(n) n!
• h(n) = nlogn
8. [MCQ] [GATE-2015 : 1M] Which of the following statements about the
asymptotic behaviour of f(n), g(n), and h(n) is true?
Consider the equality in= 0 i3 = X and the following
(a) f(n) = O(g(n)); g(n) = O(h(n))
choices for X.
(b) f(n) = Ω(g(n)); g(n) = O(h(n))
I: (n4) II: (n5)
(c) g(n) = O(f(n)); h(n) = O(f(n))
III: O(n5) IV: (n4)
The equality above remains correct if X is replaced (d) h(n) = O(f(n)); g(n) = Ω (f(n))
by
(a) Only I 12. [MCQ] [GATE-2008 : 2M]
(b) Only II Arrange the followign functions in increasing
(c) I or III or IV but not II asymptotic order:
(d) II or III or IV but not I A. n1/3 B. en C. n7/4
9 n
D. nlog n E. 1.0000001
(a) A, D, C, E, B (b) D, A, C, E, B
9. [MCQ] [GATE-2015 : 2M]
(c) A, C, D, E, B (d) A, C, D, B, E
An algorithm performs (logN)1/2 find operations, N
insert operation, (logN)1/2 delete operations, and
(logN)1/2 decrease-key operations on a set of data Finding Time Complexity
items with keys drawn from a linearly ordered set. 13. [MCQ] [GATE-2020 : 1M]
For a delete operation, a pointer is provided to the What is the worst-case time complexity of inserting
record that must be deleted. For the decrease-key n elements into an empty linked list, if the linked list
operation, a pointer is provided to the record that has needs to be maintained in sorted order?
its key decreased. Which one of the following data (a) (n log n) (b) (n)
structures is the most suited for the algorithm to use, (c) (1) (d) (n)2
if the goal is to achieve the best total asymptotic
complexity considering all the operations?
14. [MCQ] [GATE-2017 : 1M]
(a) Unsorted array
Consider the following C function.
(b) Min – heap
int fun (int n)
(c) Sorted array
{ int i, j;
(d) Sorted doubly linked list
for (i = 1; i < = n; i ++){
for (j = 1; j < n; j + = i)
10. [MCQ] [GATE-2012 : 1M] {
Let W(n) and A(n) denote respectively, the worst printf{“%d %d”, i, j);
case and average case running time of an algorithm }
executed on an input of size n. Which of the }
following is ALWAYS TRUE? }

6.2
GATE Wallah CS & IT Topic wise PYQs
Asymptotic Analysis

Time complexity of fun in terms of  notation is 17. [MCQ] [GATE-2013 : 2M]


Consider the following function:
(a) ( n n ) (b) (n2)
int unknown (int n)
(c) (n log n) (d) (n2 log n) {
int i, j, k = 0;
15. [NAT] [GATE-2016 : 2M] for (i = n/2; i < = n; i++)
The given diagram shows the flowchart for a for(j = 2; j < = n; j = j*2)
recursive function A(n). Assume that all statements, k = k + n/2;
except for the recursive calls, have O(1) time return(k);
complexity. If the worst-case time complexity of this }
function is O(n), then the least possible value The return value of the function is ___.
(accurate up to two decimal positions) of  is _____. (a) (n2) (b) (n2 log n)
Flow chart for Recursive Function A(n) (c) (n)3 (d) (n3 log n)
Start
18. [MCQ] [GATE-2010 : 1M]
A(n/2)
Two alternative packages A and B are available for
processing a database having 10k records. Package
Return A(n/2) A(n/2) A(n/2) Return A requires 0.0001n2 time units and package B
requires 10nlog10n time units to process n records.
what is the smallest value of k for which package B
A(n/2) will be preferred over A?
(a) 12 (b) 10
Return A( n/2) Return (c) 6 (d) 5

16. [MCQ] [GATE-2015 : 2M] 19. [MCQ] [GATE-2009 : 2M]


Consider the following C function The running time of an algorithm is represented by
int fun1 (int n) the following recurrence relation:
{   n n3
 n
int i, j, k, p, q = 0; T(n) =  T   + cm
  3 otherwise
for (i = 1; i < n; ++i) 

{ Which of the following represents the time
p = 0; complexity of the algorithm?
for (j = n; j > 1; j = j /2) (a) (n) (b) (n log n)
++p; (c) (n) 2
(d) (n2 log n)
for (k =1; k < p; k = k*2)
++q; Solving Recurrence Relation
} 20. [MCQ] [GATE-2021 : 2M]
return q; Consider the following recurrence relation:
}   n
 T   + T  2n  + 7n if n  0
Which one of the following most closely 
T(n) =   2   
 5
approximates the return value of the function fun1? 
 1 if n = 0
(a) n3 (b) n(log n)2 
(c) n log n (d) n log (log n) Which one of the following options is correct?

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)

24. [MCQ] [GATE-2008 : 2M]


22. [MCQ] [GATE-2017 : 1M] 2k
When n = 2 for some k ≥ 0, the recurrence relation
Consider the recurrence function T(n) 2T ( n / 2 ) + n ,T(1) = 1 evaluated to:

T(n) = 

( )
 2T n + 1 n2
(a) n ( log n + 1)
 2 0n 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) 2. (a, d) 3. (a) 4. (a)


5. (d) 6. (b) 7. (d) 8. (c)
9. (a) 10. (c) 11. (d) 12. (a)
13. (d) 14. (c) 15. (2.2 to 2.4) 16. (d)
17. (b) 18. (c) 19. (a) 20. (c)
21. (b) 22. (b) 23. (a) 24. (a)

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

Data FIND INSERT DELETE DEC Total


Structur – KEY time
e
Unsorted N. log n N log n log n N. log n
6. (b) Array
3/2
Min- N. log n N.log N (log n) (log n)3/2 N.log N
n grows faster than log and slower than linear
Heap
100
whereas n grows linearly and decreases with Sorted (log n)3/2 N2 N. log n N. log n N2
n
Array
n.
Sorted N. log n N2 log n N. log n N2
D.L.L

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

8. (c) 11. (d)


 n(n + 1) 2 2n nn n log n
3i =  

 2  n.log 2 2 n log n (log n) 2
= O(n4)
n n log n (log n) 2
= (n4)
Therefore: h  f  g
= (n4)

6.6
GATE Wallah CS & IT Topic wise PYQs
Asymptotic Analysis

12. (a) 15. (2.2 to 2.4)


For the given scenario we can observe that: T(n) = 5.T(n/2) + C
• n1/3 < n7/4 and n1/3 < nlog9n First check C is it O ( n log2 5 − ) , yes
• 1.0000001n < en and 1.0000001n < n7/4
 T(n)is  ( n log 2 5 ) =  ( n 2.32 )
• nlog9n < 1.0000001n
• n7/4 is asymptotically larger than nlog9n. (If we
take n common from both the functions then n3/4
is larger than log9n).
• Thus, n1/3 < nlog9n < n7/4 < 1.0000001n < en.
16. (d)
[log logn + log logn + … +]n
return(q) = n.log logn
for (i = 1; i < n; ++i): n
for (j = n; j > 1; j = j /2): log n
for (k =1; k < p; k = k*2): log p(log log n)
13. (d)
Total comparision’s: (1 + 2 + 3 + … + n – 1)
n(n − 1)
:
2
: O(n2) 17. (b)
: (n2) n
• for (i = n/2; i < = n; i++) :
:  (n )2
2
• for(j = 2; j < = n; j = j*2)
n
k = k + n/2; : log n
2
n  n 
Overall Complexity:  .log n  .
2  2 
14. (c)
i = 1, n, +1
j =1, n, +i
i = 1, i = 2, i = 3
n n 18. (c)
j = 1, n, j = , j=
2 3 10.n.log10n < 0.0001n2
n n n
n+ + + + 10.10k .k<10−4 .10 2 k
2 3 n
 1 10k 106
n  nx = 1  k k=6 6  10
 x 105 105
T(n) = n.logn = (n.logn) k=6

6.7
GATE Wallah CS & IT Topic wise PYQs
Algorithms

19. (a) 21. (b)


T(n) = T(n/3) + cn T(n) = T(n1/a) + 1; T(b) = 1
a = 1; b = 3; f(n) = cn T(n) = T(n1/a) + 1 …(1)

T ( n1/a ) = T ( n1/a ) +1
2
Master method: …(2)
0–

T(n) = T ( n1/a )+2


I. cn is O(n ), dissatified 2
…(3)
II. cn is (n ·log n), dissatified
0 k

= 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

7. [NAT] [GATE-2014: 2M] (a) O(n2) (b) O(nlogn)


The minimum number of comparisons required to (c) (nlogn) (d) O(n3)
find the minimum and the maximum of 100 numbers
is _______. 12. [MCQ] [GATE-2014 : 1M]
Let P be a quicksort program to sort numbers in
8. [MCQ] [GATE-2010 : 2M] ascending order using the first element as the pivot.
The weight of a sequence a0, a1, ..., an-1 of real Let t1 and t2 be the number of comparisons made by
numbers is defined as a0 + a1/2 + ... an-1/2n-1. A P for the inputs [1 2 3 4 5] and [4 1 5 3 2]
subsequence of a sequence is obtained by deleting respectively. Which one of the following holds?
some elements from the sequence, keeping the order
(a) t1 = 5 (b) t1 < t2
of the remaining elements the same. Let X denote
(c) t1 > t2 (d) t1 = t2
the maximum possible weight of a subsequence of
a0, a1, ... an-1 and Y the maximum possible weight of
subsequence of a1, a2, ..., an-1. Then X is equal to 13. [MCQ] [GATE-2009 : 2M]
In quick sort, for sorting n elements, the (n/4) th
(a) max(Y, a0 + Y) (b) max(Y, a0 + Y/2)
smallest element is selected as pivot using an O(n)
(c) max(Y, a0 + 2Y) (d) a0 + Y/2 time algorithm. What is the worst case time
complexity of the quick sort?
Quick Sort (a) θ(n) (b) θ(n log n)
9. [NAT] [GATE-2019 : 1M] 2
(c) θ(n ) (d) θ(n2 log n)
An array of 25 distinct elements is to be sorted using
quicksort. Assume that the pivot element is chosen 14. [MCQ] [GATE-2008 : 2M]
uniformly at random. The probability that the pivot Consider the Quicksort algorithm. Suppose there is a
element gets placed in the worst possible location in procedure for finding a pivot element which splits
the first round of partitioning (rounded off to 2 the list into two sub-lists each of which contains at
decimal places) is_____. least one-fifth of the elements. Let T(n) be the
number of comparisons required to sort n elements.
10. [MCQ] [GATE-2015 : 1M] Then
Which one of the following is the recurrence (a) T(n) ≤ 2T (n / 5) + n
equation for the worst-case time complexity of the (b) T(n) ≤ T (n/5) + T (4n/5) + n
Quicksort algorithm for sorting n (> 2) numbers? In
(c) T(n) ≤ 2T (4n/5) + n
the recurrence equations given in the option below,
c is a constant. (d) T(n) ≤ 2T (n/2) + n
(a) T(n) = 2T(n/2) + cn
Merge Sort
(b) T(n) = T(n – 1) + T(1) + cn
15. [MCQ] [GATE-2015 : 1M]
(c) T(n) = 2T(n – 1) + cn
Assume that a merge sort algorithm in the worst case
(d) T(n) = T(n/2) + cn takes 30 seconds for an input of size 64. Which of
the following most closely approximates the
11. [MCQ] [GATE-2014 : 1M] maximum input size of a problem that can be solved
You have an array of n elements. Suppose you in 6 minutes?
implement quicksort by always choosing the central (a) 256 (b) 512
element of the array as the pivot. Then the tightest
(c) 1024 (d) 2048
upper bound for the worst-case performance is

6.11
GATE Wallah CS & IT Topic wise PYQs
Algorithms

16. [MCQ] [GATE-2012 : 2M] Selection Sort


A list of n strings, each of length n, is sorted into 20. [MCQ] [GATE-2013 : 1M]
lexicographic order using the merge sort algorithm Which one of the following is the tightest upper
The worst-case running time of this computation is bound that represents the number of swaps required
(a) O(n log n) (b) O(n2log n) to sort n numbers using selection sort?
(c) O(n2 + log n) (d) O(n2) (a) O(log n) (b) O(n)
(c) O(n log n) (d) O(n2)
Insertion Sort
17. [MCQ] [GATE-2021 : 1M] 21. [MCQ] [GATE-2009 : 1M]
Consider the following array: What is the number of swaps required to sort n
elements using selection sort, in the worst case?
23 32 45 69 72 73 89 97
(a) (n) (b) (n log n)
Which algorithm out of the following options uses (c) (n) 2
(d) (n2 log n)
the least number of comparisons (among the array
elements) to sort the above array in ascending order?
Heap Sort
(a) Quicksort using the last element as pivot
(b) Selection Sort 22. [MCQ] [GATE-2021 : 1M]
We have a binary heap on n elements and wish to
(c) Mergesort
insert n more elements (not necessarily one after
(d) Insertion sort another) into this heap. The total time require for
this is
18. [MCQ] [GATE-2016 : 1M] (a) Θ(log n) (b) Θ(n)
The worst-case running times of Insertion sort,
(c) Θ(n log n) (d) Θ(n2)
Merge sort and Quick sort, respectively, are:
(a) (n log n), (n log n), and (n2)
23. [MCQ] [GATE-2021 : 1M]
(b) (n2), (n2) and (n log n) Let H be a binary min-heap consistiong of n elements
(c) (n2), (n log n), and (n log n) implemented as an array. What is the worst-case
time complexity of an optimal algorthm to find the
(d)  (n2), (n log n), and (n2)
maximum elemets in H?
Bubble Sort (a) Θ(1) (b) Θ(log n)
19. [MCQ] [GATE-2016 : 1M] (c) Θ(n) (d) Θ(n log n)
Assume that the algorithms considered here sorts the
input sequences in ascending order. If the input is 24. [NAT] [GATE-2020 : 2M]
already in ascending order, which of the following Consider the array representation of a binary min-
are TRUE? heap containing 1023 elements. The minimum
number of comparisons required to find the
I: Quick sort runs in (n2) time
maximum in the heap is _____.
II: Bubble sort runs in (n2) time
III: Merge sort runs in (n) time 25. [NAT] [GATE-2018 : 2M]
IV: Insertion sort runs in (n) time The number of possible min-heaps containing each
(a) I and II only (b) I and III only value from {1, 2, 3, 4, 5, 6 7} exactly once is
________.
(c) II and IV only (d) I and IV only

6.12
GATE Wallah CS & IT Topic wise PYQs
Divide and Conquer

26. [NAT] [GATE-2016 : 2M] (a)


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 ______.

27. [MCQ] [GATE-2015 : 1M]


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 (b)
(a) 4 (b) 5
(c) 2 (d) 3

28. [MCQ] [GATE-2015 : 2M]


Consider a max heap, represented by the array:
40, 30, 20, 10, 15, 16, 17, 8, 4
Array
1 2 3 4 5 6 7 8 9
Index
Value 40 30 20 10 15 16 17 8 4 (c)
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

29. [MCQ] [GATE-2013: 1M]


The number of elements that can be sorted in Θ(log (d)
n) time using heap sort is
(a) Θ(1) (b)  log n
 log n 
(c)    (d) Θ(log n)
 log log n 
30. [MCQ] [GATE-2011 : 1M]
A max-heap is a heap where the value of each parent
is greater than or equal to the value of its children.
Which of the following is a max-heap?

❑❑❑

6.13
GATE Wallah CS & IT Topic wise PYQs
Algorithms

1. (a) 2. (b) 3. (b) 4. (c)


5. (29 to 29) 6. (d) 7. (148 to 148) 8. (b)
9. (0.08 to 0.08) 10. (b) 11. (a) 12. (c)
13. (b) 14. (b) 15. (b) 16. (b)
17. (d) 18. (d) 19. (d) 20. (b)
21. (a) 22. (b) 23. (c) 24 (511 to 511)
25. (80 to 80) 26. (8 to 8) 27. (d) 28. (b)
29. (c) 30. (b)

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

Maximum – Minimum(Using Divide and Conquer


 3n 
Algorithm: strategy) :  – 2
 2 
Majority element (A, n, x) → O(logn)
n
{ T ( n ) = 2T   + 2,n  2
1. i = first occurrence of x in A. 2
from left, using modified binary search = 1, n=2
= 0, n = 1
 n 
2. if  A i  = A  + i   them 3  100
 2
  –2
2
print(A[i] is majority); : 3 × 50 – 2 = 150 – 2 = 148
else
print (No);
}

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

For worst case scenario, the pivot should get fixed


either at the first or at the last position.
T ( n ) = T ( n –1) + O ( n ) + C

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

13. (b) n = 512 = 512  9 = 4608


∴ n = 512
n  3n 
T(n) = T  + T  + O(n)
4  4 

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.

Heapify method / Build heap


Time:  ( 2n ) =  ( n )

6.17
GATE Wallah CS & IT Topic wise PYQs
Algorithms

23. (c) 26. (8 to 8)


• Maximum element in a heap lies at the leaf-
level.
n
• Maximum of n/2 elements; O   =  ( n )
2

24. (511 to 511)


Maximum will lie at the leaf. The maximum depth at which integer 9 can appear is
 
Number of nodes at leaf =  1023  = 511.5 = 512
 2  8 for the given condition.
Number of comparisons needed = 512 – 1 = 511

25. (80 to 80) 27. (d)


• Left subtree heap: 6C3
• Left and Right Nodes

Can be arranged in 2! way The minimum number of interchanges needed to


 Total so far: (6C3 * 2!) convert it into a max heap = 1 + 1 + 1 = 3.
Total number of possibilities = (6C3) * (2!) *
(2!)
6  5 4
=  2  2 = 20 * 2 * 2 = 80.
3 2

6.18
GATE Wallah CS & IT Topic wise PYQs
Divide and Conquer

28. (b) 29. (c)


A: 40 30 20 10 15 16 17 8 4 Time of heap sort: O(n log n)
1 2 3 4 5 6 7 8 9 O ( )
log n log log n  O ( log n log log n )

Time of heap sort = n.log n

log n   log n  

Time =   log  
 
log log n   log log n 

= log n  ( log log n – log log log n )


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

3. [NAT] [GATE-2017 : 2M]


A message is made up entirely of characters from the
set X = {P, Q, R, S, T}. The table of probabilities for
each of the characters is shown below:

6.20
GATE Wallah CS & IT Topic wise PYQs
Greedy Techniques

5. [MCQ] [GATE-2009 : 2M] 8. [NAT] [GATE-2018 : 2M]


Consider the following graph: Consider the following undirected graph G:
Choose a value of x that will maximize the number
b e of minimum weight spanning trees (MWSTs) of G.
2
5 5 The number of MWSTs of G for this value of x is
6 6 _______.
a 4 d 3 g
6
3 5 4
c f
6
Which one of the following is NOT the sequence of
edges added to the minimum spanning tree using 9. [MCQ] [GATE-2016 : 1M]
Kruskal’s algorithm?
Let G be weighted connected undirected graph with
(a) (b, e) (e, f) (a, c) (b, c) (f, g) (c, d) distinct positive edge weights. If every edge weight
(b) (b, e) (e, f) (a, c) (f, g) (b, c) (c, d) is increased by the same value, then which of the
following statements is/are TRUE?
(c) (b, e) (a, c) (e, f) (b, c) (f, g) (c, d)
• P: Minimum spanning tree of G does not
(d) (b, e) (e, f) (b, c) (a, c) (f, g) (c, d) change.
• Q: Shortest path between any pair of vertices
Prim's Algorithm does not change.
(a) P only (b) Q only
6. [MCQ] [GATE-2021 : 1M] (c) Neither P nor Q (d) Both P and Q
Let G = (V, E) be any connected undirected edge-
weighted graph. The weights of the edges in E are
10. [NAT] [GATE-2016 : 2M]
positive and distinct. Consider the following
Let G be a complete undirected graph on 4 vertices,
statements:
having 6 edges with weights being 1, 2, 3, 4, 5 and
I: Minimum spanning Tree of G is always unique. 6. The maximum possible weight that a minimum
II: Shortest path between any two vertices of G is weight spanning tree of G can have is _____.
always unique.
Which of the above statements is/are necessarily 11. [MCQ] [GATE-2016 : 2M]
true? G = (V, E) is an undirected simple graph in which
(a) I only each edge has a distinct weight, and e is a particular
(b) II only edge of G. Which of the following statements about
the minimum spanning trees (MSTs) of G is/are
(c) Both I and II
TRUE?
(d) Neither I nor II I: If e is the lightest edge of some cycle in G, then
every MST of G includes e.
7. [NAT] [GATE-2020 2M] II: If e is the heaviest edge of some cycle in G, then
Consider a graph G = (V, E), where V = {v1, v2 every MST of G excludes e.
…v100}, E = {vi, vj} |1  i  j  100}, and weight of (a) I only (b) II only
the edge (vi, vj) is |i – j|. The weight of minimum (c) Both I and II (d) Neither I nor II
spanning tree of G is ____.

6.21
GATE Wallah CS & IT Topic wise PYQs
Algorithms

12. [MCQ] [GATE-2011 : 2M] 13. [MCQ] [GATE-2008 : 2M]


Let G = (V, E) be a weighted undirected graph and Dijkstra’s single source shortest path algorithm
when run from vertex a in the above graph,
let T be a Minimum Spanning Tree (MST) of G
computes the correct shortest path distance to
maintained using adjacency lists. Suppose a new
weighted edge (u, v)  V  V is added to G. The
worst-case time complexity of determining if T is
still an MST of the resultant graph is

(a) (|E| + |V|)

(b) (|E| |V|) (a) Only vertex a


(c) (|E| log |V|) (b) Only vertices a, e, f, g, h
(c) Only vertices a, b, c, d
(d) (|V|)
(d) All the vertices

❑❑❑

6.22
GATE Wallah CS & IT Topic wise PYQs
Greedy Techniques

1. (358 to 358) 2. (a) 3. (225 to 225) 4. (69 to 69)


5. (d) 6. (a) 7. (99 to 99) 8. (4 to 4)
9. (a) 10. (7 to 7) 11. (b) 12. (d)
13. (d)

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

3. (225 to 225) 5. (d)


<P, Q, R, S, T> = <0.22, 0.34, 0.17, 0.19, 0.08 In kruskals the edges are added in the non-
increasing order. Only option (d) applies the
algorithm correctly.

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

Sum = 36 + 10 + 16 + 7 = 69. The number of spanning trees = 2C1 * 2C1


= 2 * 2 = 4.

6.24
GATE Wallah CS & IT Topic wise PYQs
Greedy Techniques

9. (a)

The maximum possible weight that a minimum


To the above graph we are adding a constant value weight spanning tree of G can have is 7.
100. The M.S.T of the graph before adding the
constant and after is shown below:

11. (b)
Given that V = a, b, c, d and E = ab, bc, cd, da, ac,
let G = (V, E) be a graph.

After adding a constant 100 the shortest path from


node 2 to node 3 = 107.
After adding a constant 100 the path from node 2 to
node 3 = 110.
This proves that even after adding a constant value
to the edge weights the minimum spanning tree of
the graph does not change. • Assign the following weights to the edges:
ab = 1, bc = 2, cd = 4, da = 5, ac = 3. The MST
abcd with cost 7 (= ab + bc + cd) does not
include ac, which is obviously the cycle's
lightest edge.

10. (7 to 7)

• Assign the following weights to the edges: ab =


6, bc = 7, cd = 4, da = 5, and ac = 3. The MST
bacd with cost 13 (= ba + ac + cd) includes ac
since it is the cycle cdac's lightest edge.

6.25
GATE Wallah CS & IT Topic wise PYQs
Algorithms

Therefore, the lightest edge may or may not be E : O (E) : O(v)


included in the MSTs of G. A tree having n vertices will always have n – 1
edges.

13. (d)

• Let e be the heaviest edge. Consider the least


spanning tree that includes e. A cycle will be
produced if we increase the spanning tree's edge
count by one. Suppose we add edge e’ to the
spanning tree which generated cycle C. We can
reduce the cost of the minimum spanning tree if
Destination-d-values
we choose an edge other than e from C for
removal which implies that e must not be in Vertex
a b c d e f g h
minimum spanning tree and we get a selected
contradiction.
{a} – 1      
{a, b} – 1 3  –2   

{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.

V → R, for each edge (u, v)  E, define W (u, v) as


W (u, v) + f(u) – f(v).
Which one of the options completes the following
sentence so that it is TRUE?
“The shortest path in G under W are shortest paths
under W too,. ______”.

6.27
GATE Wallah CS & IT Topic wise PYQs
Algorithms

4. [MCQ] [GATE-2011 : 2M] 8. [MCQ] [GATE-2010 : 2M]


What will be the cost of the Minimum Spanning Tree What is the minimum possible weight of a path P
(MST) of such a graph with n nodes? from vertex 1 to vertex 2 in this graph such that P
1 contains at most 3 edges?
(a) (11n 2 − 5n) (b) n2 – n + 1
12 (a) 7 (b) 8
(c) 6n – 11 (d) 2n + 1 (c) 9 (d) 10

5. [MCQ] [GATE-2011 : 2M] Graph Searching


The length of the path from v5 to v6 in the MST of 9. [NAT] [GATE-2023 : 2M]
previous question with n = 10 is Let U = {1, 2, 3}. Let 2U denote the powerset of U.
(a) 11 (b) 25 Consider an undirected graph G whose vertex set is
(c) 31 (d) 41 2U. For any A, B  2U, (A, B) is an edge in G if and
only if (i) A  B, and (ii) either A ⊊ B or B ⊊ A. For
any vertex A in G, the set of all possible orderings
6. [MSQ] [GATE-2011 : 2M]
in which the vertices of G can be visited in a Breadth
Let G be a weighted graph with edge weights greater
First Search (BFS) starting from A is denoted by
than one and G’ be the graph constructed by
B(A).
squaring the weights of edge in G. Let T and T’ be
If  denotes the empty set, then the cardinality of
the minimum spanning trees of G and G’
B() is__________ .
respectively, with total weights t and t’. Which of the
10. [MSQ] [GATE-2021 : 2M]
following statements is TRUE?
An articulation point in connected graph is a vertex
(a) T’ = T with total weight t’ = t2
such that removing the vertex and its incident edges
(b) T’ = T with total weight t’ < t2
disconnects the graph into two or more connected
(c) T’≠T but total weight t’ = t2
components.
(d) None of the above Let T be a DFS tree obtained by doing DFS in
connected undirected graph G.
Common data for next two question Which of the following options is/are correct?
Consider a complete undirected graph with vertex set {0, (a) Root of T can never be an articulation point in
1, 2, 3, 4}. Entry Wij in the matrix W below is the weight G.
of the edge {i, j}. (b) Root of T is an articulation point in G if and only
 0 1 8 1 4 if it has 2 or more children.
 
  (c) A leaf of T can be an articulation point in G.
 1 0 12 4 9
  (d) If u is an articulation point in G such that x is an
W =  8 12 0 7 3
  ancestor of u in T and y is a descendent of u in
 1 4 7 0 2
  T, then all paths from x to y in G must pass
 4 9 3 2 0
  through u.
11. [NAT] [GATE-2021 : 1M]
7. [MCQ] [GATE-2010 : 2M] Consider a complete binary tree with 7 nodes. Let A
What is the minimum possible weight of a denote the set of first 3 elements obtained by
performing Breadth–first search (BFS) starting from
spanning tree T in this graph such that vertex 0
the root. Let B denote the set of first 3 elements
is a leaf node in the tree T?
obtained by performing Depth – First Search (DFS)
(a) 7 (b) 8
starting from the root.
(c) 9 (d) 10 The value of |A – B | is _________.

6.28
GATE Wallah CS & IT Topic wise PYQs
Graph Based Algorithm

12. [MCQ] [GATE-2018 : 2M] 16. [MCQ] [GATE-2015 : 2M]


Let G be a simple undirected graph. Let TD be a depth Let G = (V, E) be a simple undirected graph, and s
first search tree of G. Let TB be a breadth first search be particular vertex in it called the source. For
tree of G. Consider the following statements: x  V, let d(x) denote the shortest distance in G from
I: No edge of G is a cross edge with respect to TD. s to x. A breadth first search (BFS) is performed
(A cross edge in G is between two nodes neither starting at s. Let T be the resultant BFS tree. If (u, v)
of which is an ancestor of the other in TD). is an edge of G that is not in T, then which one of the
II: For every edge (u, υ) of G, if u is at depth i and following CANNOT be the value of d(u) – d(v)?
υ is at depth j in TB, then |i –j| = 1. (a) –1 (b) 0
Which of the statements above must necessarily be (c) 1 (d) 2
true?
17. [NAT] [GATE-2014 : 1M]
(a) I only (b) II only
Suppose depth first search is executed on the graph
(c) Both I and II (d) Neither I nor II
below starting at some unknown vertex. Assume that
13. [MCQ] [GATE-2017 : 1M] a recursive call to visit a vertex is made only after
The Breadth First Search (BFS) algorithm has been first checking that the vertex has not been visited
implemented using the queue data structure. Which earlier. Then the maximum possible recursion depth
one of the following is a possible order of visiting (including the initial call) is ______.
the nodes in the graph below?

M N O

18. [MCQ] [GATE-2014 : 1M]


R Q P Let G be a graph with n vertices and m edges. What
is the tightest upper bound on the running time of
(a) MNOPQR (b) NQMPOR Depth First Search on G. When G is represented as
(c) QMNROP (d) POQNMR an adjacency matrix.
14. [NAT] [GATE-2016 : 1M] (a) Θ(n) (b) Θ(n + m)
Consider the following directed graph: 2
(c) Θ(n ) (d) Θ(m2)

19. [MCQ] [GATE-2014 : 1M]


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
The number of different topological orderings of
(a) The shortest path between every pair of
the vertices of the graph is ______
vertices.
15. [NAT] [GATE-2016 : 1M]
(b) The shortest path from W to every vertex in the
Breadth First Search (BFS) is started on a binary tree
graph.
beginning from the root vertex. There is a vertex t at
(c) The shortest paths from W to only those nodes
a distance four from the root. If t is the n th vertex in
that are leaves of T.
his BFS traversal, then the maximum possible value
(d) The longest path in the graph.
of n is_______.

6.29
GATE Wallah CS & IT Topic wise PYQs
Algorithms

20. [MCQ] [GATE-2014 : 1M] 1. a b e f d g c


Consdier the directed graph given below: 2. a b e f c g d
3. a d g e b c f
4. a d b c g e f
A Depth First Search (DFS) is started at node a. The
nodes are listed in the order they are first visited.
Which of one the following is TRUE? Which all of the above is (are) possible output(s)?
(a) The graph does not have any topological (a) 1 and 3 only (b) 2 and 3 only
ordering. (c) 2, 3 and 4 only (d) 1, 2 and 3
(b) Both PQRS and SRQP are topological
orderings.
(c) Both PSRQ and SPRQ are topological 22. [MCQ] [GATE-2008 : 1M]
orderings. The Breadth First Search algorithm has been
(d) PSRQ is the only topological ordering. implemented using the queue data structure. One
possible order of visiting the nodes of the following
21. [MCQ] [GATE-2008 : 2M] graph is
Consider the following sequence of nodes for the
undirected graph given below:

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) 4. (b)


5. (c) 6. (a, b) 7. (d) 8. (b)
9. (5040 to 5040) 10. (b) 11. (1 to 1) 12. (a)
13. (d) 14. (6 to 6) 15. (31 to 31) 16. (d)
17. (19 to 19) 18. (c) 19. (b) 20. (c)
21. (b) 22. (c)

1. (d) 2. (a)

W(1, 3) + W(3, 2) + f(1) – f(3) + f(3) – f(2)


W(1, 2) = 1 + 3 = 4
W (1, 2) = Transformation of W(u, v) = W(1, 2) +
f(1) – f(2).

3. (12 to 12)

S→A →C→E →T

Shortest path between 3 and 4 has to be minimum


{x, 12}.

6.31
GATE Wallah CS & IT Topic wise PYQs
Algorithms

There can exist multiple shortest paths with same 5. (c)


cost.
<1, 2> = 2
<1, 3> = {8, 5 + x} x = 2 = 3
<1, 4> = 5
0 2 8 5
2 0 5 8 
W=
8 5 0 x
 
5 8 x 0

At least one shortest path must include edge cost


‘x’; ∴ The length of the path = (3 + 4 + 6 + 8 + 10)
Let (4, r) = (3, 4) From V5 to V6 in MST = 31.
1. 3 – 4 = x
2. 3 – 2 – 4 = 13 (Incorrect)
3. 3 – 1 – 4 = 13 (Incorrect)
4. 3 – 2 – 1 – 4 = 12
6. (a, b)
x = 12

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

Minimum cost spanning tree = 10 =7*6*5*4*3*2*1


= 42 * 20 * 6 = 42 * 120

= 5040

8. (b) {1} {2} {3}

Vertex set 0 1 2 3 4

{1} 1 – 12 4 9 {1,2} {2,3} {1,3}

{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

10. (b) B = {1, 2, 4}


A – B ={3}
A – B =1

The value of |A – B | will be 1 after performing the


said operation on a complete binary tree.

If the Root has only one child it can never


disconnect a tree on its removal. Therefore, option
(b) is correct.

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.

Breadth First Search = 1,2,3, 4,5, 6,7


Depth First Search = 1, 2, 4, 5, 3, 6, 7
A = {1, 2, 3}

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

The maximum number of nodes will be 31.

Different number of topological sorting for the 16. (d)


given graph will be 6. 2 cannot be the value of d(u) – d(v).

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

The maximum possible value of n for h=1 is 3.


v 4 v
The maximum possible value of n for h=2 is 7.
G T2
: 2h + 1 –3
: 23 – 1
=7

17. (19 to 19)


There exists total 21 nodes and only 2 nodes require
back track therefore the maximum recursion depth
h=1 is 21 – 2 = 19.
For h = 4
h4
Total Nodes = 2h+1 – 1 = 25 –1 = 31

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 possible outputs are: a d g e b c f


BFS spanning tree

20. (c) 22. (c)

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

Common data for next two questions: 9. [MCQ] [GATE-2008: 2M]


A sub-sequence of a given sequence is just the given Conider the following C funcitons:
sequence with some elements(possibly none or all) left out. intf1(int n)
We are given two sequence X[m] and Y[n] of lengths m and {
n, respectively with indexes of X and Y starting from 0.
if(n = = 0||n = = 1) return n;
else
7. [MCQ] [GATE-2009 : 2M] return (2* f1(n – 1) + 3* f1 (n – 2));
We wish to find the length of the longest common }
subsequence (LCS) of X[m] and Y[n] as l(m,n),
int f2 (int n)
where an incomplete recursive definition for the
{
function l(i, j) to compute the length of the LCS of
X[m] and Y[n] is given below: int i;

l(i, j) = 0, if either i = 0 or j = 0 int X[N], Y[N], Z[N];


X[0] = Y[0] Z [0] = 0;
= expr1, if i, j > 0 and x[i – 1] = Y [j – 1]
X[1] = 1; Y[1] = 2; Z[1] = 3;
= expr2, if i, j > 0 and x[i – 1] ≠ Y [j – 1]
for (i = 1; i ≤n; i ++)
Which one of the following options is correct?
{
(a) expr1 = l(i − 1, j) + 1
X[i] = Y[i – 1] + Z [i – 2];
(b) expr1 = l(i, j − 1)
Y[i] = 2* X[i];
(c) expr2 = max (l(i − 1, j), l(i, j − 1))
Z[i] = 3*X[i];
(d) expr2 = max(l(i − 1, j − 1), l(i, j))
}
return X[n];
8. [MCQ] [GATE-2009 : 2M] }
The values of l(i, j) could be obtained by dynamic The running time of f1(n) and f2(n) are
programming based on the correct recursive
(a) Θ(n) and Θ(n) (b) Θ(2n) and Θ(n)
definition of l(i, j) of the form given above, using an
(c) Θ(n) and Θ(2n) (d) Θ(2n) and Θ(2n)
array L[M, N], where M = m + 1 and N = n + 1, such
that L[i, j] = l(i, j).
Common data for next two questions:
Which one of the following statements would be
TRUE regarding the dynamic programming solution The subset-sum problem is defined as follows. Given a set
for the recursive definition of l(i, j)? of n positive integers, S = {a1, a2, a3, …… ,an} and a positive
integer W, is there a subset S whose elements sum of W? A
(a) All elements of L should be initialized to 0 for
dynamic program for solving this problem uses a 2-
the values of l (i, j) to be properly computed.
dimensional Boolean array, X with n rows and W + 1
(b) The values of l(i, j) may be computed in a row columns X [i, j], 1 ≤ i ≤ n, 0 ≤ j ≤ W, is TRUE if and only if
major order or column major order of L [M, N]. there is a subset of {a1, a2……, ai} whose elements sum to j.
(c) The values of l(i, j) cannot be computed in either 10. [MCQ] [GATE-2008 : 2M]
row major order or column major order of L [M, Which entry of the array X, if TRUE, implies that
N]. there is a subset whose elements sum of W?
(d) L [p, q] needs to be computed before L [r, s] if (a) X [1, W] (b) X [n, 0]
either p < r or q < s. (c) X [n, W] (d) X [n – 1, n]

6.38
GATE Wallah CS & IT Topic wise PYQs
Dynamic Programming

11. [MCQ] [GATE-2008 : 2M] 0/1 Knapsack Problem


Which of the following is valid for 2 ≤ i ≤ n, and 15. [NAT] [GATE-2018 : 2M]
ai ≤ j ≤ W? Consider the weights and values of items listed
(a) X [i, j] = X [i – 1, j]  X[i, j − ai] below. Note that there is only one unit of each item.

(b) X [i, j] = X [i − 1, j]  X[i − 1, j − ai] Item Weight in (Kgs) Value in


Number (Rupees)
(c) X [i, j] = X [i – 1, j]  X[i, j − ai]
1 10 60
(d) X [i, j] = X [i − 1, j]  X[i − 1, j − ai]
2 7 28
12. [MCQ] [GATE-2008 : 2M]
3 4 20
The subset-sum problem is defined as follows:
4 2 24
Given a set S of n positive integers and a positive
integer W, determine whether there is a subset of S The task is to pick a subset of these items such that
whose elements sum to W. their total weight is not more than 11 kgs and their
An algorithm Q solves this problem in O(nW) time. total value is maximized. Moreover, no item may
Which of the following statements is false? be split. The total value of items picked by an
(a) Q solves the subset−sum problem in optimal algorithm is denoted by Vopt. A greedy
polynomial time when the input is encoded in algorithm sorts the items by their value-to-weight
unary. ratios in descending order and packs them greedily,
(b) Q solves the subset sum problem is polynomial starting from the first item in the ordered list. The
time when the input is encoded in binary. total value of items picked by the greedy algorithm
(c) The subset sum problem belongs to the class NP. is denoted by Vgreedy.

(d) The subset sum problem in NP−hard. The value of Vopt – Vgreedy is _______.

Floyd Warshall's Algorithm Matrix Chain Multiplication


13. [MCQ] [GATE-2017 : 1M] 16. [MCQ] [GATE-2018 : 2M]
Consider the followign table: Assume that multiplying a matrix G1 of dimension P
Algorithm Design paradigms × Q with another matrix G2 of dimension q × r
(P) Kruskal (i) Divide and conquer requires pqr scalar multiplications. Computing the
(Q) Quicksort (ii) Greedy product of n matrices G1 G2 G3 …. Gn can be done by
(R) Floyd-Warshall (iii) Dynamic parenthesizing in different ways. Define GiGi + l., as
programming an explicitly computed pair for a given
parenthesizing if they arc directly multiplied. For
(a) P-ii, Q-iii, R-i (b) P-iii, Q-i, R-ii
example, in the matrix multiplication chain
(c) P-ii, Q-i, R-iii (d) P-i, Q-ii, R-iii G1G2G3G4G5G6 using parenthesization
14. [MCQ] [GATE-2016 : 1M] (G1(G2G3))(G4(G5G6)), G2G3 and G5G6 are the only
The Floyd-Warshall alorithm for all-pair shortest explicitly computed pairs.
paths computation is based on Consider a matrix multiplication chain F 1F2F3F4F5,
(a) Greedy paradigm where matrices F1, F2, F3, F4 and F5 are of
(b) Divide-and-conquer paradigm dimensions 2 × 25, 25 × 3, 3 × 16, 16 × 1 and 1 ×
(c) Dynamic programming paradigm 1000, respectively. In the parenthesization of
(d) Neither Greedy nor Divide-and-conquer nor F1F2F3F4F5 that minimizes the total number of scalar
Dynamic Programming paradigm multiplications, the explicitly computed pairs is/are

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) 2. (c) 3. (d) 4. (a, c)


5. (a) 6. (b) 7. (c) 8. (b)
9. (b) 10. (c) 11. (b) 12. (b)
13. (c) 14. (c) 15. (16 to 16) 16. (c)
17. (1500 to 1500) 18. (c)

1. (34 to 34)

3. (d)

T ( n ) = 2.T ( n–1) + C
= 2.T ( n–1) + 1

Algorithm for tower of hanoi problem:


1. “qpqr”
TOH (n,L,M,R)
2. “qprr”
{
3. “pqrr”
if (n = 0) return
Length = 4 = x else
Number of LCS = 3 = y {
x + 10y = 4 + 10 × 3 = 34. TOH (n–1, L, R, M);
Move (1, L, R)
TOH (n–1, M, L, R);
}
}
2. (c)

Bellman ford = (n.e) = (n.n 2 ) for complete


= (n 3 ) graph e = (n 2 )

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.

X[n, W] = Entry stating whether subset exists/Not 13. (c)


X [i, j] = True/False, whether there exist a subset Algorithm Design paradigms
from first ‘i’ elements that sum to ‘j’.
(P) Kruskal (ii) Greedy

(Q) Quicksort (i) Divide and conquer

(R) Floyd-Warshall (iii) Dynamic


programming
11. (b)
The right answer is B X[i, j] = X[i − 1, j]  X[i − 1,
j − ai].
It is NP-complete (the subset-sum problem).
For instance, the technique checks to see if the sum
14. (c)
of the elements in the set S' is zero such that S'  S
given the set S = {–7, –3, –2, 5, 7}. The Floyd-Warshall’s algorithm is based on
dynamic programming. It is used to find all the pair
For instance, say if S' = {–3, –2, 5}.
shortest path for every pair (i, j), there exists two
x is a two-dimensional boolean array with n rows possibilities for every pair:
and W + 1 columns in the proposed dynamic
• If k is not an intermediate vertex in the shortest
method, where W is a positive weight or sum.
path from (i) to (j) then the value of dist[i][j]
x[i, j]1 ≤ i ≤ n, 0 ≤ j ≤ W remains same.
The element in the subset of S is determined by the • If k is an intermediate vertex in the shortest path
ith row, and the associated weight is determined by from (i) to (j), then the value is updated as
the jth column. dist[i][j] + dist[k].
When 2 ≤ i ≤ n and a ≤ j ≤ W
Let S' be a subset with weight W and a1, a2,...., ai.
This means that X[i, j] = X[i – 1, j]  X[i – 1, j – ai].

6.43
GATE Wallah CS & IT Topic wise PYQs
Algorithms

15. (16 to 16) 17. (1500 to 1500)


M = 11
Using optimal algorithm, Vopt = 60

Item No. Weight Value V/W

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

If we perform parenthesization such that F3F4 forms


a pair then it will reduce the total number of scalar
multiplications. Number of scalar multiplications = minimum of
(I, II, III, IV, V).

❑❑❑

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.

2. [NAT] [GATE-2022 : 2M]


Let G(V, E) be a directed graph, where V = {1, 2, 3,
4, 5} is the set of vertices and E is the set of directed
edges, as defined by the following adjacency matrix
1, 1  j  i  5
A. A i  j = 
0, otherwise S
A[[i][j] = 1 indicates a directed edge from node i to (a) The graph does not have a topological order.
node j. A directed spanning tree of G, rooted at r ϵ V, (b) A depth-frist traversal starting at vertex S
is defined as a subgraph T of G such that the classifies three directed edges as back edges.
undirected version of T is a tree, and T contains a (c) For each pair of vertices u and v, there is a
directed path from r to every other vertex in V. The directed path from u to v.
number of such directed spanning trees rooted at (d) The graph does not have a strongly connected
vertex 5 is_____________.
compnont.

6.45
GATE Wallah CS & IT Topic wise PYQs
Algorithms

5. [NAT] [GATE-2021: 2M] (a) A-3, B 2, C-4, D-1


In a directed acryclic graph with a source vertex s, (b) A-1, B-2, C-4, D-3
the qauility-score of a directed path is defined to be
the product of the weights of the edges on the path. (c) A-2, B-3, C-4, D-1
Further, for a vertex v other than s, the quality-score (d) A-2, B-1, C-3, D-4
of v is defined to be the maximum among the
quality-scores of all the paths from s to v. the quality 8. [MCQ] [GATE-2015 : 2M]
score of s is assumed to be 1. Given below are some algorithms, and some
f g t algorithm design paradigms.
1 1 List-I List-II
9 9 9
c d e A Dijkstra’s Shortest 1. Divide and
1 9 path Conquer
1 1 1 B Floyd-Warshall 2. Dynamic
algorithm to compute programming
s 9 a 1 b all pairs shortest
The sum of the quality -scores of all the vertieces in path.
the graph shown above is___. C Binary search on a 3. Greedy Design
sorted array
6. [NAT] [GATE-2020 : 2M]
D Backtracking search 4. Depth-first
Consider the following undirected graph with edge
on a graph search
weights as shown:
5. Breadth-first
search

Math the above algorithm (List-I) to the


corresponding design paradigam (List-II) they
follow:
The number of minimum-weight spanning trees of (a) A-1, B-3, C-1, D-5
the graph is _____.
(b) A-3, B-22, C-1, D-5

7. [MCQ] [GATE-2015 : 1M] (c) A-3, B-2, C-1, D-4

Match the following: (d) A-3, B-2, C-1, D- 5

List-I List -II


A: Prim’s algrithm for 1. Backtraking 9. [MCQ] [GATE-2014 : 1M]
minimum spanning tree In an adjacency list representation of an undirected
B: Floyd-Warshall algorithm 2. Greedy simple graph G = (V, E), each edge (u, 𝜐) has two
for all pairs shortest paths Method adjacency list entries: [𝜐] in the adjacency list of u,
C: Mergesort 3. Dynamic and [u] in the adjacency list of 𝜐. These are called
programming twins of each other. A twin pointer is a pointer from
D: Hamiltonian circuit 4. Divide and an adjacency list entry to its twin. If |E| = m and
conquer |V| = n, and the memory size is not a constraint, what

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______.

10. [MCQ] [GATE-2008 : 1M]


15. [MCQ] [GATE-2015 : 1M]
The most efficient algorithm for finding the number
Consider a complete binary tree where the left and
of connected components in an undirected graph on
the right subtrees of the root are max-heaps. The
n vertices and m edges has time complexity-
lower bound for the number of operations to convert
(a) Θ(n) (b) Θ(m) the tree to a heap is?
(c) Θ(m + n) (d) Θ(mn) (a) (log n) (b) (n)
(c) (n log n) (d) (n2)
Tree
16. [NAT] [GATE-2014 : 2M]
11. [MCQ] [GATE-2020 : 1M]
Suppose we have a balanced binary search tree T
What is the worst-case time complexity of inserting
holding n-numbers. We are given two numbers L
n2 elements into an AVL-tree with n elements
and H and wish to sum up all the numbers in T that
initially?
lie between L and H. Suppose there are m such
(a) (n4) (b) (n2 log n) numbers in T.
(c) (n3) (d) (n2) If the tightest upper bound on the time to compute
the sum is O(na logb n + mc logd n), the value of a +
12. [MCQ] [GATE-2020 : 2M] 10b + 100c + 1000d is ____.
In a balanced binary search tree with n elements,
what is the worst-case time complexity of reporting 17. [MCQ] [GATE-2013 : 1M]
all elements in range [a, b]? Assume that the number Which one of the following is the tightest upper
of reported elements is K. bound that represents the time complexity of
(a) (log n) (b) (n log k) inserting an object into a binary search tree of n
nodes?
(c) (log n + k) (d) (k log n)
(a) O(1) (b) O(log n)
(c) O(n) (d) O(n log n)
13. [MCQ] [GATE-2015 : 1M]
What are the worst-case complexities of insertion
and deletion of a key in a binary search tree? 18. [MCQ] [GATE-2012 : 1M]
The worst-case running time to search for an element
(a) (logn) for both insertion and deletion in a balanced binary search tree with n2 n elements
(b) (n) for both insertion and deletion is?
(c) (n) for insertion and (logn) for deletion (a)  (n log n) (b)  (n2n)
(d) (logn) for insertion and (n) for deletion (c) (n) (d) (log n)

6.47
GATE Wallah CS & IT Topic wise PYQs
Algorithms

Recurrence Relation (a) P – (iii), Q – (iv), R – (i), S – (ii)


19. [MCQ] [GATE-2017 : 1M] (b) P – (iv), Q – (iii), R – (i), S – (ii)
Match the algorithms with their time complexities: (c) P – (iii), Q – (iv), R – (ii), S – (i)
List-I (Algorithm) (d) P – (iv), Q – (iii), R – (ii), S – (i)
(P) Tower of Hanoi with n disks.
(Q) Binary search given n sorted numbers 20. [MCQ] [GATE-2008 : 2M]
(R) Heap sort given n numbers at the worst case. If we use Radix Sort to sort n integers in the range
(nk⁄12, nk), for some k > 0 which is independent of n,
(S) Addition to two n × n matrices
the time taken would be
List-II (Time complexity)
(a) Θ(n) (b) Θ(kn)
(i) (n2) (ii) (n log n)
(c) Θ(n log n) (d) Θ(n2)
(iii) (2n) (iv) (log n)

❑❑❑

6.48
GATE Wallah CS & IT Topic wise PYQs
Miscellaneous Topics

1. (a, b, c) 2. (24 to 24) 3. (a) 4. (a, b)


5. (929 to 929) 6. (3 to 3) 7. (c) 8. (c)
9. (b) 10. (c) 11. (b) 12. (c)
13. (a) 14. (995 to 995) 15. (a) 16. (110 to 110)
17. (b) 18. (c) 19. (c) 20. (c)

1. (a, b, c)

• Since G has Exactly One MST, as all the edge


weights are unique so the statement in option (a)
is correct.
• As already the given graph G has distinct as
weights, the third and fourth smallest edge
weights will always be a part of MST.
• This is a popular and basic property of Minimum
Spanning Tree, known as “Cut Property.” The cut
property is the basis for the algorithms that we
consider for the MST problem. “Cut Property” n=3
and “Cycle Property” are basic theorems.

Total of 6 spannig tree


2. (24 to 24)
n = 3; r = 3; 2 ST’s
n = 4; r = 4; 3! = 6 ST’s
n = 5; r = 5; 4! = 24 ST’s

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

contains only distinct values. q ( i ) = 929


i =1

4. (a, b)
6. (3 to 3)

The number of minimum-weight spanning trees of


the graph is 3C1 = 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

There are 3 back edges from node 3 to 2, node 9 to


S, and node 12 to 11.

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

size of Adj list = O(n+ m)

6.51
GATE Wallah CS & IT Topic wise PYQs
Algorithms

14. (995 to 995) 17. (b)


Final output = [500 + 99 * 5] = 500 + 495 = 995. Time to insert an element into a binary search tree
(n) = logn.

15. (a)
18. (c)
Time = log(n.2n) = log n + log 2n
T(n) = (log n + n) = O(n) = (n) = (n).

Height of a heap with ‘n’ elements is logn.

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

You might also like