• edited by
28,552 views

7 Answers

Best answer
69 69 votes

Answer is (D) $O(2^n)$

int recursive (int n) {
    if(n == 1)      // takes constant time say 'A' time
        return (1); // takes constant time say 'A' time
    else
        // takes T(n-1) + T(n-1) time
        return (recursive (n-1) + recursive (n-1));
}

 

$T(n) = 2T(n - 1) + a$ is the recurrence equation found from the pseudo code. Note: $a$ is a constant $O(1)$ cost that the non-recursive part of the function takes.

Solving the recurrence by Back Substitution:

$$\begin{align*}
T(n) &=  2T(n - 1) + a \\[1em]
T(n - 1) &= 2T(n - 2) + a \\[1em]
T(n - 2) &= 2T(n - 3) + a \\[1em]
&\vdots
\end{align*}$$

Thus, we can re-write the equation for $T(n)$ as follows

 $$\begin{align*}
T(n)
&= 2 \left [ 2T(n - 2) + a \right ] + a &= 4T(n - 2) + 2a + a \\[1em]
&= 4 \left [ 2T(n - 3) + a \right ] + 3a &= 8T(n - 3) + 4a + 2a + a \\[1em]
&\vdots \\[1em]
&= 2^k T(n - k) + (2^k - 1) a
\end{align*}$$

On Substituting Limiting Condition

$$T(1) = 1 \\
\implies n - k = 1 \\
\implies k = n - 1
$$

Therefore, our solution becomes

$$2^{n - 1} + \left ( 2^{n - 1} - 1 \right ) a \\ = O(2^n)$$

• edited by
33 33 votes
Its similar to tower of hanoi problem

$$T(n)=2T(n-1) + 1$$
$T(1)=1$
$T(2)=2.1 + 1 =3$
$T(3)=2.3 +1 =7$
$T(4)=2.7 +1 =15 .... .....$
$T(n)=2.T(n-1)+ 1$
we can see that its a pattern getting formed which is $T(n)=2^n-1$ so, it is $O(2^n)$

Answer : $D.$ $O(2^n)$
• edited by
16 16 votes

The recurrence relation from the code is :

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

The above recurrence can be solved easily with the help of Subtract and conquer master's theorem.

Here $a=2, b=1 , d=0$

Since $a>1,$ the  $3^{rd}$ case applies

$O(n^0 . 2^{n/1}) = O(2^n)$ 

Answer : $D. O(2^n)$ 

Reference : https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwiapt6kxLXTAhVCOo8KHVOMCLYQFggiMAA&url=https%3A%2F%2Fwww.eecis.udel.edu%2F~saunders%2Fnotes%2Frecurrence-relations.pdf&usg=AFQjCNEDfKzz_SaGkG9uYob8-Ut4qV7jww&sig2=MZlwUU2OTfrZF0p_6ddVbA

• edited by
5 5 votes

Another way to visualize this problem.

3 3 votes

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

$a_{n}=2a_{n-1}+1$

First solve this

$a_{n}=2a_{n-1}$

$r^n=2r^{n-1}$

$\dfrac{r^n}{r^{n-1}}=2$

$r=2$

$a_{n}^{(h)}=d(2)^n...........(1)$

Now solve this

$a_{n}^{(p)}=p_{0}$

$a_{n}=2a_{n-1}+1$

$a_{n}-2a_{n-1}=1$

$p_{0}-2(p_{0})=1$

$p_{0}=-1$

$a_{n}^{(p)}=-1...........(2)$

$Add\ (1)+(2)$

$a_{n}=d(2^n)-1............(3)$

$Given\ a_{1}=1$

$Substitute\ in\ (3)$

$d=1$

$a_{n}=1.(2^n)-1$


$T(n)=O(2^n)$

Answer:
Position:
Show:

Related questions

67 67 votes
7 answers 7 answers
31.2k
31.2k views
Kathleen asked Sep 18, 2014
31,156 views
The recurrence equation$ T(1) = 1$$T(n) = 2T(n-1) + n, n \geq 2$evaluates to$2^{n+1} - n - 2$$2^n - n$$2^{n+1} - 2n - 2$$2^n + n $
85 85 votes
13 answers 13 answers
33.5k
33.5k views
Kathleen asked Sep 18, 2014
33,517 views
Let $A[1,\ldots,n]$ be an array storing a bit ($1$ or $0$) at each location, and $f(m)$ is a function whose time complexity is $\Theta(m)$. Consider the following program...
74 74 votes
5 answers 5 answers
27.2k
27.2k views
Anu asked Jun 1, 2015
27,221 views
A hash table with ten buckets with one slot per bucket is shown in the following figure. The symbols $S1$ to $S7$ initially entered using a hashing function with linear p...
67 67 votes
3 answers 3 answers
33.9k
33.9k views
Kathleen asked Sep 23, 2014
33,872 views
If one uses straight two-way merge sort algorithm to sort the following elements in ascending order: $20, \ 47, \ 15, \ 8, \ 9, \ 4, \ 40, \ 30, \ 12, \ 17$then the o...