tags:

views:

92

answers:

5

For some reason im unable to solve this. what will be the Big-o Notation

for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
        c[i][j] = 0;
        for (int k = 0; k < n; k++)
            c[i][j] += a[i][k] * b[k][j];
    }
+1  A: 
for (int i = 0; i < n; i++)
 for (int j = 0; j < n; j++) {
  c[i][j] = 0;
  for (int k = 0; k < n; k++) c[i][j] += a[i][k] * b[k][j];
 }

It looks like it's O(n^3) because it has 3-level loops.

SHiNKiROU
3 levels of loops. Each go to N. Each goes up by 1. Pretty clear o(n^3) to me, but it's been too long since college....
bwawok
+2  A: 

The answer is: O(n^3)

Because the outer most loop executes N times.
For each element in that loop the middle loop executes N times.
For each element in the middle loop, the inner most loop executes N times.

Total looping is N*N*N = N^3

This statement executes N^2 times c[i][j] = 0; but it is irrelevant compared to the inner most statement which executes N^3 times.

Brian R. Bondy
A: 

The way to work this out is to calculate the number of operations in terms of n, and then discard less-significant terms (if there are any)...

Oli Charlesworth
+3  A: 

Steven, if we simply tell you what the complexity is, you're not going to learn how to compute the complexity. Please suggest what you think the complexity is and why. Then we can better help you.

levity37
+3  A: 
Jon Purdy