I'm filling a lower triangular matrix like this:
for (i = 0; i < size; i++) {
for (j = 0; j <= i; j++)
l[i][j] = j + 1;
}
And I want to calculate the order of the code in Big O notation but I'm really bad. If it was a regular matrix it would be O(n²) but in this case I'm not sure if it's O(nlog(n)) or something like that.
Typically (but not always) one loop nested in another will cause O(N²).
Think about it, the inner loop is executed i times, for each value of j. The outer loop is executed size times.
This comes out to be 1/2 of N^2, which is still O(N^2)
Related
I get O(n^2 logn) as output of the following code. Yet I am unable to understand why?
int unknown(int n) {
int i, j, k = 0;
for (i = n / 2; i <= n; i++)
for (j = 2; j <= n; j = j * 2)
k = k + n / 2;
return k;
}
A fixed constant starting point will make no difference to the inner loop in terms of complexity.
Starting at two instead of one will mean one less iteration but the ratio is still a logarithmic one.
Think in terms of what happens when you double n. This adds one more iteration to that loop regardless of whether you start at one or two. Hence it's O(log N) complexity.
However, you should keep in mind that the outer loop is an O(N) one since the number of iteratations is proportional to N. That makes the function as a whole O(N log N), not the O(N2 log N) you posit.
How is this loop's time complexity O(n^2)?
for (int i = n; i > 0; i -= c)
{
for (int j = i+1; j <=n; j += c)
{
// some O(1) expressions
}
}
Can anyone explain?
Assumption
n > 0
c > 0
First loop
The first loop start with i=n and at each step, it substracts c from i. On one hand, if c is big, then the first loop will be iterated only a few times. (Try with n=50, c=20, you will see). On the other hand, if c is small (let say c=1), then it will iterate n times.
Second loop
The second loop is the same reasoning. If c is big, then it will be iterated only a few times, if c is small, many times and at the worst case n times.
Combined / Big O
Big O notation gives you the upper bound for time complexity of an algorithm. In your case, first and second loop upper bound combined, it gives you a O(n*n)=O(n^2).
I'm trying to write code to find A in a system of linear equations Ax=B, so I used LU decomposition. Now that I have L and U properly, I'm stuck in the forward substitution in order to get the y in B=Ly.
I wrote some code in MatLab that works perfectly, but I can't get the same results rewriting the code in C. So I was wondering if someone may know what i'm doing wrong, I'm not fully used to C.
Here's my code in MatLab:
y(1,1) = B(1,1)/L(1,1);
for i= 2:n
sum=0;
sum2=0;
for k = 1: i-1
sum = sum + L(i,k)*y(k,1);
end
y(i,1)=(B(i,1)-sum)/L(i,i);
end
where L is my lower triangle matrix, B is a vector of the same size, and n is 2498 in this case.
My C code is the following:
float sum = 0;
y_prev[0]=B[0]/(float)Low[0][0];
for (int i = 1; i < CONST; i++)
{
for (int k = 0; k < i-1; k++)
{
sum = sum +Low[i][k]*y_prev[k];
}
y_prev[i]= (B[i]- sum)/(float)Low[i][i];
}
One difference between the codes comes from the way you've changed the for loop indices to work with the zero based indexing in C. (I can't run the MATLAB version, and don't have some of the context for the code, so there may be other differences.)
The variables i and k have values which are smaller by 1 in the C code. This is exactly what you want for the loop indices, but a problem arises when you use i to control the number of iterations in the inner loop over k. This is i-1 in both versions of the code, even though i has different values. For instance, in the first iteration of the outer loop the inner loop runs once in the MATLAB code but not at all in the C one.
A possible fix would be to rewrite the inner loop in the C code as
for (int k = 0; k < i; k++)
{
sum = sum +Low[i][k]*y_prev[k];
}
A second difference is that you're resetting sum to zero in the MATLAB code but not in the C (the MATLAB code also has a sum2 which doesn't seem to be used?). This will cause differences in y_prev[i] for i>0.
So I get that the first for loop runs O(n) times, then inside that it runs 3 times, then 3 times again. How do I express this at big O notation though? Then do the 2 print statements matter? How do I add them to my big-o expression? Thanks, really confused and appreciate any help.
for (int x = 0; x < n; x++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
printf("%d", arr[x]);
}
printf("\n");
}
}
O(n) is linear time, so any k * O(n) where k is a constant (like in your example) is also linear time and is just expressed as O(n). Your example has O(n) time complexity.
Big O notation are always defined as a function of input size - n. Big O gives the upper limit of total time taken to run that module. Because your inner "for" loops are always run 3*3 =9 times irrespective of the input size of n - there are still considered as constant time in Big O calculations
Time Complexity = O(n+9+constantTimeToPrint) = O(n)
The two inner loops are constant, so it's still O(n). constant factors don't matter, the runtime varies only with the input size.
Time complexity of a triple-nested loop
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
for(int k=j+1; k<n; k++)
I want to know the right solution of time complexity.
A formal solution to determine the order of growth of your algorithm:
First guess: three loops depending on n, so it should be O(n³)
If you try to compute the exact complexity you have to compute it for the inner an multiply it with the outer loop.
inner loop takes O(n-k)
middle loop takes O(n-j + n-j-1 + ... + n-j-n) = O((n-j) ⋅ (n-j+1) / 2) = O((n-j)²)
outer loop takes O((n-1)² + (n-2)² + ... + (n-n+1)²) = O(n³)
Sure this is not exact, but in terms of big-O it is exact enought.