Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The complexity of n choose 2 is in Theta (n^2)?

I'm reading Introduction to Algorithms 3rd Edition (Cormen and Rivest) and on page 69 in the "A brute-force solution" they state that n choose 2 = Theta (n^2). I would think it would be in Theta (n!) instead. Why is n choose 2 tightly bound to n squared? Thanks!

like image 394
Jenny Shoars Avatar asked Oct 16 '13 23:10

Jenny Shoars


People also ask

Is n choose 2 quadratic?

The function itself grows quadratically. Computing it actually only takes one multiplication operation, one subtraction, and one division by 2, so computing f(n) can be done in O(1) (assuming constant-time arithmetic operations).

Is O n 2 the same as O n 3?

Since n2 = O(n3), then any f(n) = O(n2) is also O(n3). Definition 9.2 (Big-Omega: Lower Bound) f(n) = Ω(g(n)) means there exists some constant c such that f(n) ≥ c · g(n), for large enough n (that is, as n → ∞).

What is the complexity of n choose k?

Thus, the complexity is O(n choose k) .


1 Answers

n choose 2 is

n(n - 1) / 2

This is

n2 / 2 - n/2

We can see that n(n-1)/2 = Θ(n2) by taking the limit of their ratios as n goes to infinity:

limn → ∞ (n2 / 2 - n / 2) / n2 = 1/2

Since this comes out to a finite, nonzero quantity, we have n(n-1)/2 = Θ(n2).

More generally: n choose k for any fixed constant k is Θ(nk), because it's equal to

n! / (k!(n - k)!) = n(n-1)(n-2)...(n-k+1) / k!

Which is a kth-degree polynomial in n with a nonzero leading coefficient.

Hope this helps!

like image 86
templatetypedef Avatar answered Sep 21 '22 16:09

templatetypedef