QP3
QP3
B.Tech Degree S6 (R, S) / S6 (PT) (R) Examination June 2023 (2019 Scheme)
PART A
1. Show that for any real constants a and b, where b > 0, (n + a)b = O(nb )
Ans:
We need to find c and n0 such that (n + a)b ≤ c n b, for all n>= n0
(n + a) ≤ (n + |a|)
(n + a)b ≤ (n + |a|)b
|𝑎|
(n + |a|)b = nb (1 + 𝑛 )b
|𝑎| b
(1 + ) ≤c
𝑛
Balance Factor
Balance Factor of a node = height of left subtree – height of right subtree
In an AVL tree balance factor of every node is -1,0 or +1
Otherwise the tree will be unbalanced and need to be balanced.
Ans:
A, B, C, D, E
A, B, D, C, E
5. Write the control abstraction of divide and conquer strategy
Ans:
Algorithm DAndC(P)
{
if Small(P) then
return S(P)
else
{
Divide P into smaller instances P1, P2, . . . . Pk, k≥1;
apply DAndC to each of these sub-problems;
return Combine(DAndC(P1), DAndC(P2), . . . . , DAndC(Pk));
}
}
3 Colorable graph
PART B
11.
a) Define Big Oh, Big Omega and Theta notations and illustrate them graphically.
Ans:
Asymptotic Notations
It is the mathematical notations to represent frequency count.
Big Oh (O)
The function f(n) = O(g(n)) iff there exists 2 positive constants c and
n0 such that 0 ≤ f(n) ≤ c g(n) for all n ≥ n0
It is the measure of longest amount of time taken by an
algorithm(Worst case).
It is asymptotically tight upper bound
Omega (Ω)
The function f(n) = Ω (g(n)) iff there exists 2 positive constant c and n0
such that f(n) ≥ c g(n) ≥ 0 for all n ≥ n0
It is the measure of smallest amount of time taken by an
algorithm(Best case).
It is asymptotically tight lower bound
Theta (Ɵ)
The function f(n) = Ɵ (g(n)) iff there exists 3 positive constants c1, c2
and n0 such that 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0
It is the measure of average amount of time taken by an
algorithm(Average case).
ii)
Frequency count of first for loop = n/c
Frequency count of second for loop = n/c
Total frequency count = 2n/c
Time complexity = O(n )
12.
a) Find the best case, worst case and average case time complexity of binary search
Ans:
b) Find the time complexity of following function using recursion tree method
(i) T(n) = 2 T(n/2) + n2
(ii) T(n) = T(n/3) + T(2n/3) +n
Ans:
Find(3) will return 1, which is the root of the tree that 3 belongs
Find(6) will return 6, which is the root of the tree that 6 belongs
Find Algorithm
Algorithm Find(n)
1. while nparent != NULL do
1.1 n = nparent
2. return n
Worst case Time Complexity = O(d), where d is the depth of the tree
o Union Operation
Join two subsets into a single subset.
Here first we have to check if the two subsets belong to same set. If no, then
we cannot perform union
i 1 2 3 4 5 6 7
P -1 1 1 1 6 1 6
Union Algorithm
Algorithm Union(a, b)
1. X =Find(a)
2. Y = Find(b)
3. If X != Y then
1. Yparent = X
Worst case Time Complexity = O(d), where d is the depth of the tree
14.
a) Write DFS algorithm for graph traversal. Also derive its time complexity.
Ans:
Algorithm DFS(G, u)
1. Mark vertex u as visited
2. For each adjacent vertex v of u
2.1 if v is not visited
2.1.1 DFS(G, v)
Algorithm main(G,u)
1. Set all nodes are unvisited.
2. DFS(G, u)
3. For any node x which is not yet visited
3.1 DFS(G, x)
Complexity
If the graph is represented as an adjacency list
Each vertex is visited atmost once. So the time devoted is O(V)
Each adjacency list is scanned atmost once. So the time devoted is
O(E)
Time complexity of DFS = O(V + E).
If the graph is represented as an adjacency matrix
There are V2 entries in the adjacency matrix. Each entry is checked
once.
Time complexity of DFS = O(V2)
b) Find the strongly connected components of the given directed graph.
Ans:
PASS-1
Perform a depth first search on the whole graph. If a vertex has no unvisited neighbor,
then push this vertex to the stack.
Final stack will look like:
PASS-2
Now reverse the original graph.
Again pop the next item from the stack and if it is unvisited perform DFS. It will
generate the next strongly connected component.
Again pop the next item from the stack and if it is unvisited perform DFS. It will
generate the next strongly connected component.
0-3-2-1
4-6-5
7
15.
a) Explain 2- way merge sort algorithm with an example and derive its time complexity
Ans:
Complexity
T(n) = a if n=1
2 T(n/2) + cn Otherwise
b) Find the optimal solution for the following Fractional Knapsack problem. Given the
number of items(n) = 7, capacity of sack(m) = 15,
W={1, 3, 5, 4, 1, 3, 2} and P = {10, 15, 7, 8, 9, 4}
Ans:
There are 7 weights and 6 profits. So we cannot solve this problem.
16.
Ans:
This is minimum cost spanning tree and its cost = 21
b) Apply Dijikstra’s algorithm for finding the shortest path from vertex A to all other
vertices.
Ans:
A B C D E F G
A 0 ∞ ∞ ∞ ∞ ∞ ∞
B 5 ∞ ∞ ∞ ∞ ∞
C 4 12 13 ∞ ∞
D 6 6 13 10 ∞
F 10 10 18
E 10 13
G 13
C D C E
17.
a) Find the optimal parenthesis of matrix chain product whose sequence of dimensions
is 5 x 4, 4 x 6, 6 x 2, 2 x 7
Ans:
18.
a) Define TSP problem. Apply branch and bound algorithm for solving TSP.
Ans:
Adjacency Matrix : ∞ 10 50 45
10 ∞ 25 25
50 25 ∞ 40
45 25 40 ∞
b) Write Floyd Warshall’s algorithm for finding all pairs shortest path algorithm.
Ans:
Algorithm FloydWarshall(cost[][], n)
{
for i=1 to n do
for j=1 to n do
D[i, j] = cost[i, j]
for k := 1 to n do
for i := 1 to n do
for j := 1 to n do
D[i, j] = min{D[i, j] , D[i, k] + D[k, j]
Return D
}
Time Complexity
o Floyd Warshall Algorithm consists of three loops over all the nodes. Each
loop has constant complexities.
o Hence, the time complexity of Floyd Warshall algorithm = O(n3), where n is
the number of nodes in the given graph.
19.
20.
Algorithm findingA_LV(A, n)
{
repeat
{
Randomly choose one element out of n elements
}until(‘a’ is found)
}
The expected number of trials before success is 2.
Therefore the time complexity = O(1)
Algorithm findingA_MC(A, n, k )
{
i=0;
repeat
{
Randomly select one element out of n elements
i=i+1;
}until(i=k or ‘a’ is found);
}
This algorithm does not guarantee success, but the run time is bounded.
The number of iterations is always less than or equal to k.
Therefore the time complexity = O(k)
Example:
Consider an unsorted array : [3, 6, 8, 10, 1, 2, 4]
Randomly choose one element as pivot element with the condition that n/4 elements
are greater than that pivot element and n/4 elements are less than that pivot element.
Suppose we select element 4 as the pivot element. Here 3 elements are greater than 4
and 3 elements are less than 4.
n/4 = 7/4 ≈ 1
So the condition satisfied and the element 4 is the pivot element.
Swap the pivot with the 1st element of the array.
Now the array is : [4, 6, 8, 10, 1, 2, 3]
Partition the array
4 6 8 10 1 2 3
low high
4 3 8 10 1 2 6
low high
4 3 8 10 1 2 6
low high
4 3 2 10 1 8 6
low high
4 3 2 10 1 8 6
low high
4 3 2 1 10 8 6
low high
4 3 2 1 10 8 6
high low
1 3 2 4 10 8 6
high low
1 3 2 4 10 8 6
Now the location of 4 is fixed. It partition the array into 2 subarrays
Subarray1: [1, 3, 2]
Subarray2: [10, 8, 6]
Then recursively call the two subarrays and perform the above operations.
First consider the subarray1: [1, 3, 2]
Suppose 2 is the randomly selected pivot element. Swap it with the 1st element in that
array. Now the array becomes [2, 3, 1].
Partition this array
2 3 1
low high
2 1 3
low high
2 1 3
high low
1 2 3
high low
1 2 3
Now 2 is placed in its actual location. It partition the array in to 2 subarrays. These
subarrays contain only one element. So no need for further sorting.
8 10 6
low high
8 6 10
low high
8 6 10
high low
6 8 10
high low
6 8 10
Now 8 is placed in its actual location. It partition the array in to 2 subarrays. These
subarrays contain only one element. So no need for further sorting.
1 2 3 6 8 10