0% found this document useful (0 votes)
14 views14 pages

DAA PY PUT Sol

The document contains solutions to various questions related to Design & Analysis of Algorithms for a B. Tech (CSE) examination. It covers topics such as recurrence relations, stable sorting algorithms, B-trees, red-black trees, convex hulls, and various algorithmic approaches like dynamic programming and greedy methods. Additionally, it includes practical problems like sorting, tree manipulations, and optimization problems with detailed explanations and time complexities.

Uploaded by

akshat3000.at
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views14 pages

DAA PY PUT Sol

The document contains solutions to various questions related to Design & Analysis of Algorithms for a B. Tech (CSE) examination. It covers topics such as recurrence relations, stable sorting algorithms, B-trees, red-black trees, convex hulls, and various algorithmic approaches like dynamic programming and greedy methods. Additionally, it includes practical problems like sorting, tree manipulations, and optimization problems with detailed explanations and time complexities.

Uploaded by

akshat3000.at
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Roll No.

of Student

B. Tech (CSE)
Pre-University Theory Examination (Sem. V) 2020-21
Design & Analysis of Algorithms (KCS-503)
SOLUTION
Time: 3Hours Max. Marks:100

Section-A
1. Attempt all parts. All parts carry equal marks. Write answers for each part in short. (2*10=20)

(a) CO1: Solve given recurrence T(n) = 4T(n/2)+n2. K3


Sol: T(n) = 4T(n/2) + n2
= n2 + 4[4T(n/4) + n²/4]
= 2n2 + 16T(n/4)
= ...
= k⋅n2 + 4kT(n/2k)
= ...
The process stops when 2k reaches n.
⇒ k = log2n
⇒ T(n) = O(n2logn)

(b) CO1: What is a stable sort? Name two stable sort algorithms. K1,K2
Sol: Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a
sorting algorithm is stable if whenever there are two records R and S with the same key and with R appearing
before S in
the original list, R will appear before S in the sorted list. Eg: Merge sort, insertion sort

(c) CO2: What is the minimum number of keys in a B tree of order 32 and height 5? K1,K2
Sol: 1. In such a tree, the root would have only 2 children (1 key), since this is the minimum allowed for a
root. All other nodes would have ceil(m/2) children, and ceil(m/2) - 1 keys.
2. For convenience, let c = ceil(m/2).

1 node 1 key at level 1


2 nodes 2 * (c-1) keys at level 2
2*c nodes 2 * c * (c-1) keys at level 3
2*c**2 nodes 2 * c**2 * (c-1) keys at level 4
.. .. ..
.. .. ..
2*c**(h-2) nodes 2 * c**(h-2)*(c-1) keys at level h
2 * [c**(h-1)-1] + 1 = 2 * c**(h-1) - 1 keys total

Hence, total min no of keys at height 4 with order 32 is (1)+(2*15)+(2*16*15)+(2*16*16*15)

(d) CO2: Prove that in n key B tree of height h and t>=2, h<= logt(n+1)/2 K4

Sol: B-tree with a minimum no of keys , there is one key in the root , 2 nodes at depth 1 , 2ti -1 nodes at depth i .

Let h be the height of the tree and n be the no of nodes .

Then which works out to th <= (n+1) / 2


So we take the logt of both sides h <= logt(n+1)/2

(e) CO2: Prove “A red black tree with n internal nodes has height at most 2lg(n+1).” K4

Sol: First we bound the number of nodes in any subtree then we bound the height of any subtree We claim
that any subtree rooted at x has at least 2bh(x) -1 internal nodes where bh(x) is the black height of node
x Proof by induction

bh(x) =0 → x is a leaf . We have 20 -1 = 0

Now assume it is true for all tree with black height < bh(x).

If x is black both subtrees have black height bh(x) -1

If x is red the subtrees have black height bh(x).

Therefore the number of internal nodes in any subtree is n ≥ 2bh(x)-1 -1 + 2bh(x)-1 -1 +1 ≥ 2bh(x) -1

Now let h be the height of our redblack tree At least half the nodes on any single path from root to leaf
must be black if we ignore the root

