0% found this document useful (0 votes)
3 views8 pages

CS2002 - Data Structures and Algorithms: Assignment Number 1

The document outlines the assignment details for CS2002 - Data Structures and Algorithms, including instructions, due date, and marking scheme. It consists of various questions focusing on algorithm efficiency, time complexity analysis, and coding tasks in C++. Each question requires justifications and calculations to support the answers, with specific emphasis on big-O notation for time complexities.

Uploaded by

f236087
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
0% found this document useful (0 votes)
3 views8 pages

CS2002 - Data Structures and Algorithms: Assignment Number 1

The document outlines the assignment details for CS2002 - Data Structures and Algorithms, including instructions, due date, and marking scheme. It consists of various questions focusing on algorithm efficiency, time complexity analysis, and coding tasks in C++. Each question requires justifications and calculations to support the answers, with specific emphasis on big-O notation for time complexities.

Uploaded by

f236087
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/ 8

CS2002 – DSA Assignment Number 1

CS2002 – Data Structures and Algorithms


Assignment Number 1
Spring 2025

Maximum Marks: 100 Due Date: 13 February 2025

Instructions:
• Partially or fully copied assignments will be marked as zero.
• Only handwritten solution on A4 page will be accepted.
• Use the title page provided with this assignment.
• Late submissions are not allowed.
• Solve all the questions in the given order.
• Justify your answers with proper reasons/calculations. No marks will be awarded in
case of missing calculations/reasons.

Taxonomy
CLO No. CLO Statement Domain
Level
Compute the efficiency of algorithms and data
1 Cognitive 3
structures in terms of time and space complexity.

Question Number 1 (CLO 1) (5 + 5 = 10 marks)


Use the growth rate concept to arrange the following functions in ascending order. Also discuss
which functions grow at the same rate.
2
𝑁, √𝑁, 𝑁1.5 , 𝑁 2 , 𝑁 log 𝑁 , 𝑁 log log 𝑁 , 𝑁log 2 𝑁, 𝑁 log(𝑁 2 ), , 2𝑁 , 37, 𝑁 2 log 𝑁 , 𝑁 3
𝑁

Question Number 2 (CLO 1) (5 + 5 = 10 marks)


Compute the best- and worst-case time complexity for the following code fragments.

void fun(int n)
void fun(int a, int b) {
{ int i = 1;
// a and b are +ive integers while (i < n)
while (a != b) {
{ int j = n;
if (a > b) while (j > 0)
a = a - b; {
else j = j / 2;
b = b - a; }
} i = i * 2;
} }
}

National University of Computer and Emerging Sciences – FAST, CFD Campus 1


CS2002 – DSA Assignment Number 1

Time complexity = O(1) in best case and


O(max(a, b)) worst case. Best case time complexity is O(1), when n =
1, and worst case time complexity is
If the value of a and b are the same, then O(log2n).
while loop will not be executed. Hence, time In each iteration, i become twice
complexity will be O(1). But if a!=b, then the (T.C=O(logn)) and j become half
while loop will be executed. The number of (T.C=O(logn)). So, time complexity will
iterations will be equal to a/2 if a > b become O(log2n).
otherwise it will be b/2. Hence we can say We can convert this while loop into for loop.
that there are max(a/2, b/2) iterations. So for( int i = 1; i < n; i = i * 2)
we can say that time complexity is for( int j=n ; j > 0; j = j / 2).
O(max(a/2, b/2))⇒ O(max(a, b)), it is Time complexity of above for loop is also
considered as worst case because it takes O(log2n).
more time.

Question Number 3 (CLO 1) (5 + 5 = 10 marks)


Write a C++ program to determine if a positive integer, N, is prime and compute the worst-
case running time of your program in terms of N? Discuss the minimum possible complexity
for this program.

#include<iostream>
#include<math.h>
using namespace std;
void JudgePrime(int n)
{
if(n < 2)
cout<<"Not Prime"<<endl;
for(int i = 2; i<=sqrt(n); i++)
{
if(n % i == 0)
{
cout<<"Not Prime"<<endl;
exit(0);
}
}
cout<<"Prime"<<endl;
}
main()
{
int n;
cout<<"Enter a Number : ";
cin>>n;
JudgePrime(n);
}

The maximum number of iterations in the function are √𝑛 , hence the complexity is 𝑂(√𝑛).

National University of Computer and Emerging Sciences – FAST, CFD Campus 2


CS2002 – DSA Assignment Number 1

Question Number 4 (CLO 1) (5 + 5 = 10 marks)


Compute the time complexities of the following code fragments and support your answers with
proper reasons.

