Answer 2019

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Answer Keys

Design & Analysis of Algorithms (CSEN 2201), 2018 – 2019

Group A
1. (i) ---- (c), (ii) --- (d), (iii) --- (b), (iv) --- (a), (v) --- (b), (vi) --- (c), (vii) --- (d), (viii) --- (b),
(ix) --- (c), (x) --- (d)
Solve the following recurrence by recurrence tree method and
2. (a) Consider the marks division as 2 + 2 verify it with forward substitution method, T(n) = 3T(n/4) + cn^2

For the verification part of this question, if the students answer the question correctly by
mentioning only substitution, instead of forward substitution, then full marks may be given
3 (a)
If you do this as a tournament arranged as a binary tree where as many teams as possible play in
the 1st level and then in the 2nd level etc. then such a tree will have a height of  lgn . In other
words the champion (the smallest element) has to play at most  lg n  games in order to be the
champion. If we can track which numbers were the direct losers to the champion, then the
smallest of them is the 2nd order statistic. To find the minimum of  lgn  numbers it will take
 lgn  -1 more comparisons.

You are provided as input an even positive integer n > 1 and an unsorted sequence S of n integers. You would like to
find both the second order statistic and the nth order statistic of S. Show that they can
be found out in (3 n/2 + lgn - 3) number of pair-wise comparisons.

click above
Now you can find out the nth order statistic in the same way. Total number of comparisons
required to find the 1st order statistic and nth order statistic simultaneously are: n/2 (1st level
out
comparisons) + (n/2 - 1) (to find the winner our of the larger numbers of 1st level) + (n/2 - 1) (to
find the winner out of the smaller numbers of 1st level) = 3n/2 - 2
So total number of comparisons to find the second order and nth order statistic along with the
earlier case is as follows: (3n/2 - 2) + (  lgn  -1) = 3 n/2 +  lgn  - 3

3. (b)

Average case analysis of Binary Search


One Approach
Let’s first assume that the number of elements in the array is n = 2k – 1, where k = log2(n+1).
Since each position of the array is equally likely, so we have n situations each with prob. 1/n.
Let’s further assume that search is always successful.
Now, let’s consider that Sc be the number of situations for which the algo does c comparisons.
Thus, S1 = 1 = 20 = 21-1, S2 = 2 = 21 = 22-1, S3 = 4 = 22 = 23-1, …., Sc = 2c-1, c<=k
So, the avg. case complexity of Binary Search can be represented by following recurrence:

TBSA(n) = 1 / nc 1 S c * c
k

Upto this point is algorithm, then it is completely mathematics to solve the recurrence and after
solving it we get TBSA(n) = O(log n)
Another Approach
Suppose when you first compare the key with the middle element, it matches, then the number of
comparisons required to find the key is only 1. Number of positions for which only one
comparison is required is therefore 1.
Now if you do not find the key in the 1st comparison but you find it in the 2nd comparison, then
number of such positions = 2 (middle elements of the half-arrays).
The number of positions for which 3 comparisons are reqd. is 4.

Hence average number of comparisons


≤ (1/n) [(1.1 + 2.2 + 4.3 + 8. 4 + …+ 2 (lgn – 1) lgn) + lgn] = (S + lg n) / n (say)

(highlighted term is for not finding the key, for which lh n comparisons are required)

Hence S = ∑ k = 1 to lg n k.2 (k -1). This is a Arithmetico-geometric (AP_GP) series. You are


supposed to know how to solve it.

S = 1.1 + 2.2 + 4.3 + 8. 4 + … + 2 (lgn – 1) lgn)


2S = 2.1 + 4.2 + 8.3 + 16.4 +………………..+ 2lgn lgn

S – 2S = 1 – [2(2–1) + 4(3-2) + 8(4-3) + 16(5-4) + ………] - 2lgn lgn

-S = 1 – [2 + 4 + 8 + 16 + …. + 2 (lgn – 1) ] - 2lgn lgn


-S = 1 – [2(2 (lgn – 1) -1) / (2 -1)] – n lgn = 1 – 2(n/2 – 1) – n lg n = 1 – n + 2 – n lg n
S = n lg n + n – 3

Hence average case is (1/n) (n lg n + n + lg n – 3) = O(lg n)


If just gives the correct answer, i.e. O (lg n), 1 mark may be given.
4.

M matrix:

5250 8750 9375 9250


3000 3750 3750
1500 2250
3000
S matrix:
1 2 2 2
2 2 2
3 4
4
Parenthesized expression:
((A1A2)((A3A4)A5))
5. a) Students are supposed to show how LCS follows dynamic programming technique

