You are on page 1of 18

ALGORITHMS

SOLUTIONS

1. What is the value printed by following program?

int a, b, i;
int k = 0;
int n = 32;
for (a = 1; a ≤ n; a = 2 * a)
{
for (b = 1; b ≤ n; b = b + 10)
{
for (i = 1; i ≤ n; i = 2 * i)
{
k = k+1;
}
}
}
printf(“%d”, k);

(a) 80 (b) 81
(c) 132 (d) 144

Solution: Option (d)

Explanation:

(d)

The value of ‘k’ printed is equal to the number of times “k = k+1” is executed.

The outer for loop executes for (log2 n + 1) = 6 times


i.e. for a = 1, 2, 4, 8, 16 and 32 (6 times)

The middle loop executes for b = 1, 11, 21, 31 (4 times)


The inner-most loop executes for i = 1, 2, 4, 8, 16, 32 (6 times)

∴ The value of k = 6 × 4 × 6 = 144

2. If f(n) = Θ(q(n)) and q(n) = Θ(h(n)) then f(n) =

1
(a) O(h(n)) (b) Θ(h(n))
(c) Ω(h(n)) (d) None of these

Solution: Option (b)

Explanation:

(b)

It is given that

f(n) = Θ (g(n))
f(n) = c1·g(n)
It is also given that g(n) = Θ(h(n))
g(n) = c2·h(n)

Substituting (2) in (1) we get

f(n) = c1·c2h·(n)
f(n) = c3·h(n)
f(n) = Θ (h(n))

3. Consider the following recurrence relation to find greatest common divisor of two numbers P
and Q.

What is the time complexity to compute GCD (P, Q)?

(a) O(log2 QP) (b) O(log2 max(Q, P))


(c) O(log2 min(P, Q)) (d) O(log2 (Q+P))

Solution: Option (b)

Explanation:

(b)

GCD algorithm has worst case time complexity when both P and Q are prime numbers.
The worst case time complexity is O(log2 max(Q, P)).

2
4. Which of the following statements is/are true?

S1: Heap sort is based on the selection method.


S2: Insertion sort is best when the best cases of input are considered (for comparisons based
algorithms)

(a) Only S2 (b) Only S2


(c) Both S1 and S2 (d) Neither S1 nor S2

Solution: Option (c)

Explanation:

(c)

The following are best case of the sorting algorithms

Insertion sort : O(n)


Quick sort : O(n logn)
Merge sort : O(n logn)
Bubble sort : O(n2)
Heap sort : O(n logn)

As we can see insertion sort has most efficient best case.

In heap sorting algorithm we extract the elements one by one. First the smallest (or largest) item
is located and it is separated from the rest, then the next smallest (or next largest) is selected and
so on until all items are separated.

Hence both S1 and S2 are true.

5. Using Kruskal’s algorithm how many distinct minimum cost spanning trees are possible?

(a) 3 (b) 4
(c) 5 (d) 6

Solution: Option (b)

Explanation:

3
(b)

Only these four minimum cost spanning trees are possible.

6. Alice needs to send a large message to Bob using only five words. Seeing that the message is
too long she decides to compress the message using Huffman coding algorithm.

If the respective frequencies of words are given in the table, what would be the hamming code
for w3w1w2 that she was supposed to use?

(a) 100011011 (b) 100011101


(c) 100001101 (d) 111011000

Solution: Option (b)

Explanation:

(b)

The following are hamming codes obtained by using Huffman coding algorithm:

w4 = 0 w3 = 1000
w1 = 11 w5 = 1001
w2 = 101

∴ w3w1w2 = 100011101

7. In the longest common-subsequence (LCS) problem we are given two sequences X =


〈x1 , x2 , … xm 〉 and Y = y1 , y2 , … yn and we wish to find a maximum length common subsequence
of X and Y.

The following is the recursive formulation of LCS problem.

4
Identify the respective expressions for A, B and C.

(a) X[m] ≥ Y[n], m, m – 1 (b) X[m] ≤ Y[n], m, m – 1


(c) X[m] = = Y[n], m, m + 1 (d) None of these

Solution: Option (d)

Explanation:

(d)

The following is the respective expressions for A, B and C.


A: X[m] = = Y[n]
B: m
C: m – 1

8. How many passes of bubble sort are required to sort the following sequence (Pass is counted
only when at least one swap is performed in the bubble sort pass)?

12, 15, 17, 11, 14, 13

(a) 6 (b) 5
(c) 4 (d) 3

Solution: Option (c)

Explanation:

(c)

Original sequence: 12, 15, 17, 11, 14, 13


After 1st pass: 12, 15, 11, 14, 13, 17
After 2nd pass: 12, 11, 14, 13, 15, 17
After 3rd pass: 12, 11, 13, 14, 15, 17
After 4th pass: 11, 12, 13, 14, 15, 17

9. Consider an array A of size K where each element is either 10 or 20. What is the minimum
time required to sort the array A.

5
(a) O(K log K) (b) O(K2)
(c) O(log K) (d) O(K)

Solution: Option (d)

Explanation:

(d)

The minimum time required is O(K).

Take two variables ‘i’ and ‘j’ such that i = 0 and j = K – 1. ‘i’ moves right until it finds 20 and ‘j’
moves left until it finds ‘10’. Then both will swap, and procedure continues as long as i < j. In
this way maximum ‘K’ comparisons are required.

Hence it take O(K) time.

10. A graph G is such that every vertex is connected to every other vertex via direct link. We
need to find shortest path between two vertices using Bellman ford algorithm. How much time
does it take?

(a) O(V2 log V) (b) O(V2)


(c) O(V3) (d) (V+E) log V

Solution: Option (c)

Explanation:

(c)

In Bellman ford algorithm for every round ‘E’ edges are relaxed. Each edge relaxation takes
constant time. In general there are (V – 1) rounds excluding the verification round.

Total time = (V – 1) × O(E) × O(1) = O(VE)


But for complete graph E = O(V2)
∴ Total time = O(V3)

11. Given a hash table with 6 keys and 10 slots, with simple uniform hashing. If collisions are
resolved by chaining then the probability that first slot ends up empty?

Solution: (0.531) [0.53 – 0.54]

Explanation:

6
(0.531) [0.53 – 0.54]

Each key as 1/10 probability of hashing into first slot.


1
∴ Probability of not hashing into first slot in (1 − 10)
∴ The probability that no key hashes into first slot

1 6 9 6
(1 − ) = ( ) = 0.531
10 10

12. A hash table of size 11 using the hash function h(x) = x mod 11 and Quadratic probing with
hi(x) = (h(x)+i2) mod 11 ∀i ∈ {0, 1, 2, … 10}.

The key values are given in the following order: 41, 27, 9, 21, 13, 22, 23, 26, 2, 30. The location
of ‘30’ in the hash table, if the index value starts from ‘0’ is __________.

Solution: 6

Explanation:

(6)

∴ 30 is located at 6.

13. Assume Dijkstra’s algorithm is used to find the shortest paths from node ‘A’ in the following
graph.

The number of edges are there which are not included in any of the shortest paths from node A is
___________.

Solution: 4

7
Explanation:

(4)

A → E : 3[edge e1]
A → D : 6[edge e3]
A → B : 4[edge e2]
A → C : 9[edge e2, edge e5]

The edges e4, e6, e7 and e8 are never used in any of the shortest paths.

14. Consider the following knapsack instance.

 Objects: (x1, x2, …, x7)


 Weights: (W1, W2, W3, …, W7) = (3, 4, 6, 7, 2, 4, 2)
 Profits: (P1, P2, …, P7) = (12, 8, 20, 8, 10, 24, 5)
 Capacity of the bag (m) = 17
 If knapsack problem is solved using maximum profit per unit weight then the overall
profit is __________.

Solution: 71

Explanation:

71

P1 P2 P3 P7
( , , , … , ) = (4, 2, 3.33, 1.142, 5, 6, 2.5)
w1 w2 w3 w7
P
Decreasing order of wi is x6 , x5 , x1 , x3 , x7 , x2 , x4
i

Pi
Since capacity of bag is 17 we can only add the following items using the ratio.
wi
x6 (4) + x5 (2) + x1 (3) + x3 (6) + x7 (2)

Total profit = 1 × 24 + 1 × 10 + 1 × 12 + 1 × 20 + 1 × 5
= 24 + 10 + 12 + 20 + 5
= 71 units.

15. Consider a set of 20 elements. To find maximum and minimum element in the given set, the
minimum number of comparisons required is ________?
(using DAC Max-Min algorithm)

8
Solution: 28

Explanation:

28
3
The minimum comparisons required are 2 n − 2
3(20)
∴ − 2 = 28 comparisons
2

16. A newly introduced machine can sort 400 entries in 400 sec using bubble sort. The number
of entries the same machine sort in 1600 sec using bubble sort is __________.

Solution: 800

Explanation:

(800)