void fun(int n, int k)


int fun(int n)
{
{
for(int i = 1; i <= n; i++)
for(int i = 1; i <= n; i++)
{
{
int p = pow(i, k);
for(int j=1; j<n; j+=i)
for (int j = 1; j <= p; j++)
{
{
// Some O(1) task
// Some O(1) work
}
}
}
}
}
}

Time complexity of above function can be


written as 1k + 2k + 3k + … nk. For i = 1, the inner loop is executed n times.
k=1
Sum = 1 + 2 + 3 ... n For i = 2, the inner loop is executed
= n(n+1)/2 approximately n/2 times.
= n2/2 + n/2 For i = 3, the inner loop is executed
k=2 approximately n/3 times.
Sum = 12 + 22 + 32 + ... n2.
For i = 4, the inner loop is executed
= n(n+1)(2n+1)/6
approximately n/4 times.
= n3/3 + n2/2 + n/6
k=3 For i = n, the inner loop is executed
3 3 3 3
Sum = 1 + 2 + 3 + ... n . approximately n/n times.
= n2(n+1)2/4
So the total time complexity of the above
= n4/4 + n3/2 + n2/4
algorithm is (n + n/2 + n/3 + … + n/n)
In general, asymptotic value can be written as
(nk+1)/(k+1) + O(nk). Which becomes n * (1/1 + 1/2 + 1/3 + … + 1/n).

If n>=k then the time complexity will be It is a diverging harmonic series, and the sum of
considered in O((nk+1)/(k+1)) this series is given as (logn).

And if n<k, then the time complexity will be Hence the time complexity becomes O(nlogn).
considered as in the O(nk)

National University of Computer and Emerging Sciences – FAST, CFD Campus 3


CS2002 – DSA Assignment Number 1

Question Number 5 (CLO 1) (2 + 3 + 3 + 2 = 10 marks)


Suppose that we are inserting and deleting some elements from a 2D array and want to maintain
relative order. Recall the process of insertion or deletion and compute the time complexity in
big-oh notation for the following: Justify your answers with proper reason(s).
• Insertion at the end
• Insertion at the beginning
• Deleting a row with index (n – k), where k is an integer and k < n.
• Deleting the last row.

Insertion at the End


If we want to insert a row at the end of a 2D array, then there is no need to shift any row. We
simply insert the new row at the end. But the row we want to insert may have a maximum
of n elements. Hence the complexity is O(n).

Insertion at the Beginning


If we want to insert a row at the beginning and want to maintain the relative order, then we
need to shift all the row downward to create a space for new row and then we will insert the
new row.
We need to shift n rows and each row may have up to n elements. Then we will insert a row
with a maximum of n elements. Hence the complexity is (n2 + n) and in big-oh notation it
will be written as O(n2).

Deleting a Row with Index (n – k)


If we want to delete a row with index (n – k) then in the worst-case we may be deleting the
first row. So, we need to shift (n – 1) rows upwards and each row may have up to n elements.
Hence the complexity will be O(n2).

Deleting the Last Row


In order to delete the last row, we will only decrement the row count. There is no need to
shift any row. Hence, the complexity of deleting the last row is O(1).

National University of Computer and Emerging Sciences – FAST, CFD Campus 4


CS2002 – DSA Assignment Number 1

Question Number 6 (CLO 1) (10 marks)


Use the complexity analysis concepts to show that the time complexity of the given code is
𝑂(√𝑛).

void function(int n)
{
int i = 1, s = 1;
while (s <= n)
{
i++;
s += i;
cout<<“++”;
}
}

We can define the terms “s” according to relation si = si-1 + i. The value of ‘i’ increases by
one for each iteration. The value contained in “s” at the ith iteration is the sum of the first ‘i’
positive integers. If k is total number of iterations taken by the program, then while loop
terminates if:
1 + 2 + 3 + 4 + ⋯+ 𝑘 > 𝑛
The sum of first k positive integers can be given as:
𝑘(𝑘 + 1) 𝑘 2 + 𝑘
1 + 2 + 3 + 4 + ⋯+ 𝑘 = =
2 2
Hence the termination condition will become
𝑘2 + 𝑘
>𝑛
2
We can notice that k is a second-degree polynomial and while writing the complexity in big-
oh notation we will ignore the leading coefficients and polynomials. Hence the termination
condition will be
𝑘2 > 𝑛
Solving for k we get
𝑘 > √𝑛
Hence the complexity is 𝑂(√𝑛).

National University of Computer and Emerging Sciences – FAST, CFD Campus 5


