14

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.

rubixibuc
  • 7,111
  • 18
  • 59
  • 98
  • 14
    _"complexity of 2^n would be optimal"_ LOL – wilhelmtell Apr 01 '11 at 02:05
  • 2
    I once worked with a system that had logging implemented in a way that made the whole system operate in O(n^n). I was told that it was good enough, that logging could not possibly impact an application "it's just logging" but calculated that to process the data set for the customer I was asked to work on I would need approximately 6.4 billion years on the hardware I had. I just wrote an SQL script generator and got done in a few hours, also got a shit storm for not using the company's official toolset. haaa good'ol memories !! – Newtopian Apr 01 '11 at 02:25
  • Maybe it was n!, I don't remember exactly – Newtopian Apr 01 '11 at 02:29
  • 1
    This is clearly homework; that's OK, but please tag it as such. Thanks. – j_random_hacker Apr 01 '11 at 05:16
  • 1
    @Newtopian Are you sure it wasn't n^2? I can't imagine how you'd log in n^n time. – Nick Johnson Dec 05 '11 at 22:38
  • 1
    @Nick Believe it or not the home brewed logging system kept all previous logging statements in memory and each time one was added it ran through the list and performed the logging thing again for each entry. Ho, I should say it was a re-entrant loop so to log D to a file it logged A (again), then logged A then A and B, then A, AB and ABC, then A, AB, ABC and ABCD and performed all the formatting dance all over again for each one of them. Needless to say the log file grew VERY fast !... hmmm come to think of it your right, it's not n^n but rather (n-1)!... my bad :-) – Newtopian Dec 06 '11 at 05:32
  • @Newtopian I figured it would just be O(n^2), but that's pretty creatively awful. – Nick Johnson Dec 06 '11 at 20:56

5 Answers5

25

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.

user515430
  • 3,341
  • 2
  • 17
  • 13
9

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);
}
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
  • 1
    Isn't it only O(Fib(n)), which is only about 1.6^n? If you replaced `Fib(n - 2)` by `Fib(n - 1)` at the bottom, though, it would become 2^n. – Jeremiah Willcock Apr 01 '11 at 02:09
  • 1
    @Jeremiah, yes, technically this algorithm is theta(Phi^n), which is in O(2^n). (Phi = (5^(1/2) + 1) / 2, about 1.61. – ThomasMcLeod Apr 01 '11 at 02:14
5

Quantum Bogosort has 2^n space complexity.

wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
2

I spent a great deal of time rethinking the problem and would like to post a solution I came up with. All of the answers contributed to my ability to come up with this solution, and am very thankful for everyone that reponded. :-) I realize that algorithm does practically nothing.

it is written in java
can't seem to get the tabs to work
basic operation is i++;

public class TwoToTheN
{  
     private static int twoToTheN = 0;  
     private static int power = 3;

     public static void main(String[] args)   
     {  
         twoToTheN(power);  
         System.out.println(twoToTheN);  
     }  

     private static void twoToTheN(int n)  
     {  
         if(n == 0)   
         {  
             twoToTheN++;  
             return;  
         } 
         else if(n == 1)  
         {  
             twoToTheN++;  
             twoToTheN++;  
             return;  
         }  
         twoToTheN(n-1);  
         twoToTheN(n-1);  
     }
}  
Matt Mills
  • 8,692
  • 6
  • 40
  • 64
rubixibuc
  • 7,111
  • 18
  • 59
  • 98
0

Here's one: output the digits of 2^(2^n).

wnoise
  • 9,764
  • 37
  • 47