Need help figuring this hw example out guys.
the example states 0(n) running time.
I see that the outter loop is O(logn)
I cant figure out how to describe the inner loop in relation to n.
Help is much appreciated.
for (int i = 1; i <= N; i = i*2) // log n
for (int j = 0; j < i; j++) // less than n i don't know how to describe the growth
sum++;
Answer:: 0(n)
It might be easiest to analyze the runtime of the piece of code in the aggregate. On the first iteration of the outer loop, the inner loop will run 1 time. On the second iteration of the outer loop, the inner loop will run 2 times. On the third iteration of the outer loop, the inner loop will run 4 times. More generally, on the kth iteration of the outer loop, the inner loop will run 2k times. The outer loop stops as soon as i becomes greater than N, which happens after log2 N iterations.
If we sum up the total work done, we'll see that it's
1 + 2 + 4 + 8 + ... + 2log2 N = 2log2 N + 1 - 1 = 2n - 1
(This uses the fact that 1 + 2 + 4 + 8 + ... + 2k = 2k+1 - 1). Therefore, the total work done for the entire piece of code (that is, including both loops) is O(n).
The inner loop is O(n) time because it will go through each element once. If i is 1000, 1000 times, if i is 10000, 10000 times, etc.
Related
If the the first loop runs for n+1 times.
second loop runs for n(n+1) times.
third loop will run for ??? it has n^2+1 one relation with with with the second loop i guess but how about with the first one
somefunction(n) {
c = 0
for (i = 1 to n*n)
for (j = 1 to n)
for (k = 1 to 2*j)
c = c+1
return c
}
The first loop has O(n**2) iterations.
The second loop has O(n) iterations.
The third loop has O(n) iterations as well, since j is steadily increasing towards n.
(It's a little easier to see if you sum up the number of times c = c + 1 executes for the two inner loops combined. The inner loop runs 2 times for j = 1, 4 for j = 2, ..., and 2*n times for j = n. 2 + 4 + .. + 2*n = O(n**2).)
You can then (loosely speaking) multiply the three values together to get a total bound of O(n**4).
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)
i am trying to find the time complexity of the bubble sort
n=length[A]
for j <- n-1 to 1
for i <- 0 to j-1
if A[i]>a[i+1]
temp=A[i]
A[i]=A[i+1]
A[i+1]=temp
return A
please any one can help thanks
In line 1 we are assigning length of array to n so constant time
In line 2 we have a for loop that decrements j by 1 every iteration until j=1 and in total will iterate n-2 times.
Inside the first for loop we have a second for loop that increments i by 1 every iteration until i=j-1 and will iterate j-1 times. On each iteration of the inner for loop we have lines 4,5,6,7 which are all just assignments and array access which cost, in total, constant time.
We can think about the two for loops in the following way: For every iteration of the outer for loop, the inner for loop will iterate j-1 times.
Therefore on the first iteration of the outer for loop, we have j = n-1. That means the inner for loop will iterate (n-1)-1 = (n-2) times. Then on the second iteration of the outer for loop we have j= n-2 so the inner for loop will iterate (n-2)-1 = (n-3) times and so on. And we do this until j = 1.
We will then have the equation: (n-2) + (n-3) + ... + 2 + 1 which is the total number of times the inner loop will iterate after the entire algorithm executes. We know that 1 + 2 + ... + n-1 + n = n(n-1)/2 so our expression can be simplified to this: n(n-1)/2 -(n-1) -n = n(n-1)/2 -2n + 1 = O(n^2).
Since our inner for loop will iterate O(n^2) times, and on each iteration do constant work, then that means our runtime will be O(cn^2) where c is the amount of constant work done by lines 4,5,6,7. Combine O(cn^2) with line 1 which is O(1) we have O(cn^2) + O(1) which is just O(n^2).
Therefore runtime of BubbleSort is O(n^2).
If you are still confused then maybe this will help: https://www.youtube.com/watch?v=Jdtq5uKz-w4
I have the following pseudocode:
SelectionSort(A)
n = A.length
for j=1 to n-1
smallest = j
for i=(j+1) to n
if A[i] < A[smallest]
smallest = i
exchange A[j] with A[smallest]
I think that the first for-loop test will execute n times, and the nested for-loop will execute 1 + 2 + ... + n = n(n+1)/2 times (please correct me if i'm wrong). But I don't understand how I can tell how many times the nested if-statement will execute? Is it 1/2 * n(n+1)/2 ?
The outer for-loop would run for n times. But, it also holds an inner for-loop which is dependent on the vale of j.
I think that the first for-loop test will execute n times, and the
nested for-loop will execute 1 + 2 + ... + n = n(n+1)/2 times.
The inner for-loop will run (n-1)+(n-2)+...+1 times based on all the iterations of outer for-loop. So, the net iteration of the loop-statements combinedly would be (n-1) + (n-2) + (n-3) + ... + 1 = (n-1)*n/2 = (n2-n)/2 times.
But I don't understand how I can tell how many times the nested
if-statement will execute? Is it 1/2 * n(n+1)/2 ?
As the inner-if statement is dependent on the array elements, it is not possible to directly ascertain exactly how many times it will run.
But, it is for sure that in the worst-case condition,as it is located in the inner for-loop, the maximum number of possible execution(in the worst case) of the if-statement will be (n2-n)/2 times.
So, the worst-case complexity of execution of if-statement is
O(n2)
The outer loop shall run from 1 to n-1, hence n-1 times.
The inner loop shall run from j+1 to n times. This means that when j is 1, it will run from 2 to n times (n-1 times) and when j is n-1, it will run from n to n times (1 time).
Hence the inner loop shall run (n-1 + n-2 + ... + 1) times = n(n-1)/2 times.
The if statement shall execute the same number of times as the inner loop does. Ofcourse the conditional statments shall depend on the result of if conditional.
int sum = 0;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= i*i; j++)
sum++;
Is this complexity 1 + 2^2 + 3^2 + 4^2 ... + N^2? How to represent it in big-o notation?
The sum is N(N+1)(2N+1)/6 so your algorithm is O(N^3).
If you expand the sum you have 1/3 * N^3 + ...
You can see it simply by plotting the values of sum after running the algorithm for different values of N:
Straightforwardly, using Sigma notation:
Another way to look at it is to use the "worst case" analysis.
Originally, we have:
The outer for loop will run N times
The inner for loop will run i*i times
This might be confusing since i seems to be changing based on the outer for loop.
However, in the "worst case" scenario, the largest value that i ever attains is N.
Therefore, just substitute N instead of i in our analysis.
Now we have:
The outer for loop will run N times
The inner for loop will run N*N times
And the time complexity of this situation is of course O(N³).
you can always use the intuitive way.
for i = 2 second loop 1 time, first loop 1 time. 1*1
for i = 3 second loop 9 times, first loop 2 times. 9*2
for i = 4 second loop 16 times, first loop 3 times 16*3
for i = 5 second loop 25 times, first loop 4 times. 25*4
.
.
.
you can see the pattern easily. it is (nˆ2)*(n-1) therefore its big o should be O(nˆ3)