You are on page 1of 7

KINGDOM OF SAUDI ARABIA | JAZAN UNIVERSITY

COLLEGE OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY


ASSIGNMENT- I (2021-22)
Academic Year 2021-22 Semester Spring
Course with code Algorithm & Data Structures-II Section 2216
Type of
Problem Marks 10
Assignment
Date of
13-02-2022 Deadline 24-02-2022
Announcement

ASSIGNMENT PROBLEM STATEMENT

Q1. Order the following functions by asymptotic growth rate. [2 Marks]


4nlogn+2n 210 2logn
3n+100logn 4n 2n
n2 +10n n3 nlogn

210 2logn 3n+100logn 4n nlogn 4nlogn+2n n2+10n n3 n2

210 is constant time 2logn is linear O(n) by the definition of log


3n+100logn is O(n)

4n is also O(n) and larger than 3n+100logn because the 4n term is larger than the 3n term

nlogn is O(nlogn)

4nlogn+2n is also O(nlogn) because the nlogn term dominates the 2n term

210+10n is O(n2)

N3 is O(n3) n2 is O(n2) – exponential.


. Q2. Give a big-Oh characterization, in terms of n, of the running time of
the method shown in Code Fragment [1 Mark]

/∗∗ Returns the sum of the integers in given array. ∗/

public static int example1(int[ ] arr) {


int n = arr.length, total = 0;
for (int j=0; j < n; j++)
total += arr[j];
return total;
}
Initializing variables n and total uses O(1) time.

- Initializing the array a uses O(n) time.

- There is a single for loop, which is controlled by counter j.

The maintenance of that loop contributes a total of O(n) time. •


The body of the loop is executed n times, for j = 0,...,n−1. Thus,
statements total += arr[j]; is executed n times each.

Since each of these statements uses O(1) time per iteration,


their overall contribution is O(n) time.

The eventual return of total uses O(1) time.

-Total =a constant c×n=cn=O(n)


Q3. Give a big-Oh characterization, in terms of n, of the running time of
the method shown in Code Fragment [1 Mark]

/∗∗ Returns the sum of the integers with even index in given array. ∗/
public static int example2(int[ ] arr) {
int n = arr.length, total = 0;
for (int j=0; j < n; j += 2)
total += arr[j];
return total;
}
Initializing variables n and total uses O(1) time.
-Initializing the array a uses O(n) time.

- There is a single for loop, which is controlled by counter j.


The maintenance of that loop contributes a total of O(n) time. •

The body of the loop is executed n times, for j = 0,...,n−1. Thus,

statements total += arr[j]; is executed n times each.


Since each of these statements uses O(1) time per iteration,

their overall contribution is O(n) time.


- The eventual return of total with even index uses O(1) time.

-Total =a constant c×n=cn=O(n)

. Q4. Give a big-Oh characterization, in terms of n, of the running time of


the method shown in Code Fragment [1
Mark]

/∗∗ Returns the sum of the prefix sums of given array. ∗/

public static int example3(int[ ] arr) {


int n = arr.length, total = 0;
for (int j=0; j < n; j++)
for (int k=0; k <= j; k++)
total += arr[j];
return total; }
Total =c×n×n=cn2=O(n2).
total+=arr[j] is executed j times inside k loop.

j vary from 0 to n so the statement will execute for 0+1+2+...n-1 times

total operations = n*(n-1)/2 = (n^2)

(using sum of aritmetic progression and big Oh definition, c=10, g(n) = n^2, n0=1)

g(n) = n^2

c1 = 1, c2=2, n0=1

T(n) =Theta(n^2)

.
Q5. Give a big-Oh characterization, in terms of n, of the running time of
the method shown in Code Fragment [1 Mark]

/∗∗ Returns the sum of the prefix sums of given array. ∗/

public static int example4(int[ ] arr) {


int n = arr.length, prefix = 0, total = 0;
for(intj=0;j<n;j++){ //loopfrom0ton-1
prefix += arr[j];
total += prefix;
}
return total;
}
prefix+=arr[j] execute n times

T(n) = O(n) = Theta(n)

g(n) = n, n0=1 for both cases

for O, c=10, Theta->c1=0.01, c2=100


. Q6. Give a big-Oh characterization, in terms of n, of the running time of
the method shown in Code Fragment [2
Marks]

/∗∗ Returns the number of times second array stores sum of prefix sums
from first. ∗/

public static int example5(int[ ] first, int[ ] second) { /


int n = first.length, count = 0;
for(inti=0;i<n;i++){ //loopfrom0 to n-1
int total = 0;
for (int j=0; j < n; j++)
for (int k=0; k <= j; k++)
total += first[k];
if (second[i] == total) count++;
}
return count;
}
inner two loop require O(n^2) time, from first part

outer loop runs n times.

T(n) = n^3 = O(n^3) = Theta(n^3)

g(n) = n^3, n0=1 for both cases

for O, c=10, Theta->c1=0.01, c2=100


Q7: Find the Big-Ω for the following f(n).Also find the value of c and
n0 with proper justification. [2 Marks]
3 2 3
f(n)=9n + 70n +2n logn + 2n+80

justification: 9n3+ 70n2 +2n3logn + 2n+80<=(9+70+2+2+80) n3 = cn3


for c = 163 , when n≥n0 = 1 .

Important Instructions:

 Submit the soft copy of your assignment on Blackboard.


 Make the Assignment Name like this (your ID.Assignment_1)
 Take proper care during submission of Assignment; submit it in proper
group and proper document. After the submission kindly check the
document again. If you have submitted the wrong document or in a
wrong group, immediately contact me to me (Course Teacher).(via
WhatsApp)
 Submit the Assignment soft copy before 23:59hrs of the due date.
 If you submit your assignment late. Each day, 10% penalty will incur of
the total marks scored by the students.

Mr. Khaja Raoufuddin


Name of the Course Teacher Signature
Ahmed

You might also like