user12150264
user12150264

Reputation:

Why does this algorithm have a O(n^2) time instead of O(n^3)?

I've been studying Big-O for an upcoming interview and I wanted something explained.

So one way I've been determining the run-time of algorithms is by counting the number of for-loops. For example, if an algorithm uses 1 for-loop I'd count that as O(n) and if it used two for-loops I'd say it was O(n^2). I find that this is generally an easy way to figure out time for simple algorithms. Although in a textbook I am reading they said this algorithm has an O(n^2) run-time.

 a=5
b=6
c=10
for i in range(n):
   for j in range(n):
      x = i * i
      y = j * j
      z = i * j
for k in range(n):
   w = a*k + 45
   v = b*b
d = 33

How would this have an O(n^2) run-time? If 𝑇(𝑛)=3+3𝑛2+2𝑛+1=3𝑛2+2𝑛+4 then are we just discarding the last loop (2n) because it does not add to 3n^2?

Thanks for reading!

Upvotes: 0

Views: 312

Answers (1)

gct
gct

Reputation: 14563

Let's look at what the curves look like:

enter image description here

You can see here what a curve for just N looks like, along with the curve for N^2 and their sum. Notice anything? The N^2+N curve looks a lot more like an N^2 curve than an N curve, adding the N didn't really change it a whole lot. In fact, if we scale the sum curve by a constant (.93 in this case), we can make them look almost identical:

enter image description here

If we're analyzing algorithms, at least theoretically, we're generally worried about the factors that will dominate our processing time. If adding a factor of N to the runtime doesn't change much, why bother thinking about it? We should worry about the quadratic behavior. This isn't necessarily true in real life, where we might really care if something is a constant 2x factor, but at least for theoretical analysis it is.

This is why big-O notation is an asymptotic measure of performance. We only consider what happens as N gets very large. If we think about the ratio N^2/N as N -> infinity, it's obvious that this will trend to zero. So, the larger n gets, the less important the linear factor is, and thus we discard it.

Upvotes: 3

Related Questions