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

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:

Related

What would be the time complexity for using for and while loops together in a code?

I know using two for loops sums up to O(N^2). Is it same in case of for loop and while loop?
Here is a code snippet
for(int num : nums)
{
if(!set.contains(num-1))
{
int currNum = num;
int currStreak = 1;
while(set.contains(currNum+1))
{
currNum += 1;
currStreak += 1;
}
longestStreak = Math.max(longestStreak, currStreak);
}
}
As deHaar wrote. The complexity of two for loops aren't always O(n^2).
for example, for the following code:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n * n; j++) {
//do something
}
}
the complexity is O(n^3) because the code execute the 'do something' n^3 times.
note that while and for are technically the same.
the code:
for (int i = 0; i < 100; i++) {
//do something
}
could be translated to:
int i = 0;
while(i < 100) {
//do something
i++;
}
So in your example:
let say that m is the longest streak. now the code inside the while loop is running at most m times. now let say n is the amount of numbers. so the code in the for loop is running n times.
in the code inside the while runs m times every time the for lop is running so m*n times in total. so the complexity is O(n*m)
if the content of set is nums then the longest possible streak will be with length n. and then you can say the complexity is O(n^2).

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?

Big O analysis for this for loop

sum = 0;
for (i = 1; i <= n; i++) { //#1
for (j = 1; j <= i * i; j++) { //#2
if (j % i == 0) { //#3
for (k = 1; k <= j; k++) { //#4
sum++;
}
}
}
}
The above got me confusing
Suppose #1 runs for N times
#2 runs for N^2 times
#3 runs for N/c since for N inputs N/c could be true conditions
#4 runs for N times
Therefore roughly I could be looking at O(N^5) . I am not sure. Please help clarify.
EDIT I was wondering the runtime at the if(j%i==0). Since it takes N^2 inputs from its parent loop it could be doing (N^2)/c executions instead of N/c
I would say its O(N^4) as its the same as.
for (int i = 1; i <= n; i++) //#1 O(n ...
for (int j = i; j <= i * i; j+=i) //#2 ... * n ...
for (int k = 1; k <= j; k++) //#4 ... * n^2) as j ~= i^2
sum++;
or
public static void main(String... args) {
int n = 9000;
System.out.println((double) f(n * 10) / f(n));
}
private static long f(long n) {
long sum = 0;
for (long i = 1; i <= n; i++) //#1
for (long j = 1; j <= i; j++) //#2
sum += i * j; // # 4
return sum;
}
prints
9996.667534360826
which is pretty close to 10^4
#PeterLawrey did the math, here are benchmarks plotted on chart (my data set - n vs. execution time in microseconds).
Basically I run the code in question several times with different n input (X-axis). Then I divided average execution time by n^5, n^4 and n^3 functions and plotted that:
Full size image
Note that this is a logarithmic scale and that all functions were scaled to more-or-less be in the same range.
Guess what, avergae execution time t(n) divided by n^5 keeps getting decreasing, while t(n)/n^3 keeps growing. Only t(n)/n^4 is stabilizes when approaching infinity which proves that the average execution time is in fact O(n^4).
I think the answer, using Sigma notation, would be like the following:

Categories

Resources