100% found this document useful (1 vote)
259 views28 pages

BCS-042 Solved Assignment 042 Solved Assignment 042 Solved Assignment

Here is an optimal solution for the fractional knapsack problem with n=5 items and a knapsack capacity of M: Let the items be: Item 1: Weight = 2, Value = 4 Item 2: Weight = 3, Value = 6 Item 3: Weight = 4, Value = 8 Item 4: Weight = 5, Value = 10 Item 5: Weight = 6, Value = 12 Knapsack capacity: M = 11 Sort the items by value/weight ratio in descending order: Item 5: Value/Weight = 12/6 = 2 Item 4: Value/Weight = 10/5 = 2 Item 3: Value/Weight =

Uploaded by

Naksh Mayank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
259 views28 pages

BCS-042 Solved Assignment 042 Solved Assignment 042 Solved Assignment

Here is an optimal solution for the fractional knapsack problem with n=5 items and a knapsack capacity of M: Let the items be: Item 1: Weight = 2, Value = 4 Item 2: Weight = 3, Value = 6 Item 3: Weight = 4, Value = 8 Item 4: Weight = 5, Value = 10 Item 5: Weight = 6, Value = 12 Knapsack capacity: M = 11 Sort the items by value/weight ratio in descending order: Item 5: Value/Weight = 12/6 = 2 Item 4: Value/Weight = 10/5 = 2 Item 3: Value/Weight =

Uploaded by

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

Learning Science

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.

Notations of used to express asymptotic analysis:


There are 3 asymptotic notations used to express the time complexity of an algorithm O, Ω and Θ notations.

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.

O(g(n)) = { f(n): there exist positive constants c and


n0 such that 0 ≤ f(n) ≤ c*g(n) for
all n ≥ n0}

Ω -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.

For a given function g(n), we denote by Ω(g(n)) the set of functions.

Ω (g(n)) = {f(n): there exist positive constants c and


n0 such that 0 ≤ c*g(n) ≤ f(n) for
all n ≥ n0}.

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.

3n3 + 6n2 + 6000 = Θ(n3)

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.

For a given function g(n), we denote Θ(g(n)) is following set of functions.

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:

Time Complexity Analysis of Bubble Sort Algorithm:


In this un-optimised version the run time complexity is [Θ(N^2)]. This applies to all the cases including the worst,
best and average cases because even if the array is already sorted the algorithm doesn't check that at any point and
runs through all iterations. Although the number of swaps would differ in each case.

Worst Case Time Complexity

Θ(N^2) is the Worst Case Time Complexity of Bubble Sort.

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.

Best Case Time Complexity

Θ(N) is the Best Case Time Complexity of Bubble Sort.

This case occurs when the given array is already sorted.

Average Case Time Complexity

Θ(N^2) is the Average Case Time Complexity of Bubble Sort.

Time Complexity Analysis of Selection Sort

The time complexity of Selection Sort is O(N2).

Worst Case Time Complexity of Selection Sort

O(N^2) is the Worst Case Time Complexity of Selection Sort.

5
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Best Case Time Complexity of Selection Sort

O(N^2) is the Best Case Time Complexity of Selection Sort.

Average Case Time Complexity of Selection Sort

O(N^2) is the Average Case Time Complexity of Selection Sort.

Q2: Find out the complexity of the following algorithm:

function min (X1, X2…………Xn)


min = X1;
for i = 2 to n
if (min > Xi) then
min = Xi;

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

Total time unit is [1 + (n+1) + n + 1] i.e., 2n + 3


Therefore, time complexity is O(n)

Space Complexity is O(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:

Time Complexity Analysis


Binary Search time complexity analysis is done below-
• In each iteration or in each recursive call, the search gets reduced to half of the array.
• So for n elements in the array, there are log2n iterations or recursive calls.
Thus, we have-

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:

Fractional Knapsack Algorithm:

Algorithm: Greedy-Fractional-Knapsack (w[1..n], p[1..n], W)


for i = 1 to n
do x[i] = 0
weight = 0
for i = 1 to n
if weight + w[i] ≤ W then
x[i] = 1

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 .

The solution of the problem is given below:

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

1. Assign value of polynomial p= coefficient of nth term in the polynomial


2. set i= n-1
3. compute p = p * x + a[i];
4. i=i-1
5. if i is greater than or equal to 0, then go to step 4.

14
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
6. final polynomial value at x is p.

Step – 2: Algorithm to evaluate polynomial at a given point x using Horner‟s rule:

Input: An array a[0..n] of coefficient of a polynomial of degree n and a point x

Output: The value of polynomial at given point x

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:

Polynomial of degree n using Horner’s rule is evaluated as below:

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:

The given polynomial

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 )

∴ Remainder when the given polynomial is divided by ( x − 2 )


P ( 2 ) = 625

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;

printf("\nEnter degree of the polynomial X :: ");


scanf("%d",&n);
printf("\nEnter coefficient's of the polynomial X :: \n");
for(i=n;i>=0;i--)
{
printf("\nEnter Coefficient of [ X^%d ] :: ",i);
scanf("%f",&a[i]);
}

printf("\nEnter the value of X :: ");


scanf("%f",&x);

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

where, T(n) has the following asymptotic bounds:

( ) ( )
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:

Best Case (Input array is not sorted)

The best case behaviour of Quicksort algorithm occurs when the partitioning procedure produces two regions of
size ≈ (n/2) elements.

In this case, Recurrence can be written as:

n
T ( n ) = 2T   + θ ( n )
2

Method : Using Master Method:


We have a = 2; b = 2, f ( n ) = n and nlobb a = nlob2 2 = n
∴ F ( n ) = n = O ( nlob2 2 ) → Case 2 of master method.
T ( n ) = Θ ( n log n )

27
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in
Thank You

To get the rest assignments, you can visits at


https://learningscience.co.in/shop

Or

You can WhatsApp me at 7980608289

28
BCS-042 Solved Assignment 2021-22 https://learningscience.co.in

You might also like