0% found this document useful (0 votes)
7 views

Algorithms DPPs

Algo Notes PW

Uploaded by

Saurabh Perane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Algorithms DPPs

Algo Notes PW

Uploaded by

Saurabh Perane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1

Branch : CSE & IT Batch : Hinglish


Algorithms
Analysis of Algorithms DPP

[MCQ] for (; a ≤ n; a++)


1. Sort the functions in ascending order of {
asymptotic(big-O) complexity. printf(“GATEWALLAH”);
break;
f1(n) = n, f2(n) = 80, f3(n) = nlogn, f4(n) = loglog2n,
}
f5(n) = (logn)logn }
(a) f2(n), f4(n), f1(n), f5(n), f3(n) What is the worst time complexity of above program?
(b) f2(n), f1(n), f4(n), f5(n), f3(n) (a) O(1) (b) O(n)
(c) f2(n), f1(n), f4(n), f3(n), f5(n)
(c) O (log n) (d) O n
(d) f1(n), f1(n), f4(n), f3(n), f2(n)
[MCQ]
[MCQ]
5. Find the time complexity of the following summation,
2. Consider two function f(n) =10n+2logn and
assume that k is constant, k > 0
g(n) = 5n + 2(logn)2, then which of the following is n n
1
correct option? 
(a) f(n) = θ(g(n)) (b) f(n) = O(g(n)) x =1y= x +1 k
(c) f(n) = ω(g(n2)) (d) None of the above (a) O(n2) (b) O(n)
(c) O(n3) (d) None of the above
[MCQ]
3. Consider two function f(n) = n and g(n) = n logn + n [NAT]
then f(n) /g(n) is equivalent to how many of the 6. How many of the following expressions correctly
following given below? _______. describes T(n) = nlog (n2)? ______
(i) o(n–1/2) (ii) O(n–1/2) (a) θ(n2) (b) O(n)
(iii) Ω(1/logn) (iv) θ(n–1/2) (c) Ω(n) (d) O(n2)

[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

Hints & Solutions


1. (a) 4. (a)
80 < n If we see carefully, loop will execute only one time
log log2n < n
because of break statement, therefore time complexity
put n = 10100
log(logn)2 = 10100 will be O(1)
log(100)2 < 10100
4 < 10100 5. (a)
n <nlogn n n
1 1 n n
taking log on both side   = k   (1)
x =1 y = x +1 k x =1 y = x +1
logn < lognlogn
we know that (logn)2 > log n 1 n
now, (logn) logn < nlogn
=  1 + 1 + 1 + .....n – ( x + 1) + 1 times
k x =1 
as we can see that logn in LHS and n on RHS.
1 n
n (logn)logn = n – x
k x =1
taking log on both sides
 = 1 n.n – n ( n + 1) 
logn < logn*log logn 1
 ( ) 
n n
= n 1 – n x  k 
From above we conclude that growth of log*logn is k  x =1 x =1 
 2 
higher than 1.
∴ option (a) is correct. 1 n2 + n  1  2
= n 2 – = n – n
k  2  2k  
2. (a) = O(n2)
As we can see in above function, ‘n’ is the dominating
factor in these 2 functions. Which means they also 6. (2 to 2)
have similar growth rate. Given: T(n) = nlog (n2) = 2nlogn
∴ f(n) = O(g(n))
(i) T(n) = θ(n2), which means the value of T(n) is
Hence option (a) is correct.
exactly θ(n2), but as we can see that T(n) is
3. (2 to 2) nlog(n2) so this is incorrect.
f (n)
= h (n) (ii) T(n) = O(n): T(n) ≤ k.n, but value of T(n) is
g(n)
nlog(n2)
Given
f ( n ) = n, g ( n ) = nlogn + n
So, this is also false.
(iii) T(n) = Ω(n)
n
=
n log n + n T(n)>= k.n and the complexity given for T(n) is
n nlog(n2), so it is correct.
( )
=
n n log n + n (iv) O(n2)
1 T(n) = O(n2)
=
n logn + n nlog(n2) ≤ k.n2 which is correct.
and clearly h(n) = O(n–0.5) and Hence 2 expression out of 4 are correct.
h(n) = o(n–0.5)
NOTE: if small ‘o’ possible then Big ‘O’ is possible
but if Big ‘O’ possible then small ‘o’ may or may not
possible.
∴ (i) and (ii) are correct.
Hence 2 expressions are correct.
4

7. (c) 2n has more growth rate than


n2 ∴ we conclude
f1 ( n ) = n 2 and f 2 ( n ) = n n
n 2

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

Any issue with DPP, please report by clicking here:- https://forms.gle/t2SzQVvQcs638c4r5


For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if

PW Mobile APP: https://smart.link/7wwosivoicgd4


1

Branch : CSE & IT Batch : Hinglish


Algorithms
Design Strategies DPP

[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

Hints & Solutions


1. (c) Low = 0, high = 15
We have to choose the Ist elements as pivot. Here 120 0 + 15
Mid = =7
is the Ist element. After the Ist pass Ist elements goes to 2
its exact location. A[7] = 33
All the elements greater than 120 goes to right of 120 33 > 2
and lesser elements goes to left side of 120 after Ist Low =0, high = 6;
pass.
0+6
110 30 14 24 70 120 160 180 190 Mid = =3
2
5! 3! A[3] = 17
17 > 2
5! × 3!
0+2
⇒ 720 possible arrangement Low = 0, high = 2, mid = =1
2
∴ C is correct option. A[1] = 5
5>2
2. (d) 0+0
Given Low = 0, high = 0, mid = =0
2
T(n) = [n (log(n3) – logn) + logn] n + logn A[0] = 2
  n3   2==2
= n  log  + logn  n + logn
  n  
5. (a)
= nlogn2 + logn  n + logn
  Two sorted file of size m and n takes O(m + n) time
for merging.
= n2  2logn + nlogn + logn
So, total time = 400 + 100 + 250 + 50 = 800
= 2n2 logn + nlogn + logn ∴ (a) is correct.
= O(n2logn)
∴ option (d) is correct. 6. (3.14 to 3.14)
In best case quick sort algorithm takes nlogn
3. (a) comparisons,
So, 500 × log2500  4482.89
Which takes 50 seconds.
To sort 50 names a minimum of 50(log250) = 282.19
comparisons
∴ 282.19 comparisons are needed.
282.19
This takes = 50  = 3.14 second
4482.89
𝑛 𝑛
Total number of comparisons = (2 – 1) + (2 – 1) +
7. (341 to 341)
(𝑛– 1) Given files
40, 42, 44, 46} 82 – 1 = 81 comparisons
= 2n – 3
82, 44, 46} 90 – 1 = 89 comparisons
∴ option (a) is correct. 82, 90} 172 – 1 = 171 comparisons
So, 81 + 89 + 171 = 341 comparisons
4. (4 to 4)
4

Any issue with DPP, please report by clicking here:- https://forms.gle/t2SzQVvQcs638c4r5


For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if

PW Mobile APP: https://smart.link/7wwosivoicgd4


1

Branch : CSE & IT Batch : Hinglish


Algorithms
'Dynamic Programming' & 'Graph Algorithm' DPP

[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

Hints & Solutions


1. (b) 6. (a)
Time complexity of the dynamic programming
approach is O(n3). Where n is the number of matrices.
Because it contains nested loop iterating over the
matrix dimension to fill in the optimal costs.

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

Any issue with DPP, please report by clicking here:- https://forms.gle/t2SzQVvQcs638c4r5


For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if

PW Mobile APP: https://smart.link/7wwosivoicgd4


1

Branch : CSE & IT Batch : Hinglish


Algorithms
Greedy Method DPP

[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

Hints & Solutions


1. (b)
(i) multiple spanning trees may exist, we have kirchoff
method to count them.
(ii) Spanning tree can’t have negative edge, it can have
any real value.

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.

❑❑❑

Any issue with DPP, please report by clicking here:- https://forms.gle/t2SzQVvQcs638c4r5


For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if

PW Mobile APP: https://smart.link/7wwosivoicgd4


1

Branch : CSE & IT Batch : Hinglish


Algorithms
'Heap Algorithm' & 'Backtracking and DPP
Branch-Bound'
[MCQ] [MCQ]
1. Consider the statements. 3. How many different min-heap are possible with keys
S1: Merge-sort, quick-sort and bubble sort are 1 2 3 4 5?
comparison-based sorting algorithms (a) 5 (b) 6
S2: A reverse-sorted array (ie. ……decreasing order) (c) 7 (d) 8
is always max-heap
(a) Only S1 is true [MCQ]
(b) Only S2 is true 4. What is the maximum number of exchanges required
(c) Both S1 and S2 are true to order an array of 5 elements using the selection sort?
(d) Neither S1 nor S2 is true. ____
(a) 1 (b) 2
[MCQ] (c) 3 (d) 4
2. Consider a binary search tree which is also a complete
binary tree. The problem is to convert the BST which [NAT]
is given into a minheap with the condition that, all the 5. Number of undirected graph (not necessarily
values in the left subtree of a node should be less than connected) can be constructed by given set
all the values in the right subtree of the node. This V = [1, 2, 3, 4] of 4 vertices are ____.
condition is applied on all the nodes in the process of
converting BST into minheap. [NAT]
6. The number of spanning trees of an undirected
completed graph with 7 nodes is _____

[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

Hints & Solutions


1. (c) 4. (d)
S1(true): All are comparison-based sorting algorithms. In selection sort, with each pass, at most one swap is
S2(true): 8, 7, 6, 5 performed, so array with 5 elements will perform 4
passes and with each pass there will be 1 swap.
∴ d is correct.

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

❑❑❑

Any issue with DPP, please report by clicking here:- https://forms.gle/t2SzQVvQcs638c4r5


For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if

PW Mobile APP: https://smart.link/7wwosivoicgd4

You might also like