Thus bh(x) ≥ h/2 and n ≥ 2h/2 -1 . So, n+1 ≥ 2h/2 .

⟹ log( n+1) ≥ h/2 ⟹ h is at most 2log( n+1)

(f) CO3: Define Convex hull. K2

Sol: The Convex Hull is the line completely enclosing a set of points in a plane so that there are no
concavities in the line. More formally, we can describe it as the smallest convex polygon which encloses
a set of points such that each point in the set lies within the polygon or on its perimeter.

(g) CO3: Differentiate between Dijkstra and Bellman Ford algorithm. K1

Sol:
Dijkstra Bellman Ford algorithm.
1. Application of Greedy Method 1. Application of Dynamic Programing
2. Generates a shortest path tree with a 2. It calculates shortest paths in a bottom-up
given source as root. manner

(h) CO4: Discuss the complexity of N-Queen’s method using backtracking.


K4
Sol: The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two
queens attack each other.
O(n^n) is an upper bound on solving n-queens using backtracking. Here n represents the number of
queens and will remain the same for every function call.
K is the row number and function will be called times till k reaches the n.
There if n=8,we have n rows and n queens.
(i) CO4: Differentiate between dynamic and greedy approach. K1
Sol:
DYNAMIC APPROACH GREEDY APPROACH

1. Choose at each step, but the 1. Choose the best local problem at the
choice may depend on the moment and then solve the sub-
solution to sub-problems. problems arising after the choice is
made.
2. Less efficient as compared to a 2. More efficient as compared to a
greedy approach greedy approach
e.g: 0/1 Knapsack e.g: Fractional Knapsack

(j) CO1: Explain why the statement” running time is at least O(n2) is meaningless”? K4

Sol: Let us assume the running time of the algorithm is T(n). Now, by definition,
O-notation gives an upper bound for growth of functions but it doesn’t specify the order of growth.

Therefore, saying T(n) ≥ O(n^2) means growth of T(n) is greater than some upper bound
which is meaningless as we do not have any idea about what we are comparing it with.

Section-B
2. Attempt any five questions from this section. (10*5=50)

(a) (i)CO1: Sort the following array using heap sort technique: {5,13,2,25,7,17,20,8,4}. Discuss the time
complexity of heap sort. K5
Sol:
Time Complexity: Time complexity of heapify is O(Logn). Time complexity of createAndBuildHeap() is O(n)
and overall time complexity of Heap Sort is O(nLogn).

(ii)CO1: Solve : 1. T(n) = T(n/3)+T(2n/3)+O(n)

2. T(n) = T(√n)+O(lgn).
Sol:

1. Shortest path will be most left one, because it operates on lowest value, and the
most right one will be the longest one, that means tree is not balanced.
Elements from shortest path are being divided by 3, so length of this path will be
equal to log3 n. So if number of complete levels of recursion tree for shortest path is
equal to log3 n, that means cost of algorithm for this path will be:
T(n)=c.nlog3n=Ω(nlgn)

(b) CO2: Insert the nodes (15,13,12,16,19,23,5,8) in an empty RBTree and delete the root from the final
tree. K5,K3

Sol:

(c) CO2: Insert (F,S,Q,K,C,L,V,W,M,R,N,P,A,D,Z,E) in an empty B-Tree with t=2.


K2,K3
Sol:

