Big O notation for nested loops [duplicate] - java

This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 4 years ago.
I am just learning the Big O notation and wanted to ask how it works for nested loops.
Is it true that in the case of
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
do something;
}
}
It would be O(N squared), while
for (int i = 0; i < 1000; i++){
for (int j = 0; j < N; j++){
do something;
}
}
It would be O(N) because the first loop has a constant? Or would it still be O(N squared)? Thank you

Your first statement is correct.
N can be very large and O(n) takes it into account.
so first code is O(N^2)
while second is O(1000*N) => still O(N)
BIG O notation does not include constants

Related

Struggling to understand how two nested for-loops can still have O(n) runtime

I have the following piece of solution code to rotate an image to the right.
It first transposes the matrix and then flips it on the vertical axis:
public static void rotateSquareImageCW(int[][] matrix) {
transposeMatrix(matrix);
flipVerticalAxis(matrix);
}
public static void transposeMatrix(int[][] matrix) {
int n = matrix.length - 1;
int temp = 0;
for(int i = 0; i <= n; i++){
for(int j = i+1; j <= n; j++){
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
private static void flipVerticalAxis(int[][] matrix) {
int n = matrix.length - 1;
int temp = 0;
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n/2; j++){
temp = matrix[i][j];
matrix[i][j] = matrix[i][n-j];
matrix[i][n-j] = temp;
}
}
}
The code author (Firecode.io) says that this code runs in O(n) space and O(1) time.
However, we can see that the "transposeMatrix" and "flipVerticalAxis" functions have nested for-loops that iterate upon the rows/columns of the matrix.
Shouldn't this be O(N^2), dependent upon the size of the columns and rows?
How is this still O(N)? Can anyone explain or rationalize this?
As already pointed out in the comments, it depends on the definition of n in the O(n) expression.
The most plausible definition is that n is to denote the width or height of the matrix. This is in line with various matrix algorithm analyses, and additionally supported by the fact that the code uses a variable named n with exactly that meaning.
The code uses only a fixed set of additional storage elements, being i, j, n and temp, so it's constant space (not counting the pre-existing matrix), meaning O(1) space complexity.
The two nested loops each iterate over n (or n/2) elements, so you're absolutely right, that means O(n²).
So, it's O(1) space and O(n²) time complexity.

Calculating big-o, nested loops

I have a quick question regarding these nested loops:
for(int i = 0; i < N; i++) {
for(int j = 0; j < i; j++)
for(j = i; j < N; j = j/2)
}
I am seeing here at first loop we go O(N) times, second we go O(N) times and lastly we go O(log n). Is it wrong to say this adds up to O(N)?
this is an endless loop instead of j/2 I think it should be
j*2
as suggested in the comments, right now this is O(infinity) since the third loop never stops, so ill assume you meant to write this code
for(int i = 0; i < N; i++) {
for(int j = 0; j < i; j++)
for(k = i; k < N; k = k*2)
}
and in this case, the first and second loop will be O(N) and the third loop will be O(log(N)), but since the loops are nested the total complexity of your code will be
O(N*N*log(N)) = O(N^2 * Log(N))
if you have any questions about my answer feel free to ask in the comments , and if my comment helped you please consider marking it as the answer or upvoting :)

Big O for 3 nested for loops?

public int Loop(int[] array1) {
int result = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1.length; j++) {
for (int k = 1; k < array1.length; k = k * 2) {
result += j * j * array1[k] + array1[i] + array1[j];
}
}
}
return result;
}
I'm trying to find the complexity function that counts the number of arithmetic operations here. I know the complexity class would be O(n^3), but I'm having a bit of trouble counting the steps.
My reasoning so far is that I count the number of arithmetic operations which is 8, so would the complexity function just be 8n^3?
Any guidance in the right direction would be very much appreciated, thank you!
The first loop will run n times, the second loop will run n times however the third loop will run log(n) times (base 2). Since you are multiplying k by two each time the inverse operation would be to take the log. Multiplying we have O(n^2 log(n))
If we can agree that the following is one big step:
result += j * j * array1[k] + array1[i] + array1[j]
then let's call that incrementResult.
How many times is incrementResult called here? (log n)
for (int k = 1; k < array1.length; k = k * 2) {
// incrementResult
}
Lets call that loop3. Then how many times is loop3 called here? (n)
for (int j = 0; j < array1.length; j++) {
// loop 3
}
Let's call that loop2. Then, how many times is loop2 called here? (n)
for (int i = 0; i < array1.length; i++) {
// loop 2
}
Multiply all of those and you'll get your answer :)
That depends on the loops. For instance:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
sum += i * j * k;
}
}
}
has complexity O(1), because the number of iterations does not depend on the input at all.
Or this:
for (int i = 0; i < n*n*n*n*n*n; i++) {
sum += i;
}
is O(n^6), even though there is a single loop.
What really matters is how many iterations each loop makes.
In your case, it is easy to see that each iteration of the innermost loop is O(1). How many iterations are there? How many times do you need to double a number until you reach n? If x is the number of iterations, we'd exit the loop at the first x such that k = 2^x > n. Can you solve this for x?
Each iteration of the second loop will do this, so the cost of the second loop is the number of iterations (which are easier to count this time) times the cost of the inner loop.
And each iteration of the first loop will do this, so the cost of the first loop is the number of iterations (which is also easy to count) times the cost of the second loop.
Overall, the runtime is the product of 3 numbers. Can you find them?

How do I find the time complexity of this following fragment of program?

For the following program fragment you will (a) write down the total work done by each program statement (beside each statement), (b) compute an expression for the total time complexity, T(n) and derive thhe big Oh complexity, showing all steps to the final answer. I am having a lot of trouble starting off.
for ( i = 0; i < n; i++) {
for ( j = 0; j < 1000; j++) {
a[ i ] = random(n) // random() takes constant time
}
}
int sortedArray [];
for ( i = 0; i < n; i++) {
for ( j = 0; j < i; j++) {
readArray(a) // reads in an array of n values
sortedArray = sort(a) // sort() takes n log n operations
}
}
I also had this problem. On 2nd line I have 'n', 3rd I have n^2, 4th I have n, and on 5th I have n log n. For my time complexity I have O(n^2).

Java Big O notation of 3 nested loops of log(n)

What would the Big O notation be for the following nested loops?
for (int i = n; i > 0; i = i / 2){
for (int j = n; j > 0; j = j / 2){
for (int k = n; k > 0; k = k / 2){
count++;
}
}
}
My thoughts are:
each loop is O(log2(n)) so is it as simple as multiply
O(log2(n)) * O(log2(n)) * O(log2(n)) = O(log2(n)^3)
Yes, that is correct.
One way to figure out the big-O complexity of nested loops whose bounds do not immediately depend on one another is to work from the inside out. The innermost loop does O(log n) work. The second loop runs O(log n) times and does O(log n) work each time, so it does O(log2 n) work. Finally, the outmost loop runs O(log n) times and does O(log2 n) work on each iteration, so the total work done is O(log3 n).
Hope this helps!
Yes you are right.
Easy way to calculate -
for(int i=0; i<n;i++){ // n times
for(int j=0; j<n;j++){ // n times
}
}
This example of simple nested loop. Here Big-O of each loop O(n) and it is nested so typically O(n * n) which is O(n^2) actual Big-O. And in your case -
for (int i = n; i > 0; i = i / 2){ // log(n)
for (int j = n; j > 0; j = j / 2){ // log(n)
for (int k = n; k > 0; k = k / 2){ // log(n)
count++;
}
}
}
Which is in nested loop where each loop Big-O is O(log(n)) so all together complexity would be O(log(n)^3)
Indeed, your assumption is correct. You can show it methodically like the following:

Categories

Resources