Hi i am trying to find out the big-O of this algorithm.
I think it is n^2 but because the size of the sub loop is shrinking each time I am not sure.
for(int i= 0; i < SIZE; i++){
for(int j = i; j < SIZE; j++)
{
//Code here
}
}
Yes, it's O(n^2) if SIZE = O(n) and the complexity of (code here) is a constant…
You can do the math: for a fixed value of i, the inner loop is executed Size-i times, thus the total number of execution of the inner loop is sum_{i=0}^{size-1} (size-i) = size^2 - sum_{i=0}^{size-1} i = size^2 - 1/2 size * (size-1) = O(size^2)
There's a methodology using Sigma notation that is precise enough:
Related
What would be the time complexity for the code below?
My guess is O(n log(n)) or O(n log(log(n))), but really not sure.
Would appreciate any help!
for (int i = 1; i < n; ++i) {
for (int j = 1; j < log (n); j *= 2) {
cout << '-';
}
}
it is clear that the first loop will run in
O(n)
the inner loop can be calculated as follows if look at log(n) (the stop condition of the loop) as k then it'll look like this
for(int j = 0; j < k; j *= 2)
the runtime of this for loop is
O(logk)
if we replace back log(n) instead of k we get the following
O(log(logn))
hence this is an inner loop we need to multiply its runtime with the outer loop runtime.
O(n) * O(log(logn)) => O(nlog(logn)).
Here you can read more about loops runtime and how to analyze it.
I have seen that in some cases the complexity of nested loops is O(n^2), but I was wondering in which cases we can have the following complexities of nested loops:
O(n)
O(log n) I have seen somewhere a case like this, but I do not recall the exact example.
I mean is there any kind of formulae or trick to calculate the complexity of nested loops? Sometimes when I apply summation formulas I do not get the right answer.
Some examples would be great, thanks.
Here is an example for you where the time complexity is O(n), but you have a double loop:
int cnt = 0;
for (int i = N; i > 0; i /= 2) {
for (int j = 0; j < i; j++) {
cnt += 1;
}
}
You can prove the complexity in the following way:
The first iteration, the j loop runs N times. The second iteration, the j loop runs N / 2 times. i-th iteration, the j loop runs N / 2^i times.
So in total: N * ( 1 + 1/2 + 1/4 + 1/8 + … ) < 2 * N = O(N)
It would be tempting to say that something like this runs in O(log(n)):
int cnt = 0;
for (int i = 1; i < N; i *= 2) {
for (int j = 1; j < i; j*= 2) {
cnt += 1;
}
}
But I believe that this runs in O(log^2(N)) which is polylogarithmic
Can someone please explain how the worst case running time is O(N) and not O(N^2)in the following excercise. There is double for loop, where for every i we need to compare j to i , sum++ and then increment and again repeat the operation until reach N.
What is the order of growth of the worst case running time of the following code fragment
as a function of N?
int sum = 0;
for (int i = 1; i <= N; i = i*2)
for (int j = 0; j < i; j++)
sum++;
Question Explanation
The answer is : N
The body of the inner loop is executed 1 + 2 + 4 + 8 + ... + N ~ 2N times.
I think you already stated the answer in your question -- the inner loop is executed 2N times, which is O(N). In asymptotic (or big-O) notation any multiples are dropped because for very, very large values, the graph of 2N looks just like N, so it isn't considered significant. In this case, the complexity of the problem is equal to the number of times "sum++" is called, because the algorithm is so simple. Does that make sense?
Complexity doesn't depends upon number of nested loops
it is O(Nc):
Time complexity of nested loops is equal to the number of times theinnermost statement is executed.For example the following sample loops have O(N2) time complexity
for (int i = 1; i <=n; i += c) {
for (int j = 1; j <=n; j += c) {
// some O(1) expressions
}
}
for (int i = n; i > 0; i += c) {
for (int j = i+1; j <=n; j += c) {
// some O(1) expressions
}
I'm asked to give a big-O estimates for some pieces of code but I'm having a little bit of trouble
int sum = 0;
for (int i = 0; i < n; i = i + 2) {
for (int j = 0; j < 10; j + +)
sum = sum + i + j;
I'm thinking that the worst case would be O(n/2) because the outer for loop is from i to array length n. However, I'm not sure if the inner loop affects the Big O.
int sum = 0;
for (int i = n; i > n/2; i − −) {
for (int j = 0; j < n; j + +)
sum = sum + i + j;
For this one, I'm thinking it would be O(n^2/2) because the inner loop is from j to n and the outer loop is from n to n/2 which gives me n*(n/2)
int sum = 0;
for (int i = n; i > n − 2; i − −) {
for (int j = 0; j < n; j+ = 5)
sum = sum + i + j;
I'm pretty lost on this one. My guess is O(n^2-2/5)
Your running times for the first two examples are correct.
For the first example, the inner loop of course always executes 10 times. So we can say the total running time is O(10n/2).
For the last example, the outer loop only executes twice, and the inner loop n/5 times, so the total running time is O(2n/5).
Note that, because of the way big-O complexity is defined, constant factors and asymptotically smaller terms are negligible, so your complexities can / should be simplified to:
O(n)
O(n2)
O(n)
If you were to take into account constant factors (using something other than big-O notation of course - perhaps ~-notation), you may have to be explicit about what constitutes a unit of work - perhaps sum = sum + i + j constitutes 2 units of work instead of just 1, since there are 2 addition operations.
You're NOT running nested loops:
for (int i = 0; i < n; i = i + 2);
^----
That semicolon is TERMINATING the loop definition, so the i loop is just counting from 0 -> n, in steps of 2, without doing anything. The j loop is completely independent of the i loop - both are simply dependent on n for their execution time.
For the above algorithms worst case/best case are the same.
In case of Big O notation, lower order terms and coefficient of highest order term can be ignored as Big O is used for describing asymptotic upper bound.
int sum = 0;
for (int i = 0; i < n; i = i + 2) {
for (int j = 0; j < 10; j + +)
sum = sum + i + j;
Total number of outer loop iteration =n/2.for each iteration of outer loop, number of inner loop iterations=10.so total number of inner loop iterations=10*n/2=5n. so clearly it is O(n).
Now think about rest two programs and determine time complexities on your own.
I was just wondering what is the time complexity of the following code.
The time complexity (Big O) of the code below in my opinion would be O(n^4)
What do you guys think?
int result = 0;
for(int i =1; i<n*n; i++){
for (int j=i; j*j <n; j++){
for(int k =j; k*k <n; k++){
result++;
}
}
}
Looks like n^(2.75) to me:
- outer loop: n^2
- first inner loop is sqrt(n)
- second inner loop is sqrt(sqrt(n))
Total of:
n^2 * sqrt(n) * sqrt(sqrt(n)) = n^(2+ 0.5 + 0.25) = n^(2.75)
Formal steps (need to be verified) using Sigma Notation would yield the following: