Time complexity of algorithm T(n) - algorithm

Could someone tell me the time complexity of this algorithm? The book says it is T(n) = (n-1)(1+n/4) = O(n2)
for ( i = 2 ; i <= n ; i++) {
for ( j = 0 ; j <= n) {
cout << i << j ;
j = j + floor(n/4) ;
}
}
But i don't know how to calculate it.

The outer loop iterates n-1 times, the inner loop iterates n/4 times . The complexity for the algorithm is (n-1)*(n/4)= (n^2-n)/4 = O(n^2)

Related

How do i find the big O notation

int something = 0 ;
int t = n ;
while ( t > 1 ) {
for (int i=0 ; i < t ; i++) {
something++ ;
}
t = t / 2 ;
}
//number 2
int sum = 0;
int i = 1;
while (sum <= n) {
sum = sum + i;
i++;
}
How do I find the tightest upper bound in big O notation. I think they would both be log n but I am not sure if the for loop in the first segment of code affects its run time significantly.
The complexity of the above code is O(n).
Let's calculate the complexity of first part of the program.
int t = n;
while ( t > 1 ) {
for (int i=0 ; i < t ; i++) {
something++ ;
}
t = t / 2 ;
}
This will execute: t + t/2 + t/4 + ... + 1
and the sum of this series is 2t - 1. (Refer this).
Therefore, the time complexity of the first part if O(2t - 1) = O(t) or O(n).
For second part:
int sum = 0;
int i = 1;
while (sum <= n) {
sum = sum + i;
i++;
}
Sum will be 1+2+3+....i which is i*(i+1)/2.
So sum =i*(i+1)/2 ~ i2
Also sum <= n
So i2 <= n
or i ~
Therfore the complexity of second part is O()
So the complexity of program is:
T(n): O(n) + O() = O(n)
Overall complexity is O(n)

Can't understand big-O of a nested loop

I am having problem understanding the answer to the following question about analyzing two algorithms below.
for (int i = n ; i >= 1; i = i/2) {
for ( int j = 1; j <= n ; j++) {
//do something
}
}
The algorithm above has complexity of O(n) according to the answers. Shouldn't it be lower since the outer loop always halves the amount we have to go through. I thought that it should be something along the lines of O(n/2 * )?
for ( int j = 1; j <= n ; j++ ) {
for ( int i = n ; i >= j ; i = i / 2 ) {
//do something
}
}
This one is O(n log n) if I am correct?
The first iteration will execute n steps, the second will execute n/2, the third will execute n/4, and so on.
If you compute the sum of n/(2^i) for i=0..log n you will get roughly 2n and that is why it is O(n).
If you take n out of the summation and sum only the 1/(2^i) part, you will get 2. Take a look at an example:
1 + 1/2 + 1/4 + 1/8 + 1/16 + ... = 1 + 0.5 + 0.25 + 0.125 + 0.0625 + ... = 2
Each next element is twice smaller, therefore the sum will never exceed 2.
You are right with the second nested loop example - it is O(n log n).
EDIT:
After the comment from ringø I re-read the question and in fact the algorithm is different from what I understood. ringø is right, the algorithm as described in the question is O(n log n). However, judging from the context I think that the OP meant an algorithm where the inner loop is tied to i and not n.
This answer relates to the following algorithm:
for (int i = n ; i >= 1; i = i/2) {
for ( int j = 1; j <= i ; j++) {
//do something
}
}

Algorithm_BIG_O

I need help understanding/doing Big O Notation. I understand the purpose of it, I don't know how to determine the complexity.Below are the few examples which i currently retrieved from pass year paper for doing revision before exam! And i had provide my answer for some of the question, please do help me check whether if it correct or not, tq! ##
Example 1:
for (int i = 0; sqrt(i) < n; i++)
cout << i << endl;
The code will run until sqrt(i) >= n which means that i >= n^2 so it's O(n^2)
The outer loops runs n times, the inner loop runs log(n) so that's O(n*log(n))
the first loop is O(n), after that the value of k is 2^n so the second loop is O(2^n) so in total it's O(n) + O(2^n) = O(2^n)
first part :
for (int i = 0; sqrt(i) < n; i++)
cout << i << endl;
sqrt(i) < n => i < n^2 so this loop takes O(n^2)
second part :
for(int i = 0 ; i < n ; i++){
outer loop run in O(n) [ increments by constant ]
while (k > 0){ // k >= 1
k /= 2;
k = n, n/2 , n/4 , ..... n/2^i
inner loop will stop when k == 1,
n/2^i = 1 => 2^i = n => i = log(n)
so total = O(n * log(n))
last part :
for (int i = 0; i < n; i++)
k = k * 2;
takes O(n) because it increments by constant.
second loop :
what is k value ?
k = 1 , i = 0
k = 2 , i = 1
k = 4 , i = 2
k = 8 , i = 3
k = 2^i
when i == n loop stops
value of k = 2^n
so total order = O(n + 2^n) = O(2^n)
O^1 = sequences of statements and conditions
O^n = loops
O^n*m = loops inside loops
Hope that helps.

Big O time complexity for nested j = i + 1 loop

Can anyone please let me what would be big O time complexity for the following piece of code:
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
// do something
}
}
It can't be O(n^2) since j = i + 1 ? Thanks!
There are n-1 iterations of the outer loop. On each iteration, the inner loop iterates n-i-1 times. So in total the inner loop iterates n-1 + n-2 + ... + 1 times. So the number of times that do something executes is equal to the sum of the numbers from 1 to n-1. That sum is n*(n-1)/2, which is in Theta(n^2) and thus also in O(n^2).

Computational complexity of a piece of code

I have got a program, and trying to compute its complexity. I want to be sure i am not mistaken
for(int i=4; i<=n; i=i*4)
{
cout<<"counter for first loop: "<<++count1<<endl;
for(int j=i;j>=0;j=j-4)
{
cout<<"counter for second loop: "<<++count2<<endl;
for(int k=0;k<=n;k++)
{
cout<<"counter for third loop: "<<++count3<<endl;
}
}
}
Here, the complexity of third loop is O(n), then together with the second loop, the complexity becomes O(n.log4i), and the complexity of whole program is O(n.(log4i)2). Am i right in my answer? Thanks
The complexity of the inner most loop is O(n). The complexity of the middle one is O(i/4), which in turn is O(i). The complexity of the outer most loop is O(log4n). There for the total complexity of the code is O(n.i.log4n) which is equal to O (n.(log4n)2).
You can proceed formally like the following:
Executing this fragment:
sum = 0;
for( i = 4 ; i <= n; i = i * 4 ) {
for( j = i ; j >= 0 ; j = j - 4 ) {
for( k = 0 ; k <= n ; k ++ ) {
sum ++;
}
}
}
We obtain:
Results exactly compatible with the formula above.
Besides, both inner loops' runtime is O(n) ... which means that, when executed together, we get O(n²).

Resources