b)
Method (the Greedy method): The selection policy (of which best pair of arrays to merge next) is to
choose the two shortest remaining arrays.
Implementation:
Need a data structure to store the lengths of the arrays, to find the shortest 2 arrays at any time, to
delete those lengths, and insert in a new length (for the newly merged array).
In essence, the data structure has to support delete-min and insert. Clearly, a min- heap is ideal.
Time complexity of the algorithm: The algorithm iterates (n-1) times. At every iteration two delete-mins
and one insert is performed. The 3 operations take O(log n) in each iteration.
Thus the total time is O(nlog n) for the while loop + O(n) for initial heap construction.
That is, the total time is O(nlog n).
Provide reasons for for why it will/ will not produce an optimal solution.
Ans:
The case is exactly similar as generation of Huffman code and hence since the strategy is exactly similar
as Huffman code generation it will give optimal result.
Assumption Merging two sorted arrays of length m and n will require O(m + n) time
or more precisely - c(m + n) time.
Consider the length of each array as the frequency of Huffman code. Then merging the two smallest
arrays is like merging the 2 Huffman codes of smallest frequency and rest follows easily from that. Note
that the array which merges early will be used for merging more number of times and hence it helps if it
is smaller in size. In Huffman code the character that has less frequency is merged early so that when it
gets a longer code it is multiplied by a smaller frequency.

Note: Consider the marks division for this question as 2+2

c) Students can show the priority queues in various ways. One such way can be as follows:

Nodes a b c d e f g h i
Key ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

Π NIL NIL NIL NIL NIL NIL NIL NIL NIL

Nodes a b c d e f g h i
Key 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞

Π NIL a NIL NIL NIL NIL NIL a NIL

Nodes a b c d e f g h i
Key 0 4 8 ∞ ∞ ∞ ∞ 8 ∞

Π NIL a b NIL NIL NIL NIL a NIL

In this way it can be continued till the end


Cost of the MST will be 37

6. marks distribution is (4+4)+4. But as per the nature of the question, it should be 4+(4+4).
a) Amortized cost = O(1).
Note: Since the method is not mentioned, any method can be accepted.
b)
Find out the number of components for the following graph using union-find (union by rank),

Explain the subroutines used in detail with their time complexities.


7.a)

Socks Shorts Pants shoes watch shirt belt tie


jacket

b) Given a graph G = (V, E), we want to find a shortest path from a given source vertex s ∈ V to
each vertex v ∈ V .
Shortest-paths algorithms typically rely on the property that a shortest path between
two vertices contains other shortest paths within it.

Given a weighted, directed graph G = (V, E), let p = <v0, v1, .., vk> be a shortest path from v0 to
vk.
p0i pij pjk
If we decompose path p into v0 vi vj vk, then we have that w(p) = w(p0i) + w(pij)
+ w(pjk). Now, assume that there is a path p’ij from vi to vj with weight w(p’ij) < w(pij). Then the
weight of the new path p’, i.e. w(p’) < w(p), which contradicts the assumption that p is a shortest
path from v0 to vk
8. a) Define flow in a flow network. What is an s-t cut of a flow network? Prove that flow of any s-t cut is equal to
the flow of the network.
8. b) 9

4 5 6

1 2

Only the newly created link is marked bold. The rest of the tree will be same.

9. a) Explain with a small diagram how you can show the easiness or hardness of a new problem using
the easiness or hardness result of a known problem

Show that the Clique Decision Problem is NP-hard by using the result the 3-CNF-SAT problem is
b) computationally hard
CNF formula(f) => Graph (G)
● It has a vertex for every pair (v, c).
● v is a variable or its negation (v’),
● c is a clause in the formula f that contains v.
● Edges are there between (v, c) and (u, d) if , c ≠ d and u ≠ v’
Means: between any 2 literals in different clauses, who are not each other’s negation.
● If k denotes the no. of clauses in the CNF formula (f), then the k-vertex clique in
the graph represents ways of assigning truth values to some of its variables in order
to satisfy the formula.

● f is satisfiable iff a k-clique exists.

c) Define the following terms in the context of optimization problems -


(i) Approximation scheme (AS), (ii) PTAS, (iii) FPTAS.
i) An approximation scheme for an optimization problem is an approximation algorithm
that takes as input not only an instance of the problem, but also a value ε > 0 such that for any
fixed ε, the scheme is a (1+ ε) -approximation algorithm.
ii) We say that an approximation scheme is a polynomial-time approximation scheme if for
any fixed ε > 0, the scheme runs in time polynomial in the size n of its input instance.
iii) We say that an approximation scheme is a fully polynomial-time approximation scheme if
it is an approximation scheme and its running time is polynomial in both 1/ ε and the size n of
the input instance.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy