Name & Roll No.
NATIONAL INSTITUTE OF TECHVOLOGY WARANGAL
Department of Computer Science and Engineering
END Examinations : AY 2024-25 - November 2024
. Duration: 150min
Date: 23/11/2024 Marks: 40
Progranm: BTech, CSE Class: IIYear I Sem Sections: A and B
Course Code: CS204
Course Name: Design and Analysis of Algorithms
Note: The question paper has two pages. Answer ll the questions.
1. Define the activity/interval selection problem as discussed in class. For each of the following greedy
strategies to solve the activity selection problem, determine whether the greedy choice is correct. If
the strategy is incorrect, provide a counterexampie to demonstrate its failure. If tlhe strategy works,
state the running time of the corresponding greedy algorithm. (1 + 5x l=6 M)
(a) Sclcct the activity with the shortest duration from anong thosc compatible with the previously
selected activities.
(5) Select the remaining compatible activity with theearliest finish tine.
(c) Select the compatible activity that overlaps he fewest other remaining activities.
Select the remaining compatible activity with the earliest start time.
(e) Select the last activity to start that is compatible with all previously selected activities.
2 Let G(V, E) be a simple, undirected, conmected weighted graplh with no negative weights. If tlhe
Dijkstra's algorithm is applied on Gwith |E(G)|=0(V(G)) then analyze the running time of it in
each of the following cases and represent its running time in tern1s of |V(G)| only. (3 x 1.5 = 4.5 M)
(a) Priority queue is implemented as an array
(b) Priority queue is inplemented as a Fibonacei hcap
(c) Priority queue is implemented as a minimum binary heap.
}. Say you have an art gallery with many hallways and turns., Your gallery is displaying very valuable
paintings, and you want to keep them secure. You are planning to install security cameras in each
lhallway so that tlhe cameras have every painting in view. If there is a security camera in a hallway, it
Can see every painting in the hallway. If there is a camera in the corner wlhere two or more hallways
lneet (the turn), it can view paintings in all thoe hallways. Assume that you have decided to place
art gallery
ie security cameras at corners only. Where should the cameras be placed so that the
Tequires the minimum nunber of cameraS and al hallways will have at least one camera?
(a) Model the above problen as a grapl1 and specify its vertex and edge sets. (1 M)
wh graph problem you are trying to solve aud state its optimization versio. (1 M)
(e) What is the complexity class of tlhe optimization version of the problen youare solving (1 M)
(d) What is the complexity class of the problem if G is a tree? (1 M)
A In order to prove the NIP-hardness of Clique Decision Problem(CDP), CNF-SAT is reduced to CDP.
Let <a, a), d?,... , , > be an input for CNF-SAT, wlhere o = VjKiK, C. The redction algorithm
nrodhces in polvnomial tinne an instance < G,r> for CDP that fulfills the following
requirenent:
ois satisfiable if and only f G has a chque of size at least r.
<( V; Vr3) A(r1 V2)A(2 VI3) ^ (2; VI3) A(T4), T|, T2, T3, T4 > is an instance of the CNF-SAT
and <G.r> be the corresponding instance of CDP, then determine the following. (5 x 1 = 5M)
(a) Graplh G constructed from the given instance of CNF-SAT.
(b) |V(G)| = and |E(G)] =
(c) r
(d) Maximun degree of G =
(e) Size of maximum possible clique in G=
B. State master theorem and obtain bounds for the following recurrences. (1 +4x l=5M)
(a) T(n) = 2T(;) + (n)
(b) T(n) = 27T(G)+ log n
(c) T(n) = 2T() +O)
(d) T(n) = niT(n) + n
i. Design both an iterative and a recursive divide-and-conquer algorithn for a problem of your choice.
Demonstrate that the running times of the two algorithms are equal. (5 M)
7. Imagine that you wish to exchange one currency for another. You realize that instead of directly
exchanging one currency for another, you might achieve a better outcome by making a series of trades
through other currencies, ultimately obtaining the currency you want. Suppose you can trade among
ndifferent currencies, numbered 1,2,...,n, where you start with currency l and wish to end up
with currency n. For each pair of currencies i and j, you are given an exchange rate rij, meaning
that if you start with d units of currency i, you can trade them for d- rij units of currency j. A
sequence of trades may incur a commission, c, which depends on the number of trades &you make.
Let ck = 0 for allk = 1,2,...,n. Under this condition, deternmine whether the problem of fnding
the best sequence of exchanges from currency 1 to currency n exhibits optimal substructure. Also,
deternmine that if commissions c are arbitrary values, the problem of fnding the best sequence of
Cxchanges from currency 1 to currcncy n cxhibit optimal substructure. A corrcct answer in cither
case without proper justification will receive zero marks. (4 M)
8. Consider a variant of matrix chin multiplication problenm (VarMCMP) in which the goal is to
number
parenthesize the sequence of matrices so as to maximize, rather than minimize, the
of scalar multiplications.
(1.5 M)
(a) Deternine whether the Var MCMP has an optinal substructure and justity.
(1.5 M)
(6) Give a recursive algorithmfor solviug VarMCMP and analyze its running tne.
(0 Does there exist O(n) time iterative algorithn to solve VarMCMP? Justity. (1.5M)
(4) Determine the maximum unber of sealar mmtinieations required to ind the product of a cham
(2 M)
of four natrices with dimesions 18 x 8, 8 x l0. 10 × 15 and 15 X5.
Know the true value of time; satch, no
No idleness, no laziness,
seize, and enjy every noment of it.
procrastinatio: never put off Lill Lomnorw what you can do today.
Name & Roll No
NAIONALINSTITUIE OF IECHNOLOGY WARANGAL
Departnment of Connputer Science and Engincering
MID Examinatios : ) 2024-25 -September 2024
Dae 23 09 2024 Marks: 30 Duration: 120 min
Program: BIech. CSE Class: II Year ISem Section: A
Course Name: Design and Analysis of Algorithms Course Code: CS204
Note: The question aper contains tuo pages. Answer all the questions.
A. Let A(r) = a0+a1rtapr'+...+anz" be a polynomial of degree n. where co. 4):... an are constants.
Evaluate the polynen:ial A(r) = 4r' +52? +6r +7 at r =2 using the Horner's method. Represent
the runningtime of tle Honer's nethod toevaluate a polynomial 4(r) of degree nat a givenpoint
I =I) using -notation and justify. (3 M)
2. Algorithm Something (A. 1. n)
Step 1. for i =1to n -1
Step 2. for j=n downto i +1
Step 3. if Aj] < A;j - 1]
Step 4. Saap(AG].Aj - 1])
(a) I: an n clemcn: intcger array A[1...n] is providcd as input to the Something) algorithm then
what is the opnt of the algor.thm? (1 )
(b) State clearly a lop invariant for the for loop in steps 2-4. (1 M)
(1 1)
(c) State clearly a lcop invariant for the for loop in steps 1-4.
restricted to permutat ios
3. Deternine the runn.ng time of the insertion sort algorithm if the inputs are (2 M)
iploce? Justifv.
of 1.2. . . . .n with at 1nost n (log n)' inversions. Is insertion sort
operations using -notation.
A. Represent the (worst-case) running :iines of the following
(1 M)
(a) Iaserting an clment into a qucue. (1M)
nodes. o )
(b) Searching for an element in alinked list with n (1 M)
rooted binary searcl1 tree with nodes. O?
() Fiding the ua x i nelenent iu a
aximrsubaitav
noll-recuusive, linear-tine algoithn 0r the
Ae le lollowing iceas to develop a progress toward the ight, keepng track of
the
problom. Start at the left end of the array, and
a maxiuJ subarav of 1|1. . exteud the auswer to
1haximuII Subarav seen so far. KLowiug laxmum
index t 1 by usiug the following observation a
11d a llaXi111U1) subaay endiug at
bubarray of A{1...,+ 1] is either.
AmAxiIun Nbaay of A||..J), or
9 Asubaray A....j + 1), for soe1<S)+1. kuowing a
time basedl on
Deterniue a maxinum subrray o tlhe fom A.. . J + 1| in coIstant (3A1)
axinu subarray cnding at index J
6. Supposc we have tour algoithns or a problem at hand In the tollowing, algorithns, ansiNe that
thetine for divxdhg tlhe problenn nto subpoblems is O).
lHere, problem size denotes the size of the inpt to the problem IWrite a recurence for each case.
and obtain a Obomd on the ning ime Which of the below four algorith ms is asymptotically
the best to solve 1' Justitv ( l + J-5M)
a). Algorthm Isols the problem by dividing the problem ito 9 subproblens, ech one of wlich
is onethird the size of the poblent. It then teursively solves cach subproblem and combines the
solutions in quadratic time (Lt.. quad1tic in the size of the problem). 9l
b). Alyornthm 2 solves the problen of size n by dividing it intoone subproblen of size n - 1. It then
recursively solves the subproblem and combines the solutions in O(1) time. 0
c). Algornthm 3solves the problem by dividing the problem into 216 subproblems, each one of which
0s one sixth the size of the problem. It then recursively solves each subproblemand combines the
solutions in quadratic time.
A). Algorthm 4 solves the problem by dividing the problen into one subprot-lem of size one-sixth
the size of the problen. It then recursively solves subproblenm and combines the solutions in linear
time.
j. Algorithm Display (n)
if n =1
print " I admire honesty more than any other trait."
return 0
else
for i 1 to 5
for jt- 1 to 10
for k 1to n
print " I admire honesty more than any other trait. n
for i t-1 to 3 Vn
Display(|lVn])
Let T(n) bethe running time of Display() algorithm when n is provided as input. Derive a recurrence
for T(n) and represent it's running time using -notation. For each of the following statements, state
either True or False. (2 +4 x =4 M)
(a) T(n) = w(n log n) and Display() is an ezponential time algorithm
(b) T(n) = 0(2" ) and Display() is an erponential time algorithm
(c) T(n) = 0(3") and Display() is a polynomial time algorithm r
(d) T(n) = (n log log n) and Display() is an ezponential time algorithnc
index &such that:
E. Anarray A of ncle1ents is said to be unimodal if there cxists an
" A|1]< A(2] <- <A|k] and A|k] > A|k +1] > A|n]
anc A|k| represents the
In other words, the array is first )on-decreasine and then non-inereasing,
peak clement of tlhe array.
the peak clemcnt of an unimodal array. (1 M)
(4) Desgi a divide and conqucr algorithm tÍ ind
combiue step (if auy) in your algorithm Aso
() Jndicale the divide step, the conquer step and tWe (1 M)
detenmine the annount of work done in divide step. T{,
contaiIs n elelients.
7 14 ( ) be the wotst-case niup tiue of vour aleonth) when the aray (1}M)
Deive aecunence for T(u) and rep esent it using O otatiol ofn
non negative in egers I and y (3M))
7 e sudocode for Ecids algoithm for fiudine CCD of two
Also prove the corIectness of the Euclid's GCD alyorithm.
pretly much
them happier than
"People should pursue what they're passionate about. That uwill make
anythng else.
NATIONAL INSTITUTEOF TECINOLOGY WARANGAL
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERIMG
I| BTech CSE (Section A). I Seester Minor 2Examiation
Design and Analysis of Algorithms (C8204)
Date: 29-10 2024 Duration: 60 minutes Max. Marks: 15
Note: Anstrer all questions. Wherever justification is required, correct answers without proper
justification wvill receive zero marks.
1. Derive a recurrence for the running tine of QUICKSORT algorithm discussed in the class under the
following conditions and represent it's rumning tine tusing ) notation. (2 M)
ja) If the PARTITION algorithm of QUICKSORT tses 4th smallest clement as pivot every time. (o(lgn)
c) lf the PARTITION algorithm of QUICKSORT Ises median element as pivot every time. o laloy)
2. Consider avariant of optinal binary search tree (OBST) problem discussed in the class. Also consider
the terminology and notations used in the class to arrive at a dynamie programmning algorithm to
solve the OBST problem.
(a) Define a general subproblem of OBST problem. (0.5 M)
(b) Give a recursive formulation for determining solution of OBST problemz (1 M)
(c) Deternmine the running time of recursive algorithm based on the recursive formulation given in
(b) if you are given n distinct keys and the memoization is not tused. (4log (1 M)
3. For each of the questions below state either True or False and justify. (6 x 0.5 -3 M)
(a) Number of subproblems solved in memoization solution and the dynamic programming solution
to a problem is always same.
(b) 0-1 knapsack problem is in conmplexity class P.
(c) In the selection problem discussed in class, instead of dividing the array into groups of five
elenents, it is divided into groups of nine elenents, yet the algorithm still runs in linear time.
-(d) The size of the optimal independent set of a rooted trece T is the maximun1 of the total number
of vertices at tlhe odd levels and the total umber of vertices at the eveu levels of T.
(e) The number of different ways to parenthesize a chain of six matrices is 40.
(f) There exists an O(1)-time algorithm to solve the maximun clique problem on trees.
4. Let Abe an algorithm for solving the large integer multiplication problem. Algorithm A solves a
problem of size n by dividing it into t subprob.en1s of size . recuusively solving each snbproblem,
takes constant
ad theu combiuing the solutious in O(n) tiue. AIso, assune that the divide step
A to be asynptotically more efficient than
tine. Determine the maxiun valse of t for algorithn
Karatsuba's algorithm. (1 M)
(22) already in sorted order. Cive
5. Let X|1...n and Y[1...n] be two arravs, cach contaiuing n umbers
aays N and . / (2.5 M)
an O(log n)-tine algorithn to fiud the wediau of all 2n elennents in
Justify. (Nt) ( Ml)
6. Is it possible to make the QuickSort run iu liner time in the worst- case?
addition to a prie p for ech rod.
I. Cousider a modification of the rod-cuttiue problem in which, in
solution is ow the sum of the prices
e h cut incurs a fixed COst of c. The reveue aSociated with a
problem exhibit optinnal substructure?
of the pieces minnus the costs of making the euts. Does this (1.5 M)
Justify.
o fivd th Fibouacei uuuuber? Justify. (1.5 M)
O1D0Cs there exist a polynomial time algorithuu
of their dreams.
"The future belongs to those uobo blieve in the beauty
WARANGAL
NATIONAL INSTITUTE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE& ENGINEERIMG
II BTech CSE (Section A), ISemester - Minor 1Examination
Design and Analysis of Algorithms (CS204)
Max. Marks: 10
Duration: 50 minutes
Date: 23-08-2024
required, corTect answers without proper
Note: Answer all guestions. Wherever justification is
justification will receive zero marks.
A|i in the array, the elemnent is at most
1. An arrav Ais said to be almost k-sorted if for every element
sorted array. Iu other words, an elenent that would
k positions away fromits correct position in the
in any position from i- k toi+k in the almost
be in position i in acompletely sorted array can be
elements then represent the running time of
k-sorted array. If Ais an almost 5-sorted array with n
Ausing -notation.
(1 M)
insertion sort on
arrangement g1. g2. -g12 of the
2. Rank the following functions by order of growth, that is find an (2.5 M)
O(g3)... 911 = 0(gi2).
functions satisfying g1 = 0(g2), g2 =
2 n: >1log 2i; n0 L
(Vn) (log n)'; (log n)00, non; 5": (0.3)";
nlog Jog n. (n+ 1)!
integer n is defined as:
3. The iterated logarithm log* n for a positive
if n<1,
log" n =
|1+ log' (log, n) if n > 1.
(1 M)
State true or false and justify: log (log* n) = O(log* log n).
False and justify. (5 x 0.5=2.5 M)
4. For each of the questions below state eit her True or
1 (a) Running time of insertion sort is 2(n').
(b) The following algorithm runs in polynomialtime.
Algorithm Factorial(n)
if n = 0 return 1
else retiurn n × Factorial(n 1)
functions. If f(n) = O(y(n)) then 3/() = O(39(")).
(c) Let f(n) and g(u) be asymptotically positive with n nodes can be found in
rooted binary search tree
(d) Minimum element of an arbitrary
O(log n) time.
class P.
T (e) Matrix multiplication problem is in complexity
displayed
"Hopelessness is the biggest enemy of success" is
5. Give a recurrence for the number of times asymptotically tight
n is input size and obtain an
in the Dispaly() algorithm given below, whennumber depends upon ourselves"
of tines "Happiness
solution toyour recurrence. Alsorepresent the (3 )
is provided as input.
is printed in the algorithn using -notation when n
Algorithm Display (n)
if n=1
succesSs."
print "Hopelessness is the biggest enemy of
return 1
else
for(i =1; i<n; i=i+ 3)
print"Happiness depends upon ourselves. "
for(j = 1; j< n; j=jx4)
for(k = l; k< n; k + +) of success.
Print "Hopelessness is the biggest enemy
Display (n - 1)
achieve il.
# reater vou 'll feel when you
Zhe harder you work for somethino