CS2002 – DSA Assignment Number 1

Question Number 7 (CLO 1) (1 + 1 + 2 + 4 + 4 + 3 = 15 marks)


Compute the time complexity in big-oh notation for the following six code segments. Support
your answer with proper reason/calculations.

(1) sum = 0;
for(int i = 0; i < n; i+=2)
for(int j = 0; j < n*n; j++)
sum++;
(2) sum = 0;
for(int i = 1; i <= n; i*=2)
for(int j = 0; j < i; j++)
sum++;
(3) sum = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < i*i; j++)
for(int k = 0; k < j; k++)
sum++;
(4) sum = 0;
for(int i = 1; i < n; i++)
for(int j = 1; j < i*i; j++)
if(j % i == 0)
for(int k = 0; k < j; k++)
sum++;
(5) int i, j, k = 0;
for (i = n / 2; i <= n; i++)
for (j = 2; j <= n; j = j * 2)
k = k + n / 2;
(6) int a = 0, i = n;
while (i > 0)
{
a += i;
i /= 2;
}

1) Here we have two nested for loops, the outer loop has a maximum of n iterations, and the
inner loop has a possible n2 iterations, hence the complexity is O(n3).

2) Here we have two nested for loops with n iterations in inner and log2n iterations in outer
loop, hence complexity is O(nlog2n).

3) Here we have three level nesting. The inner loop has maximum j iterations, the loop with
variable j has a maximum of n2 iterations and the outer loop has a maximum n iteration,
hence the complexity is O(n5).

4) Here j can be as large as n2, so the if statement is executed at most O(n3) times, but the
condition is true only O(n2) times, only i times for each j, hence the complexity is O(n4).

5) The outer loop will have n/2 iterations, due to single increment after each iteration. The inner
loop will have log(n) iterations because after each iteration the value of j is multiplied by 2.
Hence, the overall complexity will be O(n log(n)).

6) Here the termination condition of loop is decided by the variable i. the initial value of i is n
and after each iteration the value of i is divided by 2. Hence the complexity is O(log(n)).

National University of Computer and Emerging Sciences – FAST, CFD Campus 6


CS2002 – DSA Assignment Number 1

Question Number 8 (CLO 1) (5 + 5 + 5 = 15 marks)


Compute the time complexities for the following functions in big-oh notation. Support your
answer with proper reason/calculations.
(1) function(int n)
{
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
cout<<“ * ”;
break;
}
}
}
(2) void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j<=n; j = 2 * j)
for (int k=1; k<=n; k = k * 2)
count++;
}
(3) void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j+n/2<=n; j = j++)
for (int k=1; k<=n; k = k * 2)
count++;
}

1) There are n iterations in outer loop but there is a break statement in inner loop due to
which the inner loop will run only once. Hence the complexity is O(n).

2) The outer loop will have a maximum of n/2 iterations due to single increment in each
iteration. The loop with variable j will have log(n) iterations because after every
iteration the value of j is multiplied by 2. Similarly, the loop with variable k will have
log(n) iterations. Hence the complexity is O[(n/2) (log(n)) (log(n))] = O(nlog2(n)).

3) The outer loop will be having n/2 iterations due to single increment after each
iteration. The loop with variable j will be having n/2 iterations due to single increment
after each iteration. The loop with variable k will have log(n) iterations because the
value of k is multiplied by 2 after every iteration. Hence the complexity is
O[(n/2) (n/2) (log(n))] = O(n2log(n)).

National University of Computer and Emerging Sciences – FAST, CFD Campus 7


CS2002 – DSA Assignment Number 1

Question Number 9 (CLO 1) (3 + 3 + 4 = 10 marks)


The following code is supposed to search an element from an unsorted 2D array. Compute the
best and worst-case time complexity. Also comment about the accuracy of the code.

void function(int key)


{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(arr[i][j] == key)
{
cout<<“Found”<<endl;
}
break;
}
}
}

Best Case
The best is when the key we want to find is located at index [0][0]. According to code, there
is an unconditional break statement in inner loop due to which the inner loop will run only
once, and the outer loop will be having a maximum of r iterations. Hence the best-case time
complexity is O(r).

Worst Case
The worst case will occur when the key we want to search is not present in the array at all
and we must search the entire array. But due to unconditional break statement in inner loop
there are a possible r iteration of the outer loop. Hence the worst-case time complexity is
O(r).

Accuracy of Code
The code is not accurate at all. It will search the key only in the first element of every row.
All other elements will go unchecked due to unconditional break statement.

National University of Computer and Emerging Sciences – FAST, CFD Campus 8

You might also like