Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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
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)
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 __________.
79
53 34 45
24 29 15 19
11 13 9 10
2 7
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 ?
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
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)
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
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. (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 ________.
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 _______
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
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
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
5
1 1
5 5 5
3 3
2 2