Está en la página 1de 7

Test Code: Algorithm-II CSE

1. Match the following List-I and List-II


List-I List-II
A. DFS (Depth First Search) Graph Traversal (i) O(n)
B. Huffman coding (ii) O(n2)
C. Job Sequencing with Deadline (iii) O(V + E)
(iv) O(V + E)log V)
(v) O(nlogn)
(a) A-iii, B-i, C-ii (b) A-iii, B-v, C-ii
(c) A-iv, B-i, C-v (d) A-iv, B-v, C-i

Sol. (b)
The time complexity of DFS graph traversals is O(V + E)
The time complexity of Huffman coding algorithm is O(n log n)
2
The time complexity of job sequencing with Deadline is O(n )

2. What is the possible order of visiting the nodes in the following graph using BFS
1

2 3 4

5 6

7
(a) 1 2 3 4 5 6 7 (b) 1 2 3 4 6 5 7
(c) 1 2 3 4 5 7 6 (d) None of these

Sol. (a)
1

2 3 4

5 6

7
BFS order is 1234567

3. Match the following List-I with List-II


List-I List-II
A. Travelling sales person (i) O(2n)
B. 0/1 Knapsack problem (ii) O(n3)
C. Bellman Ford algorithm (iii) O(n2 2n)
(iv) O(VE)
(v) O(V + E)
(vi) O((V + E) log V)

ENGINEERS CAREER GROUPTM


rd th
Head Office: S.C.O-80-81-82,3 and 4 floor, Sector-34/A, Chandigarh
TOLL FREE: 1800-270-4242
WEB: www.engineerscareergroup.in
(a) A-iv, B-i, C-iii (b) A-iii, B-i, C-v
(c) A-iii, B-i , C-iv (d) A-iv, B-i, C-v

Sol. (c)
The time complexity of Travelling sales person is O(n22n)
The time complexity of 0/1 knapsack problem is O(2 n)
The time complexity of Bellman Ford algorithm is O(VE)

4. Consider the following table


O1 O2 O3 O4
Weight 1 3 4 5
Profit 1 4 5 7
M=7
The maximum profit obtaining by placing objects into the knapsack using 0/1 Knapsack algorithm is
________ ?

Sol. (9)
The recurrence relation is
K(W, wt[], p[], n) = max [p[n 1] + K(W wt[n 1], wt, p, n 1), K (W , wt, p, n1)}
= 0 when n = 0 or W = 0
0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 1 1 1 1 1 1 1
2 0 1 1 4 5 5 5 5
3 0 1 1 4 5 6 6 9
4 0 1 1 4 5 7 8 9

5. Suppose we have 8 sorted lists and each of sizes 10, 7, 29, 13, 2, 11, 45 and 15 respectively, which need to
be merged into a combined sorted list, but we can merge only two at a time. The minimum total number of
comparisons needed when apply optimal merge pattern is __________.

Sol. 350 (349.5 to 350.5)


Given lists sizes are
10,7,29.13,2, 11, 45 and 15
Sort the list sizes in increasing order
2,7, 10, 11, 13, 15,29,45
Final optimal merge tree is
132

79

53 34 45

24 29 15 19

11 13 9 10

2 7

Total comparisons = 9 + 19+ 34 + 79 + 24 + 53 + 132 = 350

6. Use Huffman coding to encode the following symbols with the frequencies listed A: 0.08, B: 0.10, C :
0.12, D : 0.15, E : 0.20, F ; 0.35. What is the average number of bits used to encode a character ?

ENGINEERS CAREER GROUPTM


rd th
Head Office: S.C.O-80-81-82,3 and 4 floor, Sector-34/A, Chandigarh
TOLL FREE: 1800-270-4242
WEB: www.engineerscareergroup.in
(a) 2.45 (b) 3.45
(c) 2 (d) 3.33

Sol. (a)
1.00
0 1

0.38 0.62

0 1 0 1

0.18 E 0.27 F
0.20 0.35
0 1 0 1

A B C D
0.08 0.10
0.12 0.15

Average number of bits used to encode a symbol using this encoding is


= 3*0.08 + 3* 0.10 + 2*0.20 + 3*0.12 + 3*0.15 + 2 * 0.35
= 2.45

7. Consider the following instance of the knapsack problem


(P1 P8) = (60, 91, 40, 50, 15, 20, 21, 29)
(W1 W8) = (30, 15, 33, 38, 10, 9, 7, 29)
M = 80
Which of the following objects are not included at all if the given problem is solved greedy method.
(a) (4, 3, 8) (b) (3, 8)
(c) 8 (d) (5, 4, 3, 8)

Sol. (b)
Ob-1 Ob-2 Ob-3 Ob-4 Ob-5 Ob-6 Ob-7 Ob-8
P1 P8 60 91 40 50 15 20 21 29
W1W8 30 15 33 38 10 9 7 2
Pi/Wi 2 6.07 1.21 1.31 1.5 2.23 3 1
Sort the ratio Pi/Wi on decreasing order.
Object Weight Profit
Ob-2 15 91
Ob-7 7 21
Ob-6 9 20
Ob-1 30 60
Ob-5 10 15
Object 3, 8 are not included.

8. Assume letters P, Q, R, S T have probability 0.5, 0.3, 0.1, 0.2 and 0.3 respectively. Which of the following
is the correct Huffman code for P, Q, R, S and T, respectively? (Assume the labels on left child as 0 and
that on right child as 1?
(a) 11, 01, 000, 001, 10 (b) 10, 00, 111, 110, 01
(c) 10, 01, 000, 001, 1 (d) 11, 01, 001, 000, 10

Sol. (a)

ENGINEERS CAREER GROUPTM


rd th
Head Office: S.C.O-80-81-82,3 and 4 floor, Sector-34/A, Chandigarh
TOLL FREE: 1800-270-4242
WEB: www.engineerscareergroup.in
14
0 1

0.6 0.8
0 1 0 1
0.3 Q T P
0 1 0.3 0.3 0.5
R S
0.1 0.2

9. Consider the following graph


a 3 c 5 e

7
5 2 6 3 6 2

b d f g
2 g 2
Compute the sequence of the nodes visited to get shortest from node a to g if source vertex is a.
(a) a, d, c, f, g, e (b) a, b, d, c , f, g
(c) a, d, c, b, f, e, g (d) a, d, c, b, f, e, g

Sol. (c)

a b c d e f g
a 0 5 3 2
0 3 2
d 4 11
0 4 3 2 8
c 6 10
0 4 3 2 8 6
b 10
0 4 3 2 8 6
f 8
0 4 3 2 8 6
e 8
0 4 3 2 8 6
g 8

10. Matrix multiplication is associative and matrix chain multiplication uses following matrices
A1 is 2 3
A2 is 3 6
A3 is 6 4
A4 is 4 5
The minimum number of multiplications required to compute A1 A2 A3 A4 is __________

Sol. 124 (123.5 to 12.5)


The recurrence relation of matrix chain multiplications is
M[I, j] = M[I, k] + M[k + 1][j] + p[I 1]* p[k] * p[j]
1 2 3 4
1 0 36 84 124
2 0 72 132
3 0 120
4 0

ENGINEERS CAREER GROUPTM


rd th
Head Office: S.C.O-80-81-82,3 and 4 floor, Sector-34/A, Chandigarh
TOLL FREE: 1800-270-4242
WEB: www.engineerscareergroup.in
11. What is the order that followed to find the minimum number of multiplications for the given A 1, A2, A3, A4

(a) ((A1A2) (A3, A4)) (b) ((A1 A2) A3)A4)


(c) (A1(A2 A3))A4 (d) ((A1(A2 A3))A4)

Sol. (b)
The minimum number of multiplications required is 124
The order that followed is
(1 2 3 )4
i.e. ((12)3)4)

12. Given two strings X = 2343523 and Y = 354234. The number of longest common sub sequences from
X to Y is ________.

Sol. 2(1.5 to 2.5)


X = 2343523
Y = 354234
LCS (X, Y) is 3423 or 3523

13. Consider the following graph


2

a 1 b 2 c 1 d

1 3 3 1
e 2 f 3 g 2 h

2 2 3 4 3 2
i 3 j 4 k3 1

3 4 3 2
2 2 3
m n o p

2
The weight of maximum spanning tree for the given weighted graph, using Kruskals algorithm is _______

Sol. 45 (44.5 to 45.5)


2

b c
a d

3 3

2 3 g h
e f
4 3 2
i 3 j 4
1
k 3
3 4 3

m n o 3 p
One of the possible maximal spanning tree is
Total Weight = 4 + 4 + 4 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 2 + 2 + 2 = 45

14. Consider the following keys that are hashed into the hash table in the order given using the hash function
H(k).
int H(int k)
{
int a = (k +7)* (k + 7)
a = a/16;
a = a+k

ENGINEERS CAREER GROUPTM


rd th
Head Office: S.C.O-80-81-82,3 and 4 floor, Sector-34/A, Chandigarh
TOLL FREE: 1800-270-4242
WEB: www.engineerscareergroup.in
a = a% 11;
return a;
}
43, 23, 1, 0, 15, 31, 4, 7, 11, 3
Assume hash table has location from 0 to 10
The location of an element 15, if hash table uses linear probing to hash the given elements is __________

Sol. 4 (3.5 to 4.5)


Given keys are
43, 23, 1, 0, 15, 31, 4, 7, 11, 3
1. Key 43
a = 50 * 50 = 2500
a = 2500/16 = 156
a = 156 + 43 = 199
a = 199% 11 = 1

2. Key 23
a = 30*30 = 900
a = 900/16 = 56
a = 56 + 23 = 79
a = 79% 11 = 2

3. Key 1
a = 8 * 8 = 64
a = 64/16 = 4
a=4+1=5
a = 5% 11 = 5

4. Key 0
a = 7 * 7 = 49
a = 49/16 = 3
a=3+0=3
a = 3% 11 = 3

5. Key 15
a = 22 * 22 = 484
a = 484/16 = 30
a = 30 + 15 = 45
a = 45% 11 = 1
It collides with location 1, because of linear probing, sequentially checks for available slots.
Now, Key 15 can be at location 4.
6. Key 31
a = 38 * 38 = 1444
a = 1444/16 90
a = 90 + 31 = 121
a = 121% 11 = 0

7. Key 4
a = 11 * 11 = 121
a = 121/16 = 7
a = 7 + 4 = 11
a = 11% 11 = 0
It collides with location 0, 1, 2, 3, 4, 5 and placed at location 6.

8. Key 7
a = 14 * 14 = 196
a = 196/16 = 12
a = 12 + 7 = 19

ENGINEERS CAREER GROUPTM


rd th
Head Office: S.C.O-80-81-82,3 and 4 floor, Sector-34/A, Chandigarh
TOLL FREE: 1800-270-4242
WEB: www.engineerscareergroup.in
a = 19% 11 = 8

9. Key 11
a = 18*18 = 324
a = 324/16 = 20
a = 20 +11 = 31
a = 31% 11 = 9

10 Key 3
a = 10 * 10 = 100
a = 100/16 = 6
a=6 +3=9
a = 9% 11 = 9
It collides with location 9 and placed at location 10.
The final hash table after all insertions is
0 31
1 43
2 23
3 0
4 15
5 1
6 4
7
8 7
9 11
10 3
So, the key 15 is found at location 4.

15. The minimum cost spanning trees possible for the following graph are _______?

5 6
1
5 5

6 3 6 2
4

Sol. 2(1.5 to 25)

5
1 1
5 5 5
3 3
2 2

ENGINEERS CAREER GROUPTM


rd th
Head Office: S.C.O-80-81-82,3 and 4 floor, Sector-34/A, Chandigarh
TOLL FREE: 1800-270-4242
WEB: www.engineerscareergroup.in

También podría gustarte