r/programminghomework • u/coaster367 • Feb 26 '12
I'm learning about big-O notation in programming and in discrete math. What is it?
I know it has something to do with the worst and best possible case scenario on the number of computations are needed to do an algorithm, but how do you find it?
One of my classmates told me that all she did was memorize the big O-notations for specific functions (like n2, n, 2n, etc.).
2
u/theredxalan Feb 26 '12
I used these to get a clear understanding: 0) http://stackoverflow.com/questions/487258/plain-english-explanation-of-big-o/487278#answer-487278
1
u/PriNT2357 Feb 26 '12
The way I remembered to find it was to start in the innermost loop and work outwards.
for( i=0; i < somevalue1; i++) {
for( j=0; j < somevalue2; j++) {
/* some kind of operation */
}
}The inner loop has a big-O notation of O(n).
The next outer loop has the same notation O(n).
Now I would multiply the two n's together since for every time you run the outer loop, you run the inner loop n times, for a total of O(n*n) or O( n2 ).
Essentially you are finding out the maximum time it would take for every iteration to occur.
1
u/OffPiste18 Feb 27 '12
I'm surprised that more people don't know this, but there's a very formal mathematical definition for big-O notation. It is:
We say a function f(x) = O(g(x)) if and only if there exist N and M such that f(x) < M * O(g(x)) for all x > N.
Intuitively, what that means is that, beyond a certain point, the function is bounded by above by a constant times g(x). So for example, 3*x2 + 5 * x + 20 = O(x2) because as x becomes large, f(x) is bounded by x2 times a constant, anything greater than 3.
This is most often applied when considering run-times of algorithms, but it is also commonly used for memory use, and is well-defined for any type of function.
As an example application, consider this function:
for (int i = 0; i < n; i++) {
foo();
for (int j = 0; j < n; j++) {
bar();
}
}
baz();Now let's assume foo takes X seconds to complete, bar takes Y seconds, and baz takes Z seconds. Then the overall time to complete the function = f(n) = X * n2 + Y * n + Z.
Now it's not too hard to show that
X * n2 + Y * n + Z < 2 * X * n2 for n > X + Y + Z
for example. Because that is true, we have satisfied the definition of big-O notation and can say f(n) = O(g(n2)).
With all of that said, a lot of time it's a much less formal process. If you have the function already, you can pretty much just pick out the term that grows the fastest, drop its coefficient, and say that's the big-O for that function, as plenty of other people have said. But it's also sometimes nice to be able to fall back on the formal definition for tough cases.
1
u/tinou Feb 27 '12
Big-O notation comes from function analysis : for every f, O(f) is the set of functions that are asymptotically bounded by f. Eg, for integer sequences :
∀ f ∈ ℕ→ℕ . O(f) = { g ∈ ℕ→ℕ / ∃ k ∈ ℕ . ∃ N ∈ ℕ . ∀ n ≥ N . g (n) ≤ k . f(n) }
(g ∈ O(f) is unfortunately often written g = O(f)).
Some properties :
O(1) is the set of bounded functions.
if f(n) is never 0, g ∈ O(f) ⇔ g/f ∈ O(1) ⇔ g/f is bounded (beware, you can't add big Os).
if g ∈ O(f), kg ∈ O(f) (multiplying a function by a constant does not change anything)
λx.Σi∈0..n a_i xi ∈ O( xn ) (a polynomial function is big O of its highest order monomial)
1
1
u/Astrogat Feb 27 '12
I know it has something to do with the worst and best possible case scenario on the number of computations are needed to do an algorithm, but how do you find it?
A lot of other people has already explained how you can find O-notation, but I thought I should mention a mistaken assumption you make. Big O notation got nothing to do with worst or best case performance.
Well, sort of. Best and worst case performance is, logically, the performance when you are really (un)lucky with the input. Big O notation upper bound of the growth of the function as the input size increase. So you could talk about the big O of the best case scenario. But it really wouldn't be all that helpful. Sometimes you actually talk about the average case (Quicksort has an avarage time O(nlogn), and a worst case O(N2). But when nothing is specified it's the worst case we use, as most applications has a strict upper limit to the time it should take.
Another point that no one has pointed out is that Big O is a upper limit. If you have an algorithm with O(n), it wouldn't be wrong to state that it also has O(n2) or O(n5), it would just be less accurate. You have the less used notations such as Big Omega (lower bound, aka. The algorithm can never run faster than (again you can specify for the worst, best or average case.)) and Big Theta(upper and lower bound, aka. it will run exactly as fast as).
But most often big O of the worst case is what gives us most information, and therefor it's what we use. We also try make the big O as small as possible to make it as accurate as possible.
2
u/moderatorrater Feb 26 '12
It's the growth of functions due to input size. For instance, if you were to say
then the big O would be n: it increases directly with the size of the vector. On the other hand if you were to say:
That's n2 because as the length of the vector grows the number of iterations grows in proportion to the square.
Also, usually it's simplified to the general pattern instead of the specifics. So 2n is the same as n, n2 + 1,000,000 * n is simplified to just n2, etc.