(d) CO3: Find the optimal solution to the fractional knapsack instances n=7 and knapsack capacity M=15
where profits and weights are (10,5,15,7,6,18,3) and (2,3,5,7,1,4,1) respectively. K5,K6
Sol: Given: n = 7, m = 15, (p1, p2… p7) = (10, 5, 15, 7, 6, 18, 3), (w1, w2… w7) = (2, 3, 5, 7, 1, 4, 1)
To prove: Optimal solution that gives maximum profit.
Proof:
Step 1: (To find profit/ weight ratio)
p1/w1 = 10/2 = 5
p2/w2 = 5/3 = 1.67
p3/w3 = 15/5 = 3
p4/w4 = 7/7 = 1
p5/w5 = 6/1 = 6
p6/w6 = 18/4 = 4.5
p7/w7 = 3/1 = 3
Step 2: (Arrange this profit/weight ratio in non-increasing order as n values) Since the highest profit/weight ratio
is 6. That is p5/w5, so 1st value is 5. Second highest profit/weight ratio is 5. That is p1/w1, so 2nd value is 1.
Similarly, calculate such n values and arrange them in non-increasing order.
Order = (5, 1, 6, 3, 7, 2, 4)
Step 3: (To find optimal solution using m = 15 & n = 7)
Consider x5 = 1, profit = 6
Then consider x1 = 1, profit = 10
So weight uptil now = 1 + 2 = 3
Now x6 =1, profit = 18
So total profit = 16 + 18 = 34
And weight uptil now = 3 + 4 =7
Now x3 = 1, profit = 15
So total profit = 34 + 15 = 49
And weight uptil now = 7 + 5 = 12
Now x7 = 1, profit = 3
So total profit = 49 + 3 = 52
And weight uptil now = 12 + 1 = 13
Since m = 15 so we require only 2 units more. Therefore x2 = 2/3
So total profit = 52 + 5 x 2/3 = 52 + 3.33 = 55.3
And weight uptil now = 13 + 3 x 2/3 = 15
Thus, the optimal solution that gives maximum profit is, (1, 2/3, 1, 0, 1, 1, 1)

OR

CO3: Write the Prim’s(connected component based method) algorithm for MST and find MST for
following graph using Prim’s algorithm: K4

Sol:Below we have the complete logic, stepwise, which is followed in prim's algorithm:
Step 1: Keep a track of all the vertices that have been visited and added to the spanning tree.
Step 2: Initially the spanning tree is empty.
Step 3: Choose a random vertex, and add it to the spanning tree. This becomes the root node.
Step 4: Add a new vertex, say x, such that
● x is not in the already built spanning tree.
● x is connected to the built spanning tree using minimum weight edge. (Thus, x can be adjacent to any of
the nodes that have already been added in the spanning tree).
● Adding x to the spanning tree should not form cycles.
Step 5: Repeat the Step 4, till all the vertices of the graph are added to the spanning tree.
Step 6: Print the total cost of the spanning tree.
for given graph:
starting from vertex a: ae->eb->bc->cf->fg->ed total cost: 3+4+2+3+4+5 =21

(e) CO4: Find all pair shortest path for the following graph: K4

Sol:

OR
CO4: What is travelling salesman problem? Find the solution to the following travelling salesman
problem using the branch and bound method. K1,K4

Sol:

(f) CO5: Attempt any one of following: K1,K2


(i) Write Recursive algorithm for FFT.
(ii) Write a short note on the Randomized algorithm.
Sol: (i) Recursive algorithm for FFT is shown below in pseudo code.

(ii) A randomized algorithm is an algorithm that employs a degree of randomness as part of its logic.
The algorithm typically uses uniformly random bits as an auxiliary input to guide its behavior, in the
hope of achieving good performance in the "average case" over all possible choices of random bits
How to analyse Randomized Algorithms?
Some randomized algorithms have deterministic time complexity. For example, this implementation of
Karger’s algorithm has time complexity O(E). Such algorithms are called Monte Carlo Algorithms and
are easier to analyse for the worst case.
On the other hand, time complexity of other randomized algorithms (other than Las Vegas) is dependent
on the value of random variables. Such Randomized algorithms are called Las Vegas Algorithms. These
algorithms are typically analysed for the expected worst case.
To compute expected time taken in the worst case, all possible values of the used random variable needs
to be considered in the worst case and time taken by every possible value needs to be evaluated.
Average of all evaluated times is the expected worst case time complexity. Below facts are generally
helpful in analysis of such algorithms.
Section-C
Attempt any two questions from this section. (15*2=30)

(a) CO5: Write Knuth Morris Pratt algorithm and calculate the prefix function for pattern P = ababaaca
and perform the string matching on the text T = abcaaababaacaaacaaabab. K5

Sol: Knuth Morris Pratt algorithm

Text a b c a a a b a b a a c a a a b a b
Pattern a b a b a a c a - - - - - - - - - -
Prefix 0 0 1 2 1 1 0 1
at position i=6 pattern found.

