BCS-042 Solved Assignment 042 Solved Assignment 042 Solved Assignment
BCS-042 Solved Assignment 042 Solved Assignment 042 Solved Assignment
BCS-042
042 Solved Assignment
Session: 2021-2022
2022 (July – January)
1
BCS-042
042 Solved Assignment 2021
2021-22 https://learningscience.co.in
Q1: (i) Define asymptotic analysis and explain the three notations which are primarily used for asymptotic
analysis with the help of examples.
Solution:
Asymptotic Analysis:
Asymptotic analysis of algorithms is a means of comparing relative performance.
O-notation: The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. For
example, consider the case of Insertion Sort. It takes linear time in best case and quadratic time in worst case. We
can safely say that the time complexity of Insertion sort is O(n2). Note that O(n2) also covers linear time.
If we use Θ notation to represent time complexity of Insertion sort, we have to use two statements for best and
worst cases:
1. The worst case time complexity of Insertion Sort is Θ(n2).
2. The best case time complexity of Insertion Sort is Θ(n).
The Big O notation is useful when we only have upper bound on time complexity of an algorithm. Many times we
easily find an upper bound by simply looking at the algorithm.
Ω -notation: Just as Big O notation provides an asymptotic upper bound on a function, Ω notation provides an
asymptotic lower bound.
2
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Ω Notation can be useful when we have lower bound on time complexity of an algorithm. As discussed in the
previous post, the best case performance of an algorithm is generally not useful, the Omega notation is the least
used notation among all three.
Let us consider the same Insertion sort example here. The time complexity of Insertion Sort can be written as Ω(n),
but it is not a very useful information about insertion sort, as we are generally interested in worst case and
sometimes in average case.
Θ-notation: The theta notation bounds a function from above and below, so it defines exact asymptotic behavior.
A simple way to get Theta notation of an expression is to drop low order terms and ignore leading constants. For
example, consider the following expression.
Dropping lower order terms is always fine because there will always be a number(n) after which Θ(n3) has higher
values than Θ(n2) irrespective of the constants involved.
3
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Θ(g(n)) = {f(n): there exist positive constants c1, c2 and n0 such
that 0 ≤ c1*g(n) ≤ f(n) ≤ c2*g(n) for all n ≥ n0}
The above definition means, if f(n) is theta of g(n), then the value f(n) is always between c1*g(n) and c2*g(n) for
large values of n (n ≥ n0). The definition of theta also requires that f(n) must be non-negative for values of n greater
than n0.
(ii) Write all the three cases of asymptotic analysis of the following sorting algorithms.
• Bubble Sort
• Selection Sort
Solution:
4
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
This is the case when the array is reversely sort i.e. in descending order but we require ascending order or
ascending order when descending order is needed.
5
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Best Case Time Complexity of Selection Sort
Solution:
6
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Algorithm Times unit
min = X1; =1
for i = 2 to n =n+2
if (min > Xi) then =n
min = Xi; =1
Q3: Write a binary search algorithm (non recursive version) and show the complexity analysis of the algorithm
step by step.
Solution:
C program to implement binary search using iterative call/non-recursive call
#include <stdio.h>
int iterativeBinarySearch(int array[], int start_index, int end_index, int element){
while (start_index <= end_index){
int middle = start_index + (end_index- start_index )/2;
if (array[middle] == element)
7
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
return middle;
if (array[middle] < element)
start_index = middle + 1;
else
end_index = middle - 1;
}
return -1;
}
int main(void){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 16;
int found_index = iterativeBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
}
else {
printf("Element found at index : %d",found_index);
}
return 0;
}
8
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Output:
9
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Time Complexity of Binary Search Algorithm is O(log2n).
Here, n is the number of elements in the sorted linear array.
This time complexity of binary search remains unchanged irrespective of the element position even if it is not
present in the array.
Q4: (i) Write an algorithm for the fractional knapsack using greedy approach and perform complexity analysis.
Solution:
10
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
weight = weight + w[i]
else
x[i] = (W - weight) / w[i]
weight = W
break
return x
Analysis:
If the provided items are already sorted into a decreasing order of piwi , then the while loop takes a time in O(n);
Therefore, the total time including the sort is in O(n logn).
(ii) Find an optimal solution for the Knapsack problem for n = 5 (the number of objects) and M (Knapsack capacity)
= 10. Profit and weight of each object is as follows:
(P1, P2, P3, P4, P5) = (10, 25, 30, 15, 35)
(W1, W2, W3, W4, W5) = (5, 7, 4, 8, 3)
Solution:
11
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Given,
Number of Objects, n = 5
Capacity of Knapsack, m = 10
(P1, P2, P3, P4, P5) = (10, 25, 30, 15, 35)
(W1, W2, W3, W4, W5) = (5, 7, 4, 8, 3)
To solve this problem, Greedy method may apply any one of the following strategies:
1. From the remaining objects, select the object with maximum profit that fit into the knapsack.
2. From the remaining objects, select the object that has minimum weight and also fits into knapsack.
P
3. From the remaining objects, select the object with maximum i that fits into the knapsack.
Wi
As per the question’s requirement, I am going to solve this problem by the 3rd approache which are mentioned
above.
Pi
Approach -3: From the remaining objects, select the object with maximum that fits into the knapsack.
Wi
P
In this approach, we select those object first which has maximum value of i , that is we select those object first
Wi
which has maximum profit per unit weight .
12
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Knapsack Problem (Fractional)
(n = 5 ) O b je c ts : O i 1 2 3 4 5
P r o fits : Pi 10 25 30 15 35
W e ig h t s : W i 5 7 4 8 3
Pi
= 2 3 .5 7 7 .5 1 .8 8 1 1 .6 7
Wi
3
Xi = 0 1 0 1
7
W h e re 0 ≤ X i ≤ 1 ( T o g e t t h e m a x i m u m p ro fit )
C o n s ta ri n t: T o ta l c a p a c ity o f K n a p s a c k ,
5
∑ X iW i ≤ m ( = 1 0 )
i =1
O b je c tiv e : M a x im iz e d to ta l p ro fit,
5
∑
i =1
X i Pi
T o ta l w e ig h t,
5
∑
i =1
X iW i = X 1W 1 + X 2W 2 + X 3W 3 + X 4W 4 + X 5W 5
3
= ( 0 × 5 ) + × 7 + (1 × 4 ) + ( 0 × 8 ) + (1 × 3 )
7
=3+ 4+3
= 1 0 u n it
13
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Maximum total Profit,
5
∑X P = X P + X
i =1
i i 1 1 P + X 3 P3 + X 4 P4 + X 5 P5
2 2
3
= ( 0 × 10 ) + × 25 + (1 × 30 ) + ( 0 × 15 ) + (1 × 35 )
7
530
=
7
= Rs. 75.71 ( approx ) ( Answer )
Q5: (i) Write a pseudo code of evaluating polynomial expression using Horner’s rule and perform complexity
analysis (step by step)
Solution:
Step – 1: Pseudo code for polynomial evaluation using Horner method, Horner(a,n,x)
//In this a is an array of n elements which are coefficient of polynomial of degree n
14
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
6. final polynomial value at x is p.
Evaluate_Horner(a,n,x)
{
p = a[n];
for (i = n-1; i 0;i--)
p = p * x + a[i];
return p;
}
15
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
For Example: p(x)=x2+3x+2 using Horner’s rule can be simplified as follows
At x = 2,
p(x) = (x+3)x+2
p(2)=(2+3).2+2
= (5).2+2
=10+2
=12
Complexity Analysis:
Initial Assignment, p = an
after iteration 1, p = x an + an–1
after iteration 2, p = x(x an + an–1) + an–2
= x2 an + x an–1 + an–2
Every subsequent iteration uses the result of previous iteration i.e next iteration multiplies the previous value of p
then adds the next coefficient, i.e.
p = x(x2 an + x an–1 + an–2) + an–2
= x3 an + x2an–1 + x an–2 + an–3 etc.
16
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Thus, after n iterations, p = xn an + xn–1 an–1 + … + a0, which is the required correct value.
In above function, First step is one initial assignment that takes constant time i.e O(1).
For loop in the algorithm runs for n iterations, where each iteration cost O(1) as it includes one multiplication, one
addition and one assignment which takes constant time.
Hence total time complexity of the algorithm will be O(n) for a polynomial of degree n.
(ii) Apply the above algorithm to evaluation the following polynomial expression.
P ( x ) = 6 x6 + 5 x5 + 3x 4 + 2 x 2 + 8 x + 9 .
Solution:
P ( x ) = 6 x6 + 5 x5 + 3x 4 + 2 x 2 + 8 x + 9
Can be arranged as –
17
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
P ( x ) = 6 x 6 + 5 x5 + 3x 4 + 2 x 2 + 8 x + 9
= 6 x 6 + 5 x5 + 3 x 4 + 0 x 3 + 2 x 2 + 8 x + 9
= x ( 6 x 5 + 5 x 4 + 3x 3 + 0 x 2 + 2 x + 8 ) + 9
(
= x x ( 6 x 4 + 5 x3 + 3x 2 + 0 x + 2 ) + 8 + 9)
(( ) )
= x x x ( 6 x3 + 5 x 2 + 3x + 0 ) + 2 + 8 + 9
= x ( x ( x ( x ( 6 x + 5 x + 3) + 0 ) + 2 ) + 8 ) + 9
2
= x ( x ( x ( x ( x ( 6 x + 5 ) + 3) + 0 ) + 2 ) + 8 ) + 9
At x = c,
Using Horner ' s method , we have
((( ) )
Remainder P ( c ) = c c c c ( c ( 6c + 5 ) + 3) + 0 + 2 + 8 + 9 )
For an example, taking x = 2
Calculatio of remainder by Horner's method:
Co-efficient: 6 5 3 0 2 8 9
At x = 2 : 6 17 ( = 6 × 2 + 5 ) 37 ( = 17 × 2 + 3) 74 ( = 37 × 2 + 0 ) 150 ( = 74 × 2 + 2 ) 308 ( = 150 × 2 + 8 ) 625 ( = 308 × 2 + 9 )
18
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
/* C Program to Evaluate Polynomial using Horner’s method */
#include <stdio.h>
int main()
{
float a[100],sum=0,x;
int n,i;
for(i=n;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
19
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
printf("\nValue of the polynomial is = [ %f ]\n",sum);
return 0;
}
20
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Output:
21
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Q6: Multiply 3426 × 2569 using Divide and Conquer method (Apply Karatsuba Method).
Solution:
22
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
23
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Q7: Discuss all the three cases of master method to solve the recurrence
n
T ( n ) = aT + f ( n ) where a ≥ 1, b > 1.
b
Solution:
If a ≥ 1 and b > 1 are constants and f(n) is an asymptotically positive function, then the time complexity of a
recursive relation is given by
n
T ( n ) = aT + f ( n ) .
b
( ) ( )
1. If f ( n ) = O n logb a −∈ , then T ( n ) = θ n logb a .
2. If f ( n ) = O ( n ) , then T ( n ) = θ ( n × log n ).
logb a log b a
3. If f ( n ) = O ( n ) , then T ( n ) = θ ( f ( n ) ).
logb a +∈
∈ > 0 is a constant.
24
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Each of the above conditions can be interpreted as:
1. If the cost of solving the sub-problems at each level increases by a certain factor, the value of f(n) will
become polynomially smaller than nlogb a . Thus, the time complexity is oppressed by the cost of the last level
ie. nlogb a
2. If the cost of solving the sub-problem at each level is nearly equal, then the value of f(n) will be nlogb a . Thus,
the time complexity will be f(n) times the total number of levels ie. n b × log n .
log a
3. If the cost of solving the subproblems at each level decreases by a certain factor, the value of f(n) will
become polynomially larger than nlogb a . Thus, the time complexity is oppressed by the cost of f(n).
Q8: (i) Illustrate the operation of partition procedure used in Quick sort algorithm for the following array
elements:
45 35 10 25 18 15 22 11
Solution:
25
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
26
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
(ii) Write a recurrence relation of Quick Sort Algorithm.
Solution:
The best case behaviour of Quicksort algorithm occurs when the partitioning procedure produces two regions of
size ≈ (n/2) elements.
n
T ( n ) = 2T + θ ( n )
2
27
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Thank You
Or
28
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in