DAA PY PUT Sol
DAA PY PUT Sol
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)
(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).
(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 .
(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
Now assume it is true for all tree with 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
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.
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
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).
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:
(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:
(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
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:
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
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*.
(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.