tags:

views:

131

answers:

3
+2  Q: 

Big-Oh Notation

if T(n) is O(n), then it is also correct to say T(n) is O(n2) ?

+8  A: 

Yes; because O(n) is a subset of O(n^2).

Oli Charlesworth
What exactly do you mean by "subset"?
Lord Torgamus
O(n) is the set of all functions whose growth is bounded asymptotically by `k.n`. Ditto for O(n^2). All functions in O(n) are in O(n^2) (basically by definition), therefore O(n) is a subset of O(n^2).
Oli Charlesworth
@Oli, okay, fair enough. Just a little different terminology than I'm used to.
Lord Torgamus
+2  A: 

if you mean O(2 * N) then yes O(n) == O(2n). The time taken is a linear function of the input data in both cases

I disagree with the other answer that says O(N) = O(N*N). It is true that the O(N) function will finish in less time than O(N*N), but the completion time is not a function of n*n so it really isnt true

I suppose the answer depends on why u r asking the question

pm100
The other answer doesn't say O(N) = O(N^2), but rather has the implied meaning that any function T(N) that is O(N) is also O(N^2) (which is correct according to the formal definition of big-O)
NullUserException
I think you are using the definition of 'theta' in place of the definition of 'big-O'. 'big-O' only provides an upper bound on the asymptotic growth of a function.
Philip Starhill
+3  A: 

Assuming

T(n) = O(n), n > 0

Then both of the following are true

T(n) = O(2n)
T(n) = O(n2)

This is because both 2n and n2 grow as quickly as or more quickly than just plain n. EDIT: As Philip correctly notes in the comments, even a value smaller than 1 can be the multiplier of n, since constant terms may be dropped (they become insignificant for large values of n; EDIT 2: as Oli says, all constants are insignificant per the definition of O). Thus the following is also true:

T(n) = O(0.2n)

In fact, n2 grows so quickly that you can also say

T(n) = o(n2)

But not

T(n) = Θ(n2)

because the functions given provide an asymptotic upper bound, not an asymptotically tight bound.

Lord Torgamus
O(2n) is not valid I thinks.
tster
@tster, why not? Big-Oh is like saying `<=` ( [Cormen](http://programmers.stackexchange.com/questions/870/if-you-could-only-have-one-programming-related-book-on-your-bookshelf-what-would/879#879), p 52); Little-Oh is analogous to `<`.
Lord Torgamus
There is a difference between the terms "equals" and "is an element of" (∈). It is not valid to say T(n) = O(n) or O(<take your pick>). It is valid to say T(n) ∈ O(n) and T(n) ∈ O(n*n). This is because big O notation is in terms of sets.
Mark S
@Mark, that's just another quirk of terminology (again, I cite Cormen).
Lord Torgamus
+1 for thoroughness, but I would rephrase the sentence "This is because..." It is also true that T(n) is in O( 0.5 * n), so the statement that 2n grows more quickly than n is not the right thing to use in this context.
Philip Starhill
@Philip Starhill: Indeed, because the definition of O() includes an arbitrary constant scaling factor.
Oli Charlesworth
@Philip, excellent point, I've edited to reflect this. +1
Lord Torgamus
@Lord Torgamus: Your edit is still slightly incorrect. It's not that the constant factors "become insignificant for large values"; they're insignificant *by the definition of O()*, as it already includes an arbitrary constant scaling factor.
Oli Charlesworth
@Oli, I considered that to be implied, but I will edit again.
Lord Torgamus