What will be time complexity of the following? - data-structures

What will be time complexity of the following function?
How can be the time complexity of inner loop be log(n)?
it is given that the inner loop executes n/i times for each value of i.its running time is n*∑(from i=1 to n){n/i}.. and that i dnt get it
fucntion(n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j+=i)
{
printf("*");
}
}
}

In the first run of inner loop, we will loop n times. The second time we will skip every other number, so we loop n/2 times, then n/3 times, etc. This continues until the final loop where we go n/n = 1 pass through the loop.
As discussed here, 1 + 1/2 + 1/3 + ... + 1/n is O(log n). Since we are doing n times that, the resulting complexity is O(n log n).

Related

How nested for loop that has complexity O(n)?

Given:
for (int i = 1; i <= n; i *= 2) {
for (int j = 0; j < i; j++) {
// Statement(s) that take(s) constant time
}
}
Running Time Complexity = O(n)
Explanation was:
I understand that the outer loop is log(n) and the inner loop is O(n).
But why time complexity is not O(n log n)? why it's O(n + log n)?
The partial sums calculation is correct. However, the total execution time explanation is unclear, although the result is indeed O(n).
The actual time is consumed by executing the inner statements and the number of these statements executions is defined by the loops. The method of partial sums already takes into consideration both loops (1 time unit for i=1, 2 time units for i=2, 4 time units for i=4, etc.) and in total the internal statements are executed O(n) times as shown by the partial sums calculation. Therefore, total time is O(n)*O(constant for the internal statements)=O(n). I don't see any reason to include the O(logn) in calculations here.
The outer loop runs log(n) times. But, in each time, the inner loop does not run in O(n). As shown in the explanation, if i = 1, the inner loop runs 1 time. If i = 2, the inner loop runs 2 times. If i = 4, the inner loop runs 4 times. Hence, the total number of running time is T(n) = 1 + 2 + 4 + ... + 2^log(n).
Now, simplify the T(n):
T(n) = 2^(log(n)+1) - 1 = 2^log(n) * 2 - 1 = 2n - 1 = Theta(n)

What is the time complexity of the given algorthm?

x=0
for i=1 to ceiling(log(n))
for j=1 to i
for k=1 to 10
x=x+1
I've included the answer I've come up with here:
I think the time complexity is θ(n^2 log(n)), but I am not sure my logic is correct. I would really appreciate any help understanding how to do this type of analysis!
Outermost loop will run for ceil(log n) times. The middle loop is dependent on the value of i.
So, it's behaviour will be :
1st iteration of outermost-loop - 1
2nd iteration of outermost-loop - 2
.....................................
ceil(log n) iteration of outermost-loop - ceil(log n)
Innermost loop is independent of other variables an will always run 10 times for each iteration of middle-loop.
Therefore, net-iterations
= [1*10 + 2*10 + 3*10 + ... + ceil(log n)*10]
= 10 * {1+2+...+ceil(log n)}
= 10 * { (ceil(log n) * ceil(log n)+1)/2} times
= 5 * [ceil(log n)]^2 + 5 * ceil(log n)
= Big-Theta {(log n)^2}
= Θ{(log n)^2}.
I hope this is clear to you. Hence, your answer is incorrect.
You have three loops. Lets consider one by one.
Innermost loop: It is independent of a n or i, and will run always 10 times. So time complexity of this loop is Theta(10).
Outermost loop: Very simply time complexity of this loop is Theta(logn).
Middle loop: As value of i can be upto logn time complexity of this loop is also O(logn)
Overall complexity: Theta(logn)*O(logn)*Theta(10) or O(logn*logn*10) or 10*O((logn)^2) or O((logn)^2)

Computational complexity of for loops-Contradicting with myself

I have a contradiction by analyzing the running time of a program. For example, consider the following piece of code:
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
.....
}
}
Here, the computational complexity of 1st for loop is O(n2), and for the second loop it is O(n). However, the second loop is executed n2 times whereas first loop is executed n times. For example, if we put a cout statement inside the inner loop, it outputs n2 times, but if we put a cout somewhere inside the 1st loop but outside the inner loop, it outputs n times. So why do we say the complexity of inner loop is O(n), but for outer loop it is O(n2). We say the complexity of outer loop is O(n2) but it executes n times, why is this the case? Am i doing something wrong? Thanks.
The inner loop executes n times, which takes O(n). The outer loop executes the inner loop n times, but you have to account for the cost of the inner loop for each of those n outer loop executions. This makes it O(n * O(n)) = O(n^2).
Outer loop will run for n times and inner loop will run for n times for every iteranion of outer loop making inner loop to run for n^2 times. Thus statements in inner loop will get executed for n^2 times.
Let say, n = 7 ; then
for(int i=0 ; i< 7 ; i++) //time complexity is : (1) + (7+1) + (7)
{
for(int j=0;j<7;j++) // time complexity is: (1) + [(7+1)+(7+1)+(7+1)+(7+1)+(7+1)+(7+1)+(7+1)] + (7)
{
.....
}
}
Total time complexity = [2+2(7)] + [1 + (7+1)^2 + 7]
Now replace 7 = n ; We will get
Total time complexity = [2+2n] + [1 + (n+1)^2 + n] = 2 + 2n + 1 + (n^2 + 2n + 1) + n = n^2 + 5n + 4
Here the dominant term is n^2. So the worst case will be O(n^2)

What is the running time complexity of this algorithm

What is the time complexity of this algorithm:
sum = 0
i = 1
while (i < n) {
for j = 1 to i {
sum = sum + 1
}
i = i*2;
}
return sum
I know that the while loop is O(logn), but what is the complexity of the for loop? Is it O(n) or O(logn)?
One way to analyze this would be to count up the number of iterations of the inner loop. On the first iteration, the loop runs one time. On the second iteration, it runs two times. It runs four times on the third iteration, eight times on the fourth iteration, and more generally 2k times on the kth iteration. This means that the number of iterations of the inner loop is given by
1 + 2 + 4 + 8 + ... + 2r = 2r + 1 - 1
Where r is the number of times that the inner loop runs. As you noted, r is roughly log n, meaning that this summation works out to (approximately)
2log n + 1 - 1 = 2(2log n) - 1 = 2n - 1
Consequently, the total work done by the inner loop across all iterations in O(n). Since the program does a total of O(log n) work running the outer loop, the total runtime of this algorithm is O(n + log n) = O(n). Note that we don't multiply these terms together, since the O(log n) term is the total amount of work done purely in the maintenance of the outer loops and the O(n) term is total amount of work done purely by the inner loop.
Hope this helps!

Complexity of a double for loop

I am trying to figure out the complexity of a for loop using Big O notation. I have done this before in my other classes, but this one is more rigorous than the others because it is on the actual algorithm. The code is as follows:
for(cnt = 0, i=1; i<=n; i++) //for any size n
{
for(j = 1; j <= i; j++)
{
cnt++;
}
}
AND
for(cnt = 0, i=1; i<=n; i*=2) //for any size n
{
for(j = 1; j <= i; j++)
{
cnt++;
}
}
I have arrived that the first loop is of O(n) complexity because it is going through the list n times. As for the second loop I am a little lost. I believe that it is going through the loop i times for each n that is tested. I have (incorrectly) assumed that this means that the loop is O(n*i) for each time it is evaluated. Is there anything that I'm missing in my assumption. I know that cnt++ is constant time.
Thank you for the help in the analysis. Each loop is in its own space, they are not together.
The outer loop of the first example executes n times. For each iteration of the outer loop, the inner loop gets executed i times, so the overall complexity can be calculated as follows: one for the first iteration plus two for the second iteration plus three for the third iteration and so on, plus n for the n-th iteration.
1+2+3+4+5+...+n = (n*(n-1))/2 --> O(n^2)
The second example is trickier: since i doubles every iteration, the outer loop executes only Log2(n) times. Assuming that n is a power of 2, the total for the inner loop is
1+2+4+8+16+...+n
which is 2^Log2(n)-1 = n-1 for the complexity O(n).
For ns that are not powers of two the exact number of iterations is (2^(Log2(n)+1))-1, which is still O(n):
1 -> 1
2..3 -> 3
4..7 -> 7
8..15 -> 15
16..31 -> 31
32..63 -> 63
and so on.
Hopefully this isn't homework, but I do see that you at least made an attempt here, so here's my take on this:
cnt is incremented n*(n+1)/2 times, which makes the entire set of both loops O(n^2). The second loop is O(n/2) on the average, which is O(n).
The first example is O(N^2) and What is the Big-O of a nested loop, where number of iterations in the inner loop is determined by the current iteration of the outer loop? would be the question that answers that where the key is to note that the inner loop's number of rounds is dependent on n.
The second example is likely O(n log n) since the outer loop is being incremented on a different scale than linear. Look at binary search for an example of a logarithmic complexity case. In the second example, the outer loop is O(log n) and the inner loop is O(n) which combine to give a O(n log n) complexity.

Resources