1
$\begingroup$

Currently while I was coding, I got a doubt.

While I was solving a particular type of problem I found it to be solved in $O(n^3)$.

I have broken that problem and solved it in $O(n^2)$.

But to solve it I have broken that $O(n^3)$ to $3$ * $O(n^2)$

Is it a better solution than $O(n^3)$ or will it matter the number of $O(n^2)$ also?

$\endgroup$

migrated from cseducators.stackexchange.com Jan 15 '18 at 3:07

This question came from our site for those involved in the field of teaching Computer Science.

  • 1
    $\begingroup$ O(3n²) = O(n²), because 3n² ≈ n² $\endgroup$ – ctrl-alt-delor Jan 13 '18 at 11:17
  • 3
    $\begingroup$ Possible duplicate of How does one know which notation of time complexity analysis to use? $\endgroup$ – Discrete lizard Jan 15 '18 at 8:34
  • 2
    $\begingroup$ I don't understand what you mean by "broken". $\endgroup$ – David Richerby Jan 15 '18 at 13:53
  • $\begingroup$ I don't understand qhat the question here is, mostly because I don't understand what you're doing. What does "broken" mean here? Where does the 3 in 3*O(n²) come from? What do you mean by "will it matter the number of"? $\endgroup$ – Raphael Jan 17 '18 at 18:11
  • $\begingroup$ Note that O(n²) and O(n³) are not mutually exclusive. $\endgroup$ – Raphael Jan 17 '18 at 18:11
4
$\begingroup$

Multiplying $O\left(n^2\right)$ by $3$ doesn't change it. Literally, it remains the same thing:

$$3{\cdot}O\left(n^2\right)~=~O\left(3n^2\right)~=~O\left(n^2\right).$$

This is because such complexity qualifications don't track scalar multipliers.

The reason that we discard scalar multipliers is pretty simple: the actual run time will depend on how fast particular computer instructions work, such that if we cared about scalar factors, our complexity qualifications would become extremely context-specific.

For example, many real-world systems will speed up as they run a long-running algorithm due to optimizations that kick in as an algorithm runs (e.g., branch profiling CPU's and hot-spot code optimizations in VM's). So, any scalar projection of run-time behavior would have to account for these factors. Such an analysis could still be quite helpful and worth studying when appropriate, but that's not the role that complexity classes intend to fill.

So, as general performance analyses qualifications, complexity classes tend to focus on the largest order-affecting factors while hand-waving the scalar stuff as implementation-dependent.

$\endgroup$
3
$\begingroup$

It is impossible to say which is better with only the given information. For large values of n, $O(n^3)$ will be slower. But there are situations in which the algorithm in question will only be executed with such small values of n that the one with the highest order might work better.

I actually implemented an exponential algorithm once as part of an important program since the expected size of the data set was too small to matter.

So, what is slower in general, may be faster in practice. But if you are programming the general case, use the quadratic algorithm, not the cubic one.

If you graph the two polynomials on the same axis this will be very clear.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy