QP2
QP2
Sixth Semester B.Tech Degree Supplementary Examination May 2023 (2019 Scheme)
Course Code: CST306
Course Name: ALGORITHM ANALYSIS AND DESIGN
PART A
1. Let T(𝑛)=7𝑛+4.Prove that this is of the order of 𝛺(𝑛).
Ans:
f(n) = 7𝑛+4
7𝑛+4 ≥ 7 n for all n ≥ 1
Here c=7 n0=1 g(n)= n
7𝑛+4 = Ω(n)
a) T(𝑛)=8𝑇(𝑛/2)+𝑛2
b) T(𝑛)=2𝑇(𝑛/2)+𝑛
Ans:
a) T(𝑛)=8𝑇(𝑛/2)+𝑛2
a=8 b=2 n2 =Ɵ (n2 log0(n)) k=2 p=0
bk =22 = 4
Here If a>𝑏 𝑘 then T(n) = 𝜃(n (log ba)) = 𝜃(n (log 28)) = 𝜃(n3)
b) T(𝑛)=2𝑇(𝑛/2)+𝑛
a=2 b=2 n =Ɵ (n1 log0(n)) k=1 p=0
k 1
b =2 = 2
Here a= bk and p>-1
T(n) = Ɵ(n(log b a) logp+1(n))
= Ɵ(n(log 2 2) log1(n))
= Ɵ(nlog(n))
o Find Operation
Determine which subset a particular element is in.
This will return the representative(root) of the set that the element belongs.
This can be used for determining if two elements are in the same subset.
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
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
4. Find any ONE topological ordering of the following the graph
Ans:
A, B, C, D, E, F
5. Give the control abstraction of Divide and Conquer algorithm design 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));
}
}
6. Apply greedy algorithm for fractional knapsack to find the optimal ordering for loading
the items in the knapsack. Let the knapsack capacity, M=15
Ans:
m = 15
n=3
i { 1, 2, 3 }
P = {24, 18, 20 }
W = {8, 9, 5 }
P/W= {3, 2, 4 }
3 Colorable graph
PART B
11.
a) Define the asymptotic notations: Big Oh, Big Omega, Theta, little oh and little omega
Ans:
Asymptotic Notations
It is the mathematical notations to represent frequency count. 5 types of
asymptotic notations
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).
Little Oh (o)
The function f(n) = o(g(n)) iff for any positive constant c>0, there
exists a constant n0>0 such that 0 ≤ f(n) < c g(n) for all n ≥ n0
It is asymptotically loose upper bound
2)
T(n) = n2 + 2T(n/2)
= n2 + 2[(n/2) 2+2T(n/22)] = n2+n2/2 + 22 T(n/22)
= n + n /2 + 2 [(n/2 ) +2T(n/23)]
2 2 2 2 2
= n2+n2/2 + n2/22 +23 T(n/23)
……………………
= n2[1+ (1/2) + (1/2)2 + …. + (1/2)k-12 ] + 2k T(n/2k) kth term
k k
Assume n/2 = 1 2 = n k = log2(n)
T(n) = n2 [(1-(1/2)k) / (1-(1/2))] + n T(1)
= 2 n2 [ 1-(1/2k)] + n = 2 n2 [ 1-(1/2log n)] + n
= 2 n2 [ 1-(1/nlog 2)] + n = 2 n2 [ 1-(1/n)] + n
= 2 n2 - 2n+ n
= O(n2)
12.
a) Perform complexity analysis for the following code segments
1) For i=1 to n do
For j = 1 to n do
A=B * C
End for
End for
2) Function F(n)
{
If(n==0)
return(1)
Else
return (n*F(n-1))
}
Ans:
1)
Most frequently executing statement is A=B*C. It will execute n2 times.
So the time complexity = O(n2)
2)
Step/Executio Frequency Total Frequency
n Count Count
n==0 n>0 n≤0 n>0
Function F(n) 0 0 0 0 0
{ 0 0 0 0 0
If(n==0) 1 1 1 1 1
return(1) 1 1 0 1 0
Else 0 0 0 0 0
return (n*F(n-1)) 1 + T(n-1) 0 1 0 1 + T(n-1)
} 0 0 0 0 0
2 2 + T(n-1)
Time Complexity = T(n) = 2 if n<=0
2 + T(n-1) Otherwise
T(n) = 2 + T(n-1)
=2 + 2 + T(n-2)
=2 + 2 + 2+ T(n-3)
=2x3 + T(n-3)
. . .
=2xn +T(n-n)
=2n + 2
b) Discuss the concept of best case, worst case and average case complexity of an
algorithm with linear search algorithm.
Ans:
In certain case we cannot find the exact value of frequency count. In this case we
have 3 types of frequency counts
Best Case : It is the minimum number of steps that can be executed for a given
parameter
Worst Case: It is the maximum number of steps that can be executed for a
given parameter
Average Case: It is the average number of steps that can be executed for a
given parameter
Example: Linear Search
Best Case: Search data will be in the first location of the array.
Worst Case: Search data does not exist in the array
Average Case: Search data is in the middle of the array.
Best Case Worst Case Average Case
S/E F TFC S/E FC TFC S/E FC TFC
C
Algorithm Search(a,n,x) 0 0 0 0 0 0 0 0 0
{ 0 0 0 0 0 0 0 0 0
for i:=1 to n do 1 1 1 1 n+1 n+1 1 n/2 n/2
if a[i] ==x then 1 1 1 1 n n 1 n/2 n/2
return i; 1 1 1 1 0 0 1 1 1
return -1; 1 0 0 1 1 1 1 0 0
} 0 0 0 0 0 0 0 0 0
3 2n + n+1
2
Best Case Complexity = 3 = Ω(1)
Worst Case Complexity = 2n + 2 = O(n)
Average Case Complexity = n+1 = Ɵ(n)
13.
a) What are the operations supported by Disjoint Data Structure? Explain the working of
Disjoint Set Data Structure for computing Connected Components of an undirected
graph given in the following Figure.
Ans:
o Disjoint Set operations
Make set
Union
Find
o Find Operation
Determine which subset a particular element is in.
This will return the representative(root) of the set that the element belongs.
This can be used for determining if two elements are in the same subset.
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)
3. while nparent != NULL do
1.1 n = nparent
4. 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)
4. X =Find(a)
5. Y = Find(b)
6. If X != Y then
1. Yparent = X
Worst case Time Complexity = O(d), where d is the depth of the tree
Consider all edges one at a time. Initially consider the edge (a, b). Using FIND
operation we came to know that a and b are belongs to different sets. So perform
UNION operation. The resultant sets are
Then consider the edge (a, c). Using FIND operation we came to know that a and c
are belongs to different sets. So perform UNION operation. The resultant sets are
Then consider the edge (b, c). Using FIND operation we came to know that b and c
are belongs to the same set. So do nothing.
Then consider the edge (b, d). Using FIND operation we came to know that b and d
are belongs to different sets. So perform UNION operation. The resultant sets are
Then consider the edge (e, f). Using FIND operation we came to know that e and f
are belongs to different sets. So perform UNION operation. The resultant sets are
Then consider the edge (e, g). Using FIND operation we came to know that e and g
are belongs to different sets. So perform UNION operation. The resultant sets are
Then consider the edge (h, i). Using FIND operation we came to know that h and i
are belongs to different sets. So perform UNION operation. The resultant sets are
All edges are completed. From the above sets we can conclude that there are 4
connected components.
{a,b,c,d}
{e,f,g}
{h,i}
{j}
b) Illustrate the advantage of AVL trees with a suitable example. Discuss the various
rotations required to balance the height of AVL tree during insertion and deletion
Ans:
AVL Tree can be defined as height balanced binary search tree in which each
node is associated with a balance factor.
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.
Why AVL Tree?
Most of the Binary Search Tree(BST) operations (eg: search, insertion,
deletion etc) take O(h) time where h is the height of the BST.
The minimum height of the BST is log n
The height of an AVL tree is always O(log n) where n is the number of nodes
in the tree.
So the time complexity of all AVL tree operations are O(log n)
An AVL tree becomes imbalanced due to some insertion or deletion operations
We use rotation operation to make the tree balanced.
There are 4 types of rotations
14.
a) Give BFS algorithm for graph traversal and perform its complexity analysis
Ans:
Algorithm BFS(G, u)
1. Set all nodes are unvisited
2. Mark the starting vertex u as visited and put it into an empty Queue Q
3. While Q is not empty
3.1 Dequeue v from Q
3.2 While v has an unvisited neighbor w
3.2.1 Mark w as visited
3.2.2 Enqueue w into Q
4. If there is any unvisited node x
4.1 Visit x and Insert it into Q. Goto step 3
Complexity
If the graph is represented as an adjacency list
Each vertex is enqueued and dequeued atmost once. Each queue
operation take O(1) time. So the time devoted to the queue operation is
O(V).
The adjacency list of each vertex is scanned only when the vertex is
dequeued. Each adjacency list is scanned atmost once. Sum of the
lengths of all adjacency list is |E|. Total time spend in scanning
adjacency list is O(E).
Time complexity of BFS = O(V) + O(E) = O(V+ E).
In a dense graph:
E=O(V2)
Time complexity= O(V) + O(V2) = O(V2)
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 BFS = O(V2)
X=78,Y=50,Z=54
PERFORM RIGHT LEFT ROTATION
LL(50)→
RR(78)→
2)
Delete 15→
X=51,y=10,z=60
PERFORM RIGHT LEFT ROTATION
RR(70)→
LL(60)→
15.
a) Write a divide and conquer algorithm for 2-way merge sort and perform its
complexity analysis.
Ans:
Given a sequence of n elements a[l],…...a[n]. Split this array into two sets
a[l],,.a.[n/2] and a[(n/2)+1],…a[n]. Each set is individually sorted, and the resulting
sorted sequences are merged to produce a single sorted sequence of n element.
Complexity
T(n) = a if n=1
2 T(n/2) + cn Otherwise
b) Give Kruskal‟s algorithm for minimum cost spanning tree computation. Apply the
algorithm to find the minimum cost spanning tree for the following graph
Ans:
Algorithm Kruskals(E, cost, n, t)
{
Construct a heap out of edge costs using Heapify();
for i=1 to n do
parent[i] = -1;
i=0;
mincost = 0.0;
while (i < n-1) and (heap not empty) do
{
Delete a minimum cost edge (u, v) from the heap and reheapify using Adjust();
j = Find(u);
k = Find(v);
if j ≠ k then
{
i = i+1;
t[i, 1] = u; t[i, 2] = v;
mincost = mincost + cost[u, v];
Union(j, k);
}
}
if i ≠ n-1 then
Write (“No Spanning Tree”);
else
return mincost;
}
This is the minimum cost spanning tree and its cost = 60
16.
a) Write Dijkstra‟s algorithm for single source shortest path. Perform its complexity
analysis.
Ans:
Algorithm Dijkstra(G,W, S)
1. For each vertex v in G
1.1 distance[v] = infinity
1.2 previous[v] = Null
2. distance[S] = 0
3. Q = set of vertices of graph G
4. While Q is not empty
4.1 u = vertex in Q with minimum distance
4.2 remove u from Q
4.3 for each neighbor v of u which is still in Q
4.3.1 alt = distance[u] + W(u,v)
4.3.2 if alt < distance[v]
4.3.2.1 distance[v] = alt
4.3.2.2 previous[v] = u
5. Return distance[], previous[]
Complexity
The complexity mainly depends on the implementation of Q
The simplest version of Dijkstra's algorithm stores the vertex set Q as an
ordinary linked list or array, and extract-minimum is simply a linear search
through all vertices in Q. In this case, the running time is O(E + V2) = O(V2)
Graph represented using adjacency list can be reduced to O(E log V) with the
help of binary heap.
b) Write the control abstraction for Greedy design technique. Give a greedy algorithm
for fractional knapsack problem.
Ans:
o Control Abstraction
Greedy(a, n) //a[1..n] contains n inputs
{
solution = Φ;
for i=1 to n do
{
x = Select(a);
if Feasible(solution, x) then
solution = Union(solution, x);
}
return solution;
}
Select() selects an input from the array a[] and remove it. The selected input
value is assigned to x.
Feasible() is a Boolean valued function that determines whether x can be
included into the solution subset.
Union() combines x with the solution and updates the objective function.
Fractional Knapsack Problem
o We are given with n objects and a knapsack(or bag) of capacity m. The object i
has weight Wi and profit Pi. If a fraction Xi is placed into the knapsack, then a
profit PiXi is obtained. The objective is to obtain an optimal solution of the
knapsack that maximizes the total profit earned.
o The total weight of all the chosen objects should not be more than m.
o Fractional knapsack problem can be stated as
Maximize 𝑛𝑖=1 𝑃𝑖𝑋𝑖 1
𝑛
Subject to 𝑖=1 𝑊𝑖𝑋𝑖 ≤m 2
0 ≤ Xi ≤ 1 and 1 ≤ i ≤ n 3
The profits and weights are positive numbers.
o A feasible solution is one that satisfies equation 2 and 3.
o An optimal solution is a feasible solution that satisfies equation 1.
o In greedy strategy we are arranging the objects in the descending order of
profit/weight.
o Algorithm
Algorithm GreedyKnapsack(m, n)
//p[1:n] is the profits and w[1:n] is the weights of n objects such that p[i]/w[i] ≥
p[i+1]/w[i+1].
{
for i= 1 to n do
x[i] = 0.0; // x[1:n] is the solution vector
U = m; // m is the knapsack capacity
for i=1 to n do
{ if w[i] > U then
break;
x[i] = 1.0
U = U – w[i];
}
If i ≤ n then
x[i] = U / w[i];
}
o Time Complexity
The for loop will execute maximum n times. So the time complexity = O(n)
17.
a) Write Floyd Warshall algorithm for all pair shortest path problem and perform its
complexity analysis
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
Floyd Warshall Algorithm consists of three loops over all the nodes. Each
loop has constant complexities.
Hence, the time complexity of Floyd Warshall algorithm = O(n3), where n is
the number of nodes in the given graph.
b) Define Travelling Salesman Problem. Solve the following instance of TSP using
branch and bound technique. The cost matrix is given below:
Ans:
18.
a) Consider the following four matrices and perform chain matrix multiplication using
the dynamic programming approach. Finally give the optimal cost of multiplication
and optimal parenthesization
Ans:
b) Define n-queens problem. Draw the state space tree for 4-queens problem using
backtracking method
Ans:
n-Queens Problem
n queens are to be placed on a n x n chessboard so that no two attack. That is, no
two queens are on the same row, column, or diagonal.
State Space Tree of 4 Queens Problem
19.
a) Explain the benefits of randomized algorithm over deterministic algorithm. Discuss
briefly the major categories of randomized algorithms. Give example for each
category.
Ans:
Randomized Algorithm
o Deterministic Algorithm: The output as well as the running time are functions
of the input only.
o Randomized Algorithm: The output or the running time are functions of the
input and random bits chosen
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)
b) Define bin packing problem. Discuss the first fit strategy for solving it. State the
approximation ratio of the algorithm
Ans:
Bin packing problem: Given n items of different weights and bins each of capacity
c, assign each item to a bin such that number of total used bins is minimized. It may
be assumed that all items have weights smaller than bin capacity
20.
a) Give a randomized version of quicksort algorithm and perform its expected running
time analysis.
Ans:
Example: