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.
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.
n 2^n grows asymptotically faster than 2^n.
Here, it's O(2^n) , or exponential, since each function call calls itself twice unless it has been recursed n times.
These two functions are related as 2^n = O(3^n) .
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With