Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2^n complexity algorithm

I need to implement and test an algorithm with a 2^n complexity. I have been trying to find one for a while. If there is any way I can acheive this by implementation -- with a exact complexity of 2^n that would be optimal. If anyone knows of a location I can find an example, or could help me implement one, that would be awesome :-). The basic operation can be anything, but a single statment like i++; would be best.

like image 452
rubixibuc Avatar asked Apr 01 '11 02:04

rubixibuc


People also ask

Which algorithm has 2 n complexity?

Exponential Time — O(2^n) An algorithm is said to have an exponential time complexity when the growth doubles with each addition to the input data set. This kind of time complexity is usually seen in brute-force algorithms.

Which time complexity is the fastest n 2 or 2 n?

n 2^n grows asymptotically faster than 2^n.

Why is recursion O 2 n?

Here, it's O(2^n) , or exponential, since each function call calls itself twice unless it has been recursed n times.

Is 2 n and 3 n Big-O the same?

These two functions are related as 2^n = O(3^n) .


2 Answers

Generate all subsets of a set with n elements.

Added. The simplest way of generating all the subsets of S = {a0, a1, ..., an-1} is probably to translate between the binary representation of the rank and the subset.

Take S = {a0, a1, a2}.

rank binary subset
0    000    {} 
1    001    {a0}
2    010    {a1}
3    011    {a0, a1}
4    100    {a2}
5    101    {a0, a2}
6    110    {a1, a2}
7    111    {a0, a1, a2}

So, a 1 in the binary means the corresponding element is in the subset. A 0 means the element is not in the subset.

But you should also lookup Gray code.

like image 175
user515430 Avatar answered Sep 20 '22 14:09

user515430


The classical recursive Fibonacci number calculation is O(2^n).

unsigned Fib(unsigned n)
{
    if (n <= 1)
        return n;
    else
        return Fib(n - 1) + Fib(n - 2);
}

Since the above is actually theta(Phi^n), I'm adding a theta(2^n) algorithm that return 2^n. Thanks to Jeremiah Willcock.

unsigned TwoExp(unsigned n)
{
    if (n == 0)
        return 1;
    else
        return TwoExp(n - 1) + TwoExp(n - 1);
}
like image 37
ThomasMcLeod Avatar answered Sep 20 '22 14:09

ThomasMcLeod