0
$\begingroup$

I'm studying for an algorithms exam and came upon the problem :

    for (i = 0; i < n * n; i++) {
        i++;
        for (j = 0; j < (log(n) * log(n) * log(n)); j++) {
            j++;
        }
    }

I figure that line 1 costs $ c_1(n^2) $, line 2 costs $ c_2(n^2 - 1) $, line 3 costs $ \sum_{j=0}^{n^2} \log(j)^3 $, and line 4 costs $\sum_{j=0}^{n^2} \log(j)^3 - 1 $. Am I on the right track? If so, what do the summations get simplified to?

$\endgroup$
  • $\begingroup$ What division by two? $\endgroup$ – David Richerby Oct 15 '16 at 17:06
  • $\begingroup$ I can't tell which track you are on, so I can not tell you if it's the right one. How did you get to these results? $\endgroup$ – Raphael Oct 17 '16 at 8:25
  • $\begingroup$ I think you have some off-by-ones there. $\endgroup$ – Raphael Oct 17 '16 at 8:26
-1
$\begingroup$

Those lines with the increments matters for the final complexity function, but do not take them in count as it will only make things messy. The function can be simplified as follow:

The outer for runs $\frac{n^2}{2}$ times, and for each of it's iterations it runs $\frac{log^3{n}}{2}$ times.

We can call the complexity function $T(n) = \frac{n^2}{2} * log^3{n}$ which leads to $O(n^2 log^3{n})$.

$\endgroup$
  • $\begingroup$ Each iteration runs log^3 (n) / 2 times :-) Same strange code in both loops. $\endgroup$ – gnasher729 Oct 15 '16 at 16:16
  • $\begingroup$ The division by 2 comes from the fact that the counters are being incremented twice, correct? $\endgroup$ – Batman Oct 15 '16 at 19:55
  • $\begingroup$ Exactly. j must become as large as $log^3 n$ but is increased twice per iteration; any decent compiler will combine that into one operation. Having j++ in your loop statement and in the body of the loop is likely to be a bug; it's rarely useful. $\endgroup$ – gnasher729 Oct 16 '16 at 11:11
  • $\begingroup$ The question states nowhere that we are interested only in $O$-bounds. $\endgroup$ – Raphael Oct 17 '16 at 8:24
-1
$\begingroup$

(Original question started with for (i = 0; i = n*n; ...) ) The runtime is zero if n = 0, and the algorithm runs forever otherwise.

I suspect strongly that the algorithm that you wrote in your question is not what you wanted to ask about. aajjbb's answer probably answers what you intended to ask.

(i = n * n in the for loop is most likely not what you wanted. Using i++ and j++ twice is most likely not what you wanted either. If you change i = n * n to i < n * n then a good compiler may notice that the statements inside the inner loop are idempotent and reduce the runtime massively. )

$\endgroup$
  • $\begingroup$ Yes! You are correct - I had intended i = n * n to be i < n * n. $\endgroup$ – Batman Oct 15 '16 at 16:54
  • $\begingroup$ Even with i == n*n, I don't see why it should run forever. For n == 0 it would perform one iteration of the outer loop (and crash because log(0) is not defined), for all other n it would halt immediately (after constant, not zero time). $\endgroup$ – Raphael Oct 17 '16 at 8:23
  • $\begingroup$ No, for n == 0 the assignment i = n x n sets i = 0 and yields 0, so the loop finishes immediately. if n != 0 then i = n x n sets i to a non-zero value and yields a non-zero value; the outer loop never finishes. $\endgroup$ – gnasher729 Oct 17 '16 at 16:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.