(b) CO4: Differentiate between Backtracking and Branch and Bound approach. Write an algorithm for
the sum of subset problems using a backtracking approach. Find all possible solutions for the
following instance using the same if m=30, S=<1,2,5,7,8,10,15,20,25>. K5,K6

Sol:

BACKTRACKING BRANCH & BOUND


1. Traverses the state space tree by 1. Traverse the tree in any manner, DFS or
DFS(Depth First Search) manner. BFS.
2. Backtracking involves feasibility 2. Branch-and-Bound involves a bounding
function. function.
3. Backtracking is used for solving Decision 3. Branch-and-Bound is used for solving
Problems. Optimisation Problems.
4. The state space tree is searched until the 4. The optimum solution may be present
solution is obtained. anywhere in the state space tree, so the
5. Backtracking is more efficient. tree needs to be searched completely.
5. Branch-and-Bound is less efficient.
Algo for Sum of Subset Problem :
● Input − The given set and subset, size of set and subset, a total of the subset,
number of elements in the subset and the given sum.
● Output − All possible subsets whose sum is the same as the given sum.
● SubsetSum(set, subset, n, subSize, total, node, sum)
Begin
if total = sum, then
display the subset
//go for finding next subset
subsetSum(set, subset, , subSize-1, total-set[node], node+1, sum)
return
else
for all element i in the set, do
subset[subSize] := set[i]
subSetSum(set, subset, n, subSize+1, total+set[i], i+1, sum)
done
End

Using state space tree we get total 7 solutions for given sub of subset problem m=30,
S=<1,2,5,7,8,10,15,20,25>
i.e. {1,2,7,20}, {1,2,5,7,15}, {2,5,8,15}, {2,8,20}, {5,10,15}, {10,20}, {5,25}

(c) (1) CO5: Describe vertex cover approximation algorithm. Show that the vertex cover problem is a
2-approximation algorithm. K1,K2

Sol: Vertex Cover Approximation Algorithm:

A Vertex Cover of a graph G is a set of vertices such that each edge in G is incident to at least one of
these vertices.

The decision vertex-cover problem was proven NPC. Now, we want to solve the optimal version of the
vertex cover problem, i.e., we want to find a minimum size vertex cover of a given graph. We call such
vertex cover an optimal vertex cover C*.

1. Approx-Vertex-Cover (G = (V, E))


2.{
3. C = empty-set;
4. E'= E;
5. While E' is not empty do
6. {
7. Let (u, v) be any edge in E': (*)
8. Add u and v to C;
9. Remove from E' all edges incident to
10. u or v;
11. }
12. Return C;
13. }

(2) CO5: Define all the classes P, NP, and differentiate between NPComplete and NPHard. K1,K2

Sol: In computer science, there exist several famous unresolved problems, and P=NP is one of the most
studied ones. Until now, the answer to that problem is mainly “no”. And, this is accepted by the
majority of the academic world. We probably wonder why this problem is still not resolved.
In this tutorial, we explain the details of this academic problem. Moreover, we also show both P
complete and NP problems. Then, we also add definitions of NP complete and NP Hard. And in
the end, hopefully, we would have a better understanding of why P= NP is still an open problem.

NP HARD NP COMPLETE
1. NP-Hard problems(say X) can be 1. NP-Complete problems can be
solved if and only if there is a NP- solved by a deterministic
Complete problem(say Y) can be algorithm in polynomial time.
reducible into X in polynomial 2. To solve this problem, it must be
time. both NP and NP-hard problems.
2. To solve this problem, it must be a 3. It is exclusively Decision
NP problem. problem .
3. It is not a Decision problem. 4. Example: Determine whether a
4. Example: Halting problem, Vertex graph has a Hamiltonian cycle,
cover problem, Circuit- Determine whether a Boolean
satisfiability problem, etc. formula is satisfiable or not, etc.

Ms. Swapna Singh


Mr. Shailendra Singh
Ms. Shelly Gupta
Ms. Preeti Gupta
(Subject Teacher) (Moderator)

You might also like