-1

The complexity of the below program is given to be O(n!)

double foo(int n){
int i;
double sum;
if(n==0){
    return 1.0; 
}
else {
    sum=0.0;
    for(i=0;i<n;i++){
        sum+=foo(i);
    }
    return sum;
}

}

But I could determine its complexity as O(n!). Can anyone explain how is it coming to be O(n!)?

Is it Θ(n!) also or only O(n!)?

If it is not Θ(n!) .Can I get a sample of Θ(n!) code.

Raphael
  • 72,336
  • 29
  • 179
  • 389
monkey
  • 57
  • 1
  • 7
  • I know technically it can be said to be O(n!), but to me this seems more like $O(2^n)$, just from counting method calls in excel at least. I might be wrong, so I'll attemp a formal proof as an answer. – ZeroUltimax Aug 13 '14 at 13:46
  • yes its growth is definitely less than n! it is definitely not Θ(n!) .It may be O(2^n) .please try if there is any proof for this – monkey Aug 13 '14 at 13:56
  • 2
    Please share your analysis so we can better judge where your problem lies. Also, note that the statements "$f \in O(n!)$" and "$f \in O(2^n)$" are not contradictory since $O(2^n) \subseteq O(n!)$. If you have problems with performing or formulating the analysis, I recommend our reference question. – Raphael Aug 13 '14 at 15:15
  • I just wanted to know Θ ...and got the correct solution..please check below solution – monkey Aug 13 '14 at 15:24
  • 1
    We just want to teach you how to fish, first to sustain yourself and then to teach others, instead of having to feed you forever. – Raphael Aug 14 '14 at 07:58

1 Answers1

3

Let's assume the time to calculate foo(n) is $T(n)$. In the case where $n>0$, we can gather the time taken for an iteration will be: $$ T(n) = \left( \sum_{i=0}^{n-1} T(i)\right) +n+c $$

Also, $T(1)=c$

First, a little justification. The summation in the parenthesis is the run time for all the calls to foo in the loop. The n accounts for the looping and the constant c accounts for things such as initialization, comparisons and such.

Now, with that in mid, let's calculate what the next step would be, and see if we can deduce the growth of the run time.

$$ T(n+1) = \left( \sum_{i=0}^{n} T(i)\right) +(n+1)+c $$

Now, if from the present sum we extract $T(n)$, we get:

$$ T(n+1) = \left( \sum_{i=0}^{n-1} T(i)\right) +T(n)+(n+1)+c $$

Now, if we rearrange the result in the following way, we have an opportunity for reducing the sum:

$$ T(n+1) =\left[ \left( \sum_{i=0}^{n-1} T(i)\right) + n+c \right]+T(n)+1 $$

You see that in the brackets, that's exactly what we defined as $T(n)$ (1rst equation). In that case, we get that:

$$ T(n+1) =T(n)+T(n)+1 = 2T(n)+1 $$

From this, it also stands that $T(n)=2T(n-1)+1$. What happens if we replace $T(n-1)$ by it's value calculated with this very function? Well, let's iterate:

$$ \begin{array}{r l} T(n) & = 2T(n-1)+1 \\ & = 2 \left[ 2 T(n-2) +1 \right] +1 \\ & = 2 \left[ 2 \left[ 2 T(n-3) +1 \right] +1 \right] +1 \\ & \vdots \\ & = 2^kT(n-k)+k \end{array} $$

Almost there. Finally, let's write it in terms of $T(0)$, our known base case. First, for that we must have $T(n-k) = T(0)$ so it stands that $k=n$. In the end, we get:

$$ T(n) = 2^nT(0)+n=2^nc+n $$

With this in mind, we can conclude that $T(n) = \theta(2^n)$

ZeroUltimax
  • 780
  • 4
  • 11
  • With a little refresher from https://www.youtube.com/watch?v=pqivnzmSbq4. I haven't done complexity analysis in a while, thanks for the opportunity to refresh myself. – ZeroUltimax Aug 13 '14 at 14:38
  • Great...I cant believe it.. opened up windows of my brain!!! – monkey Aug 13 '14 at 14:48
  • 1
    Please consider not to encourage undesirable posting behaviour. (Also, please don't use the word "complexity" as a synonym for "runtime"; it confuses people.) – Raphael Aug 13 '14 at 15:18
  • Alright, still new here, so I'll keep that in mind for future questions. – ZeroUltimax Aug 13 '14 at 15:34
  • Okay! As a way of further explanation, everything you do here is covered by our reference questions (this and this). With questions that don't contain much more than a problem statement, it's more efficient to point the users there (resp to what fits) as opposed to doing the same thing over and over again. (Another "danger" is that they don't learn much because they just copy the solution and/or that the site becomes a homework solving platform -- which we don't want.) – Raphael Aug 14 '14 at 08:03