1

I am not sure what I should be focused on when determining Big-O of functions. Sometimes it seems that the incrementing variable 'i' is what determines asymptotic complexity other times it seems like it is user defined 'n' that determines asymptotic complexity.

In example1 it seems that since there is 3 n's being multiplied that will produce a O(n^3) effect. is that right? what effect might the i = i + 1 have on the overall function? And thinking that the print statement will just produce a O(n) effect since it's nested in the loop, and all thing nested get O(n) at the very least. So overall I would guess this is O(n^3) but not entirely sure. Mainly because it's divided by 4. Not sure what effect that might have n^3/4 is that still n^3?

example1:

for (i = 1; i < (n * n * 3 * n + 17) / 4; i = i +1) {   
    System.out.println("Hello World); 
}

For the second example, the first loop is normal, I think it's effect is n + 1 but I don't know why it's n + 1, why the + 1? why not just 'n'? All in all this (I think) will produce a O(n) in the first line. Second line is a boolean, and I am not sure how that is treated or what effect that has on the Big-O. Since it's nested in the loop it is by default O(n). In the worst case if 'i' is even the second loop will run, and now this is a nested for loop therefore it will produce a O(n^2) effect. The print statement by default will be O(n^2) too. In the else case we have another for, also being nested which mutates the 'n' by multiplying it, not sure here but if those n's weren't being multiplied the nested loop would just be O(n^2) but here we have n^2 already plus the n^2 from it being nested, so this might be O(n^4)? So maybe in the worst case it will be O(n^4)? Not sure.

example2:

for (i = 0; i < n; i++) {
  if (i % 2 == 0) {
    for (j = 0; j < n; j++) {
      System.out.println("Hey");
} else {
  for (j = 0; j < n * n; j++) {
    System.out.prtinln("Now");
}

For the last example here there is n being multipled by 10000, so we immediately have an O(n) effect here. Also not sure why this is but I read that i will also effect the loop, and since "i" is i = i * 2 that will make it log_2(n) I have no idea why this is the case though. How could i = i * 2 produce log_2(n). If this is true I am thinking that the loop will maybe be nlog_2(n) because the n is being multipled by 10000, hence O(n) and then i = i * 2 produces log_2(n) so multiplied together they are nlog(n). The x = x + i; I think this is just O(n) but overall won't make much difference.

example3:

for (i = 1; i <= 10000 * n; i = i *2) {
    x = x + i; 
}

Please help I am very lost on all this.

Thank you

yre
  • 285
  • 4
  • 12
  • The middle example has invalid syntax (check your brackets), so actually I'm not sure what complexity it has. It depends on what was meant. – 4castle Mar 14 '17 at 23:50
  • 2
    Possible duplicate of [Big O, how do you calculate/approximate it?](http://stackoverflow.com/questions/3255/big-o-how-do-you-calculate-approximate-it) – 4castle Mar 15 '17 at 00:05
  • you said O(n^4), O(n^2), and O(log(n)). Could you explain this some more. – yre Mar 15 '17 at 00:08
  • The first is O(n^3), because that's the rate at which a change in `n` effects a change in the number of operations. I'm not sure if the middle is O(n^2) or O(n^3), because it's unclear if the last `for` loop is nested or not. The third one takes a bit more math to explain, so perhaps the easiest thing for you would be to trace the number of iterations for various inputs of `n`, but the complexity is O(log(n)). – 4castle Mar 15 '17 at 00:14
  • Dividing n^3 by 4 is 1/4 * n^3 or (n^3) / 4. You can't take the 4 up to the exponent. And constant factors don't matter in Big-O notation. Both 1/4 * n^3 and n^3 are in O(n^3). – mike Mar 15 '17 at 00:17

1 Answers1

0
  • Example 1:

enter image description here

  • Example 2:

enter image description here

  • Example 3:

enter image description here

For more, please consult the last couple of slides from here:

http://faculty.kfupm.edu.sa/ics/jauhar/ics202/Unit03_ComplexityAnalysis1.ppt