Bubble sort takes O(n2) time to sort ‘n’ entries.


T(n) = C1·n2
400 = C1 · (400)2
1/400 = C1
Now 1600 = C1·n2
1600 × 400 = n2
800 = n
∴ It can sort 800 entries.

17. Which of the following statements is/are true?

S1: Heap sort is an inplace sorting algorithm.


S2: Quick sort is a stable algorithm.
S3: Merge sort is a stable algorithm.
S4: Insertion sort is an inplace algorithm.

(a) Only S2 (b) Only S1 and S2


(c) Only S1, S3 and S4 (d) Only S2 and S4

Solution: Option (c)

Explanation:

(c)
9
Quick sort is not stable algorithm i.e. if the elements are repeated in the array, their order may or
may not be preserved after the application of quick sort algorithm. Hence S2 is false.

1
18. Let f1 = 100000n, f2 = 30 n2 , f3 = (log n)200 , f4 = 2n and f5 = n log n. Which of the
following is the correct order of growth in ascending order.

(a) f3, f1, f2, f5, f4 (b) f3, f2, f1, f4, f5
(c) f3, f1, f5, f2, f4 (d) f3, f2, f4, f1, f5

Solution: Option (c)

Explanation:

(c)

(log n)200 < 100000 n < n log n < 1/30 n2 < 2n

19. Which of the following are asymptotically tight.

I. 2n2 = O(n2)
II. 2n = O(n2)
III. n2 = O(n5)
IV. n4 = Ω(n3)

(a) Only I (b) Only II


(c) Only II, III (d) Only II, III and IV

Solution: Option (a)

Explanation:

(a)

II, III and IV are not asymptotically tight. They can be expressed with more tightness.
2n = o(n2)
n2 = o(n5)
n4 = ⍵(n3)

I is already asymptotically tight.

20. Consider the following recurrence relation for the running time of strassen’s algorithm.

10
Θ(1) if n = 1
T(n) = { n
PT ( ) + Θ(nR )
Q
What are the respective values of P, Q and R?

(a) 2, 7, 3 (b) 7, 2, 3
(c) 7, 2, 2.81 (d) 7, 2, 2

Solution: Option (d)

Explanation:

(d)

The correct recurrence relation is

Θ(1) if n = 1
T(n) = { n
7T (2) + Θ(n2 )

21. What is the time complexity of the following recurrence relation?

T(n) = 2T(n⁄4) + √n + 42

(a) Θ(n1/2 log n) (b) Θ(n3/2 log n)


(c) Θ(n1/2 log n1/2) (d) Θ(n log n)

Solution: Option (a)

Explanation:

(a)

Applying Master theorem


Here a = 2, b = 4, d = 1/2
1/2
nlogb a = nlog4 2 = nlog4 (4) = n1/2
also f(n) = n1/2 = nlogb a
∴ T(n) = Θ(nlogb a ∙ log n) (According to case 2 of Master theorem)
= Θ(√nlogn)

22. Given that X = 〈A, B, C, B, D, A, B〉 and Y = 〈B, D, C, A, B, A〉. Which of the following is true?

11
(a) 〈B, C, A〉 is the longest common subsequence (LCS) of X and Y
(b) 〈B, C, B, A〉 is one of the LCS of X and Y
(c) LCS of length 5 or more exists
(d) None of these

Solution: Option (b)

Explanation:

(b)

X and Y have LCS’s of length 4. The following are longest common subsequences of X and Y.

〈B, C, B, A〉
〈B, D, A, B〉

Hence (b) is true.

23. Assume that enqueing and dequeing operation take O(V) time each, during BFS algorithm on
a graph. What would be the worst case time complexity of BFS algorithm? (V is the number of
vertices in the graph and graph is given as input to BFA algorithm in the form of adjacency list).

(a) O(V3) (b) O(V2E)


(c) O(V2) (d) O(V)

Solution: Option (c)

Explanation:

(c)

In normal BFS both enqueing and dequeing operation takes O(1) but in this algorithm it takes
O(V) time.

All the vertices will be enqued and dequed once.


∴ V(V + V) = O(V2) times

Adjacency list is scanned for every vertex, when it is dequed. So overall O(E) time to scan
adjacency lists
∴ O(V2 + E) = O(V2)

24. Which of the following statements is/are true?

12
S1: Dijkstra’s algorithm is not affected by negative edge weight cycles in the graph and gives
correct shortest path.
S2: Bellman ford algorithm finds all negative edge weight cycles present in the graph.

(a) Only S2 (b) Only S1


(c) Both S1 and S2 (d) Neither S1 and nor S2

Solution: Option (d)

Explanation:

(d)

Dijkstra’s algorithm assumes that all edge weights are non-negative. Because of this negative
edge weight cycles are undetected. Hence Dijkstra’s algorithm is bound to give wrong shortest
path whenever there is a negative weight cycle. It finds only negative edge weight cycle which
are reachable from source.

Bellman Ford algorithm detects the existence of negative cycle, but it does not name the nodes in
the cycle explicitly. Also, Bellman Ford algorithm finds the shortest path even if there are
negative edge weights but not if there are negative cycles.

25. Consider hashing with chaining as collision resolution technique is implemented. The load
factor obtained was 3. What does it indicate?

(a) The minimum length of chain is 3


(b) Atleast 3 key values are hashed to same slot
(c) The number of keys are 3 times the number of slots in hash table
(d) The number of slots are 3 times the number of keys in hashed

Solution: Option (c)

Explanation:

(c)

Load factor defined as the average number of keys per slot.


i.e. if number of slots = m
and number of keys = n
∴ n/m = 3 (given)
n = 3m which is same as option (c)

13
26. What is the minimum of the multiplications required to multiply the following three matrices.

A3×7, B7×2 and C2×9

(a) 96 (b) 97
(c) 98 (d) 378

Solution: Option (a)

Explanation:

(a)

To multiply AB we need 3 × 7 × 2 = 42 multiplications.


(AB)3×2 · C2×9

Further multiplication with C requires 3 × 2 × 9 = 54 Multiplications


∴ Total 54 + 42 = 96 multiplications for (AB)C.

To multiply BC we need 7 × 2 × 9 = 126 multiplications.


A3×7(BC)7×9

Further multiplications with A requires 3 × 7 × 9 = 189 Multiplications


∴ Total 126 + 189 = 315 multiplications for A(BC)

∴ Minimum multiplications required are 96.

27. A hash table of size 10, is shown in the figure with symbols stored from a to g using some
hash function with linear probing.

The worst case number of comparisons required when the symbol being searched is not in the
table is ?

14
(a) 5 (b) 4
(c) 3 (d) 7

Solution: Option (b)

Explanation:

(b)

The search using linear probing stops whenever it finds an empty slot.

∴ Both searches has 4 comparisons.

28. Find the total possible number of back edges that we come across during depth first traversal
on the following directed graph. Algorithm starts from vertex A (back edges are those edges
connecting a vertex u to an ancestor V in a depth first tree).

(a) 3 (b) 4
(c) 5 (d) 6

Solution: Option (c)

Explanation:

(c)

Back edges are those edges connecting a vertex u to an ancestor V in a depth first tree.

15
There are only 5 back edges.

29. Consider an array with the following elements: 12, 18, 17, 11, 13, 15, 16 and 14.

How many elements will change their initial position after completion of partition algorithm by
choosing 15 as pivot?

Solution: 7

Explanation:

(7)

After completion of partition algorithm

As we can see only 16 do not change its place.


∴ 7 element changes their place.

30. Using Kruskal’s algorithm, the weight of the minimal spanning tree for the following graph
is ___________.

Solution: 66

16
Explanation:

(66)

∴ Weight = 13 + 16 + 11 + 14 + 12 = 66

31. Consider the following particular search sequence on a BST.

500, 250, 190, 220, 150, 120, 105, 115, 110

Which key in the above sequence is making the search sequence invalid, if the search was made
for 110?

Solution: 220

Explanation:

(220)

The search key 110 is less than 190. Therefore key 220 to the right of 190 makes this search
sequence invalid.

32. Consider the following min heap tree. The number of element comparisons required to
maintain the heap property after deletion of root are __________.

Solution: 6

17
Explanation:

(6)

During deletion, the element is replaced by last element (196 in this case) in the last level. We
need to compare it with two of it’s children values. Parent will be compared to minimum of
(Lchild, Rchild). 196 will be compared and swapped with min (120, 130) and so on…. This will
result in 6 comparisons.

33. We are given jobs J1, J2, … , J8 and each job requires one unit of execution. Only one job can
be executed at a time. Each job has its respective profit and deadlines as shown in the table
below.

The maximum value of profit that can be earned is __________.

Solution: 122

Explanation:

(122)

Maximum deadline is 6. Therefore maximum 6 jobs can be scheduled.

The following table takes the completion of tasks according to their deadlines to obtain
maximum profit.

Profit = 122

18

You might also like