-1

The Fizz-Buzz function (in pseudocode) takes any positive integer n. I'm especially curious about the algebraic breakdown of the cost and time required of the if-else statement. I know its worst case running time is O(n).

Fizz-Bizz(n)
  for i = 1 to n
    if (n % 3 == 0)
      print "fizz"
    if (n % 5 == 0)
      print "buzz"
    if (n % 3 != 0 and n % 5 != 0)
      print n

Example of breakdown of another algorithm: enter image description here

MNRC
  • 263
  • 1
  • 11
  • Running time depends on a lot of things. Time complexity is O(n). The performance of `if` and `else` is irrelevant to the complexity so long as they’re constant-time compared to the input (which they are). – Ry- Jun 26 '14 at 06:22

1 Answers1

2

The time complexity is O(n) because the if statement has no real effect on that. The complexity of the if statement is constant over a large enough dataset.

The if statement may actually do a different amount of work in iterations where you have a multiple of three or five but, the amount of extra work per loop iteration is not dependent on n. In fact, it averages out to a constant as n becomes bigger.

And, as an aside, I think that code may be wrong. At multiples of fifteen, it should print both fizz and buzz.

If you want to do it to the level in your edit (the added breakdown), you simply need to assign an arbitrary cost ci to each statement (and this cost is constant for a single execution of that statement) then figure out how many times each statement is run.

For example, the first if sequence runs n times, the print "fizz" runs for one-third of those, n/3. So you end up with something like this table:

                           cost   times
Fizz-Buzz(n)
  for i = 1 to n           c1     n
    if (n % 3 == 0)        c2     n
      print "fizz"         c3     n / 3       [call this a]
    else
      if (n % 5 == 0)      c4     n - a
        print "buzz"       c5     (n - a) / 5 [call this b]
      else
        print n            c6     n - a - b

Add up all of those as per your example (substituting the n-equations for a and b) and, in the end, you'll still end up with something dependent on n, hence an O(n) algorithm. It'll look something like:

  c1*n + c2*n + c3*n/3   + c4*(n-a)   + c5*(n-a)/5     + c6*(n-a-b)
= c1*n + c2*n + (c3/3)*n + c4*(n-n/3) + (c5/5)*(n-n/3) + c6*(n-n/3-(n-n/3)/5)
= c1*n + c2*n + (c3/3)*n + c4*(2/3)*n + (c5/5)*(2/3)*n + c6*(n-n/3-(n-n/3)/5)
= c1*n + c2*n + (c3/3)*n + (c4*2/3)*n + (c5*2/15)*n    + c6*(n*8/15)
= c1*n + c2*n + (c3/3)*n + (c4*2/3)*n + (c5*2/15)*n    + (c6*8/15)*n

   /             1         2             2                8    \
= ( c1  + c2   + - c3    + - c4       + -- c5          + -- c6  ) * n
   \             3         3            15               15    /

All those values inside parentheses are in fact constants (since they're multiples of constants) so the whole thing is a constant multiplier of n.

Now, if you find a minor mistake in those equations above, I wouldn't be too surprised - I haven't done this level of math for quite a few years, and I may well have thrown in a furphy in case this is for homework and you try copying it verbatim :-)

But the only mistake you're likely to find is the value of the constant multiplier itself. It will still be a constant multiplier of some description, I'll guarantee that.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Can you break down the cost for the if-else statement? Like put it in algebraic terms? – MNRC Jun 26 '14 at 06:22
  • @MNRC, in complexity analysis, it's irrelevant. The cost of that loop body for a single iteration is effectively constant and has no bearing on the complexity. – paxdiablo Jun 26 '14 at 06:31
  • @paxdiablo I just added a photo of the algebraic breakdown of Insertion sort. – MNRC Jun 26 '14 at 06:32
  • 1
    Yes, I can see that and, if you want that "bizarro homework" level of detail, you can certainly work it out. But you'll end up at the same O(n) figure I gave you :-) – paxdiablo Jun 26 '14 at 06:35
  • @paxdiablo Great breakdown! Thanks!! The part that had me stumped was the if-else statement because my textbook didn't include how that would be calculated. But I see now that no matter if is true or not the statement will always run *n* times. – MNRC Jun 28 '14 at 04:44