Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the dominant term in 2^n or n^2 for big O notation

Tags:

big-o

I have been looking at Big O notation and have come across an operational count 2^n+n^2. I understand the practice of big O notation is to remove the constants and the low order terms, however I cant figure out which one to make O(n). I think it may be 2^n but have had no luck finding anything to suggest this.

like image 669
JOsh Avatar asked Jan 08 '16 23:01

JOsh


People also ask

What is bigger 2 n or n 2?

2n2 grows faster than 2n. (Take antilog of both sides.)

What is n in Big O of n?

When analyzing the Big-O performance of sorting algorithms, n typically represents the number of elements that you're sorting. So, for example, if you're sorting n items with Bubble Sort, the runtime performance in the worst case will be on the order of O(n2) operations.

Which of these is the correct Big O expression for 1 2 3 n?

The answer is O(1) if you use the formula for summation directly. Show activity on this post. the addition is called n times, so the whole code has a time complexity of O(1) * n = O(n). If there is nothing missing in your question, O(n) will be the correct answer to the task.

What is n in O n?

O(n) is Big O Notation and refers to the complexity of a given algorithm. n refers to the size of the input, in your case it's the number of items in your list. O(n) means that your algorithm will take on the order of n operations to insert an item.


2 Answers

Look at the growth factor over time. For the first eight values of n, O(n^2) works out to:

0, 1, 4, 9, 16, 25, 36, 49...

O(2^n) produces powers of two:

1, 2, 4, 8, 16, 32, 64, 128...

It should be fairly obvious which one is growing faster.

Note that the general rule applies even with different bases and exponents. O(1.1^n) may have initially lower work than O(n^10) for smaller n, but all exponential growth with an exponent greater than 1 will eventually outpace fixed exponent polynomial growth as n approaches infinity.

like image 124
ShadowRanger Avatar answered Nov 23 '22 18:11

ShadowRanger


By L'Hopital's rule:

lim_{n -> infinity} (n^2 / 2^n )

= 1/log(2) lim_{n -> infinity} (2n / 2^n)

= 1/log(2)^2 lim_{n -> infinity} (2 / 2^n)

= 0

We have n^2 = o(2^n) which implies n^2 = O(2^n).

If this proof doesn't make sense: By definition, f(n) = O(g(n) if and only if f(n) is bounded within some constant multiple of g(n) after n grows past some constant. And a way to think of f(n) = o(g(n)) is that as n grows to infinity, g(n) will continue to outgrow f(n) faster. In other words:

  1. f(n) = o(g(n)) if and only if the limit f(n)/g(n) becomes zero as n goes to infinity.

  2. o(g(n) is a strictly stronger condition that f(n) = O(g(n)).

Alternatively, you just need to use the definition directly: Find some a and b such that n^2 <= a | 2^n | for all n >= b, which is simple algebraic manipulation.

like image 25
Katie Avatar answered Nov 23 '22 19:11

Katie