Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is O(n) equal to O(2n)

I understand that O(N) is essentially equal to O(cN) where c='some constant'. But if N = c. Doesn't that make it O(N)^2. Does this hold as c increases, or is there some formal limit.

like image 303
rsavchenko Avatar asked Oct 15 '13 00:10

rsavchenko


People also ask

Is 22n O 2n )? Justify?

2n+1 =22n ≤ c2n where c ≥ 2. Is 22n = O(2n) ? No. 22n = 2n · 2n.

Is O 2 n the same as O 3 n?

These two functions are related as 2^n = O(3^n) . or more appropriately , we can say 2^n = o(3^n) .

Is O 2n still O n?

In proper Big O Notation, O(2n) is written as O(n). In fact, O(2n) and O(3n) can both be simplified to O(n).

What is difference between O n and O n2?

Big O notation is about the order of growth. If you run the algorithm with 10 elements, it will take some time. When double the number, how much longer will it take? Double the time means it is O(n), Quadruple the time means it is O(n^2).


2 Answers

If N = c then c is not constant. Therefore this is never the case.

like image 182
orlp Avatar answered Nov 08 '22 20:11

orlp


O(n) means that the runtime of the algorithm increases linearly with the size of the input. If you double the size of the input, you double the runtime. If you triple the size of the input, you triple the runtime. And so on. So the graph is a straight line.

O(n^2) means that the runtime of the algorithm increases quadratically with the size of the input. If you double the size of the input, you quadruple the runtime. This is bad. The graph kind of loops up and gets really high really fast.

With O(2n), you have increased the slope of the line, but it is still a line. Since it's linear, it "reduces" to O(n).

Hope that helps.

like image 32
Vidya Avatar answered Nov 08 '22 22:11

Vidya