Algorithms DPPs
Algorithms DPPs
[MCQ] [MCQ]
4. Consider the following C-code
Consider two function f1 ( n ) = n 2 and f 2 ( n ) = n n
n 2
void foo (int x) 7.
{ then which of the following is true.
int a = 1; (a) f1(n) =(Of2(n)) (b) f1(n) = θ(f2(n))
if (n = = 1) (c) f1(n) = ω(f2(n)) (d) None of these
return;
2
Answer Key
1. (a) 5. (a)
2. (a) 6. (2 to 2)
3. (2 to 2) 7. (c)
4. (a)
3
n 2
f2 ( n ) O ( f1 ( n ) ) or
n2 = nn
Taking log on both side f1 ( n ) = ( f2 ( n ) )
2n log n n2 log n ∴ (c) is correct.
as we can see that
[MCQ] [NAT]
1. Consider an array containing the following elements in 4. Consider the number in the sequence
unsorted order (placed randomly) but 120 as first 2 5 11 17 19 21 26 33 39 40 51 65 79 88 99
elements Using binary search, the number of comparisons
120 160 30 190 14 24 70 180 110 required to search elements ‘2’ is____
Quick sort partitioning algorithm is applied by
choosing first elements as pivot element. Then what is [MCQ]
the total number of arrangements of array integers are 5. Merging 4 sorted files having 400, 100, 250, 50
possible preserving the effect of first pass of records will take O (___) time?
partitioning algorithm. (a) 800 (b) 400
(a) 680 (b) 700 (c) 200 (d) 100
(c) 720 (d) 740 Ans. (a)
Sol. Two sorted file of size m and n takes O(m + n) time
for merging.
[MCQ] So, total time = 400 + 100 + 250 + 50 = 800
2. Let T(n) = [n(log(n3) – logn) + logn]n + logn. ∴ (a) is correct.
complexity of T(n) is
[NAT]
(a) O(n2) (b) O(n3) 6. Consider a machine which needs a minimum of 50
(c) O(nlogn) (d) O(n2logn) seconds to sort 500 names by quick sort, then what is
the minimum time required to sort 50 names
(approximately)is _____ (round off to 2 decimal)
[MCQ]
n [NAT]
3. Assume that there are 4 sorted lists of elements
4 7. What is the total number of comparisons that will be
each, if these lists are merged into a single sorted list of required in worst case to merge the following sorted
‘n’ elements then how many key comparisons are files into a single sorted file into a single sorted file by
required in the worst case using an efficient algorithm? merging together two files at a time____.
7 Files F1 F2 F3 F4
(a) 2n – 3 (b) n –3
4 Number of records 40 42 44 46
9 6
(c) n –3 (d) n –3
4 4
2
Answer Key
1. (c) 5. (a)
2. (d) 6. (3.14 to 3.14)
3. (a) 7. (341 to 341)
4. (4 to 4)
3
[MCQ] [MCQ]
1. What is the time complexity of dynamic programming 6. How many minimum spanning tree does this graph
for matrix chain multiplication problem? have?
(a) O(n2)
(b) O(n3)
(c) O(nlogn)
(d) None of these
[NAT]
2. Consider the matrices x, y and z with dimension
10 × 20, 20 × 30 respectively. Then what is the
minimum number of multiplications required to
multiply the matrices? ________
(a) 2 (b) 3
[MCQ] (c) 4 (d) 5
3. What is the length of the LCS for the pair of
subsequences given below. [MCQ]
P = ATGACTATAA 7. Consider the following problem with knapsack
Q = GACTAATA capacity of 8
(a) 5 (b) 6 Items Profits Weights
(c) 7 (d) 8 I1 13 1
I2 8 5
[MCQ] I3 7 3
4. Consider a connected weighted graph G = (V, E), I4 3 4
Which of the following item is not selected in the
where |V| = n, |E| = m, if all the edges have distinct
optimal solution of 0/1, knapsack problem?
positive integer weights, then the maximum number of (a) I1 only (b) I2 only
minimum weight spanning trees in the graph is ? (c) I3 only (d) I4 only
(a) n (b) m
(c) 1 (d) nn–2 [MCQ]
8. Consider the following statements
S1: for every weighted graph and any two vertices p
[MCQ]
and q, Bellman ford algorithm starting at p will always
5. What is the weight of the minimum spanning tree for return a shortest path to q.
the graph shown below? S2: At the termination of Bellman ford algorithm even
if graph has negative weight cycle, correct shortest path
is found for vertex for which shortest path is well-
defined.
Which of the statement is correct?
(a) only S1
(b) only S2
(c) Both S1 and S2 are true
(a) 7 (b) 8
(d) neither S1 nor S2 is true
(c) 9 (d) 10
2
Answer Key
1. (b) 5. (d)
2. (18000 to 18000) 6. (a)
3. (c) 7. (b)
4. (c) 8. (d)
3
2. (18000 to 18000)
Given matrix dimension There are 2 dotted lines are the only choices that it has,
x : 10 × 20 ∴ Hence are only 2 MST possible here.
y : 20 × 30
z : 30 × 40 7. (b)
Optimal parentification is ((xy)z) w = 8(capacity)
Minimum number of multiplications Feasible solution
= 10 × 20 × 30 + 10 × 30 × 40 = 18000 (i) {I1, I3, I4}
Profit = 13 + 7 +3 = 23
3. (c) (ii) {I2, I3}
P=A T G A C T A T A A Profit = 8 + 7 = 15
GACTAATA Optimal solution = {I1, I3, I4}
GACTAAA With the capacity of 8 and maximum profit produced
is 23.
I2 is not selected in the solution.
∴ (b) is correct option.
4. (c)
The maximum number of minimum weight spanning 8. (d)
tree in a connected weighted graph G(V, E) with |V| = Bellman ford algorithm may not return a shortest path
n and |E| = m, where all edges have distinct positive from p to q
∴ S1 is false
integer weight is 1.
If graph has negative weight cycle, then Bellman ford
given error, so 2nd statement is also false.
5. (d)
1 + 2 + 1 + 2 + 3 + 1 = 10
[MCQ] [NAT]
1. Consider the following statements. 4. Consider is the weighted graph G given by
S1: Given a weighted declared graph with the distinct
weights, the shortest path among any two vertices
will be unique.
S2: A minimum spanning tree can contain negative
edges.
Choose the correct statements.
(a) Only S1 is true
(b) Only S2 is true
(c) Both S1 and S2 are true 4
(d) neither S1 nor S2 is true How many MST does G Have?
[MCQ] [MCQ]
2. Which of the statement is/are correct? 5. Let’s suppose, we want to merge some sorted files
(a) First edge added by Kruskal’s algorithm can be where the number of records in each file is given below.
the last edge added by prim’s algorithm (15, 18, 20, 21, 24, 28, 30, 32, 35, 40, 45, 50) then what
(b) In a graph, if one raises the length of all edge to
is the minimum number of comparisons required to
the power of 3, the minimum spanning tree will
stay the same. merge the following files?
(c) The heaviest edge in a graph cannot belong to the (a) 1200 (b) 1225
minimum spanning tree. (c) 1251 (d) 1255
(d) The maximum spanning tree (spanning tree of [MCQ]
maximum cost) can be computed by negating the 6. Greedy algorithm fails to give an optimal solution to
cost of all the edges in the graph and then
which of the following problems?
computing minimum spanning tree.
(p) Travelling salesman problem
[NAT] (q) Job scheduling with deadlines and penalty
3. Consider the following instantons of the job for- (r) Shortest path algorithm
scheduling problem with deadlines (Note: every Job (s) optimal merge pattern
takes one unit time) (t) Huffman encoding
Job J1 J2 J3 J4 J5 J6 J7 (a) p, q, r (b) r, s, t
Deadline 1 3 4 3 2 1 2 (c) p, q, r, s, t (d) All of the above
Profit 3 5 20 18 1 6 30
What is the maximum profit generated by greedy
algorithm ______?
2
Answer Key
1. (b) 4. (4 to 4)
2. (a, b, d) 5. (c)
3. (74 to 74) 6. (a)
3
2. (a, b, d)
True(a): The graph d(A, B) = 1, d(B, C) = 2 and we Choose any one dotted line here
start running prim’s at ‘C’ ∴ Totals 4 different MST’s
True (b): The MST algorithm care about relative edge 5. (c)
lengths and raising all edge lengths, the 3rd power
pressure this relationship.
False(c): This edge may be connecting two otherwise
dis-connected sub-graphs.
Ture(d): This work, and none of the proofs on MST
algorithm depends on edge weight being non-negative.
3. (74 to 74)
Put earliest deadline schedule with maximum profit
first.
Total comparisons = m = n – 1
= 32 + 40 + 51 + 61 + 67 + 80 + 96 + 111 + 148 + 208
total profit = 6 + 30 + 18 + 20 = 74 + 357
∴ 74
6. (a)
4. (4 to 4) Greedy algorithm fails to give an optimal solution for
(p) travelling salesman problem
(q) Job scheduling with deadline and penalty (Greedy
fails when there is infinite number of jobs and jobs
are arriving continuously)
(r) Shortest path algorithm in this if we follow the
greedy approach, the algorithm fails when there
Choose any 1 dotted line here are negative edge weight in the graph.
❑❑❑
[MCQ]
7. consider the following statements
S1: Backtracking is an algorithm technique for solving
What will be the worst-case time complexity (tightest) problems reclusively by trying to build a solution
of given problem, if we can take auxiliary space of incrementally.
O(n)? S2: Time complexity of N-Queens algorithm is O(n!)
(a) O(n) (b) O(n2) Which statement is true?
(c) O(logn) (d) O(nlogn) (a) only S1
(b) only S
(c) Both S1 and S2 are true
(d) Neither S1 nor S2 is true
2
Answer Key
1. (c) 5. (64 to 64)
2. (a) 6. (16807)
3. (d) 7. (c)
4. (d)
3
5. (64 to 64)
As number of undirected graphs is
2(n (n – 1)/2)
2. (a)
(i) Create an array x [] of size n. where n is the number 6. (16807)
of nodes in the given BST Number of spanning trees for an undirected complete
2. Perform the inorder traversal of the BST and copy graph with n nodes
the node values in the x [] in sorted order. n = nn–2
3. Now perform the preorder traversed of tree.
4. While traversing the root during the preorder 7. (c)
traversal, one by one copy the values from the x [] True(S1): Backtracking is an algorithm technique for
to the nodes. solving problems reclusively by trying to build a
solution incrementally.
3. (d) True (S2): Time complexity of N-Queens algorithm is
O(n!)
❑❑❑