3

If a problem of complexity 2n^2 + n can be solved in 24 units of time for n = 2, how long does it take for n = 4?

I was told that the answer is 48. But I believe it should be 24^2 because the complexity of the algorithm is O(n^2).

Appreciate if anyone could enlighten me.

Paul Brown
  • 39
  • 3

4 Answers4

2

The big O notation is just asymptotic notation. Strictly speaking, f(n)=O(n^2) means, there exists A,B real numbers and n0 integer, such that An^2 <= f(n) <= Bn^2, for n >= n0.

Therefore, first if n < n0 the trend does not even have to follow n^2. Second, for n >= n0 you are only guaranteed that f(n) is bounded (as stated above). If you wanted to approximate, O(n^2) means that for large n you can drop the lower order terms, f(n) --> 2n^2, however, for small n this will introduce significant error.

In your case you have the exact functional form of the performance, f(n) = 2n^2+2, so you should use it!

Assume each step takes time T0 with constant time C, then T(n) = T0*(2n^2+n) + C. If C = 0, then:

  1. Find T0: T(2) = 24 = T0*(2(2)^2+2) => T0 = 2.4 hours
  2. Use T0 for n=4, T(4) = (2.4 hours)*(2(4)^2+4) = 86.4 hours
bcorso
  • 45,608
  • 10
  • 63
  • 75
  • Your first two paragraphs are absolutely correct, and great learning material. However, the paragraph 3 of your answer is an assumption choice made by you. I believe that the question author had in mind much simpler case where 1 step simply takes 1 time unit, and the rest is the constant cost, also based on the neat textbook answer of 50 under this case. Maybe you should chastise the OP for not posing a well-formed question. – Boris Stitnicky Jun 02 '13 at 00:14
  • @BorisStitnicky Yeah, you're right. I've updated to mention that I'm assuming zero constant time. – bcorso Jun 02 '13 at 18:42
2

The complexity O(f(n)) comprises all computations that take c*f(n)+d amount of time, where c and d are constants. If d=0 then:

In the case for n=2 and complexity O(2n^2+2) being 24:

24 = (2*2^2 + 2)*c, hence c = 24/10 = 2.4

Now we compute for n=4:

(2*4^2+4)*2.4= 36*2.4 = 86.4 units of time

If d is not 0 the c = (24-d)/10 and for n=4 it would take

36*(24-d)/10 +d = 86.4 +0.9d

So, it is impossible for the answer to be 48, which additionally implies a linear algorithm

Candide
  • 30,469
  • 8
  • 53
  • 60
1

I certainly don't think it would be 24^2. Since you are talking about O(n^2), and you are given an example which takes 24 units of time for n = 2, it would follow that for n = 4 it would take about 4 times longer to complete (2^2 = 4; 4^2 = 16 - about 4 times).

If you were to compute it differently, plugging in n = 2 into 2*n^2 + n you get 10. If 10 = 24 it means it takes 2.4 units of time for each cycle. Then, plugging in n = 4 you get 2*4^2 + 4 = 36 and multiplying by 2.4 you get 86.4

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • Like the answer by Candide and bcorso, also yours assumes, that the constant term in the running time is 0. – Boris Stitnicky Jun 02 '13 at 00:20
  • @BorisStitnicky - my answer assumes the constant term is 0 because the expression `2n^2+n` is not expressed in terms of Big-O, which in this case would be `n^2`. So I assumed that the `2*n^2+n + 0` is the explicit computational complexity. – Mike Dinescu Jun 02 '13 at 01:55
0

Using Ruby as the math language, the OP's formula is

f = -> x { 2 * x ** 2 + x }

(1) Assuming that one step of algorithm takes 1 unit of time, the answer is 50, because the constant term c is

c = 24 - f.( 2 ) #=> 10
# and
c + f.( 4 ) #=> 50

Under this assumption, the answer is 50.

(2) If, however, we allow one step of the algorithm to take e time units instead of 1 time unit, then the constant c will be:

24 - e * 10

and the running time for n == 4 will be

24 + e * 26        # gives 50 for e == 1

(3) With an additional assumption, that the constant time is zero (that is, the given formula is exact), we have

24 - e * 10 == 0

And the answers by Candide, bcorso and Milky Dinescu apply.

Although the OP question does not literally state that 1 step == 1 time unit, I still feel that this was the author's intention, also based on the typical, easy-to-check textbook result of 50.

Community
  • 1
  • 1
Boris Stitnicky
  • 12,444
  • 5
  • 57
  • 74
  • The constant time cannot be determined with this analysis, it could be 4 or a whole range of values. – Candide Jun 01 '13 at 21:38
  • I have edited the answer to clarify the assumptions. I believe that the fact that I chose assumption (1) is not a valid reason for a downvote by you. – Boris Stitnicky Jun 02 '13 at 00:11
  • I didnt mean to be rude, it just that your assumption is not valid and hence compromises the rest of the argument. In big notation the f(n) is linearly dependent with the actual computation time. You are assuming equality which is a stronger statement than what big o notation means. Ill remove the downvote since it is that important to you, bit do take the time to read about big o notation and you will understand why your premise is not appropriate. – Candide Jun 02 '13 at 06:06
  • @Candide: Ignoring the off-topic remarks, actually, I've refreshed my memory of Big O notation here, and thanks to everyone for that. Regarding the nature of Big O, and the way the OP question is asked, it is not possible to prove that either mine, or yours, or any other assumption about times for finite n is valid. – Boris Stitnicky Jun 02 '13 at 07:07