Skip to main content
added 29 characters in body
Source Link
Rohit Singh
  • 261
  • 3
  • 13

If someone is not aware of method to solve this recurrence, then We can solve this by Substitution

Given,

$$T(n)=2T(\frac{n}{2})+1$$

Now, going by relation,

$$T(\frac{n}{2})=2T(\frac{n}{4})+1$$

We will Substitute this in Original Recurrence Relation

Therefore,

$$T(n)=2T(\frac{n}{2})+1$$
$$T(n)=2 \Biggl(2T(\frac{n}{4})+1\Biggr) + 1$$
$$T(n)=2 \Biggl(2T(\frac{n}{2^2})+1\Biggr) + 1$$
$$T(n)=2 \Biggl(2 \Biggl( 2T(\frac{n}{8})+1\Biggr) +1 \Biggr) + 1$$
$$T(n)=2 \Biggl(2 \Biggl( 2T(\frac{n}{2^3})+1\Biggr) +1 \Biggr) + 1$$

Opening Brackets Carefully and Rearranging for Observing Pattern

$$T(n)=2^3T(\frac{n}{2^3})+ 2^2 + 2^1 + 1 $$
$$T(n)=2^3T(\frac{n}{2^3})+ 2^2 + 2^1 + 2^0 $$

Using Summation formula for Geometric Progression

$$T(n)=2^3T(\frac{n}{2^3})+ 2^0(\frac{2^3-1}{2-1})$$
$$T(n)=2^3T(\frac{n}{2^3})+ {2^3-1}$$

Let us assume that the algorithm takes k iterations.

Therefore, after k iterations.

$$T(n)=2^kT(\frac{n}{2^k})+ {2^k-1}$$

Since, we know T(2). Therefore Put,
$$\frac{n}{2^k}=2$$

$$T(n)=\frac{n}{2} T(2)+ \frac{n}{2}-1$$
$$T(n)=\frac{n}{2} .1+ \frac{n}{2}-1$$
$$T(n)=n-1$$

Therefore, $T(n)$ is asymptotically $O(n)$

Another quick method of solving this is Master TheoremMaster Theorem

If someone is not aware of method to solve this recurrence, then We can solve this by Substitution

Given,

$$T(n)=2T(\frac{n}{2})+1$$

Now, going by relation,

$$T(\frac{n}{2})=2T(\frac{n}{4})+1$$

We will Substitute this in Original Recurrence Relation

Therefore,

$$T(n)=2T(\frac{n}{2})+1$$
$$T(n)=2 \Biggl(2T(\frac{n}{4})+1\Biggr) + 1$$
$$T(n)=2 \Biggl(2T(\frac{n}{2^2})+1\Biggr) + 1$$
$$T(n)=2 \Biggl(2 \Biggl( 2T(\frac{n}{8})+1\Biggr) +1 \Biggr) + 1$$
$$T(n)=2 \Biggl(2 \Biggl( 2T(\frac{n}{2^3})+1\Biggr) +1 \Biggr) + 1$$

Opening Brackets Carefully and Rearranging for Observing Pattern

$$T(n)=2^3T(\frac{n}{2^3})+ 2^2 + 2^1 + 1 $$
$$T(n)=2^3T(\frac{n}{2^3})+ 2^2 + 2^1 + 2^0 $$

Using Summation formula for Geometric Progression

$$T(n)=2^3T(\frac{n}{2^3})+ 2^0(\frac{2^3-1}{2-1})$$
$$T(n)=2^3T(\frac{n}{2^3})+ {2^3-1}$$

Let us assume that the algorithm takes k iterations.

Therefore, after k iterations.

$$T(n)=2^kT(\frac{n}{2^k})+ {2^k-1}$$

Since, we know T(2). Therefore Put,
$$\frac{n}{2^k}=2$$

$$T(n)=\frac{n}{2} T(2)+ \frac{n}{2}-1$$
$$T(n)=\frac{n}{2} .1+ \frac{n}{2}-1$$
$$T(n)=n-1$$

Therefore, $T(n)$ is asymptotically $O(n)$

Another quick method of solving this is Master Theorem

If someone is not aware of method to solve this recurrence, then We can solve this by Substitution

Given,

$$T(n)=2T(\frac{n}{2})+1$$

Now, going by relation,

$$T(\frac{n}{2})=2T(\frac{n}{4})+1$$

We will Substitute this in Original Recurrence Relation

Therefore,

$$T(n)=2T(\frac{n}{2})+1$$
$$T(n)=2 \Biggl(2T(\frac{n}{4})+1\Biggr) + 1$$
$$T(n)=2 \Biggl(2T(\frac{n}{2^2})+1\Biggr) + 1$$
$$T(n)=2 \Biggl(2 \Biggl( 2T(\frac{n}{8})+1\Biggr) +1 \Biggr) + 1$$
$$T(n)=2 \Biggl(2 \Biggl( 2T(\frac{n}{2^3})+1\Biggr) +1 \Biggr) + 1$$

Opening Brackets Carefully and Rearranging for Observing Pattern

$$T(n)=2^3T(\frac{n}{2^3})+ 2^2 + 2^1 + 1 $$
$$T(n)=2^3T(\frac{n}{2^3})+ 2^2 + 2^1 + 2^0 $$

Using Summation formula for Geometric Progression

$$T(n)=2^3T(\frac{n}{2^3})+ 2^0(\frac{2^3-1}{2-1})$$
$$T(n)=2^3T(\frac{n}{2^3})+ {2^3-1}$$

Let us assume that the algorithm takes k iterations.

Therefore, after k iterations.

$$T(n)=2^kT(\frac{n}{2^k})+ {2^k-1}$$

Since, we know T(2). Therefore Put,
$$\frac{n}{2^k}=2$$

$$T(n)=\frac{n}{2} T(2)+ \frac{n}{2}-1$$
$$T(n)=\frac{n}{2} .1+ \frac{n}{2}-1$$
$$T(n)=n-1$$

Therefore, $T(n)$ is asymptotically $O(n)$

Another quick method of solving this is Master Theorem

Source Link
Rohit Singh
  • 261
  • 3
  • 13

If someone is not aware of method to solve this recurrence, then We can solve this by Substitution

Given,

$$T(n)=2T(\frac{n}{2})+1$$

Now, going by relation,

$$T(\frac{n}{2})=2T(\frac{n}{4})+1$$

We will Substitute this in Original Recurrence Relation

Therefore,

$$T(n)=2T(\frac{n}{2})+1$$
$$T(n)=2 \Biggl(2T(\frac{n}{4})+1\Biggr) + 1$$
$$T(n)=2 \Biggl(2T(\frac{n}{2^2})+1\Biggr) + 1$$
$$T(n)=2 \Biggl(2 \Biggl( 2T(\frac{n}{8})+1\Biggr) +1 \Biggr) + 1$$
$$T(n)=2 \Biggl(2 \Biggl( 2T(\frac{n}{2^3})+1\Biggr) +1 \Biggr) + 1$$

Opening Brackets Carefully and Rearranging for Observing Pattern

$$T(n)=2^3T(\frac{n}{2^3})+ 2^2 + 2^1 + 1 $$
$$T(n)=2^3T(\frac{n}{2^3})+ 2^2 + 2^1 + 2^0 $$

Using Summation formula for Geometric Progression

$$T(n)=2^3T(\frac{n}{2^3})+ 2^0(\frac{2^3-1}{2-1})$$
$$T(n)=2^3T(\frac{n}{2^3})+ {2^3-1}$$

Let us assume that the algorithm takes k iterations.

Therefore, after k iterations.

$$T(n)=2^kT(\frac{n}{2^k})+ {2^k-1}$$

Since, we know T(2). Therefore Put,
$$\frac{n}{2^k}=2$$

$$T(n)=\frac{n}{2} T(2)+ \frac{n}{2}-1$$
$$T(n)=\frac{n}{2} .1+ \frac{n}{2}-1$$
$$T(n)=n-1$$

Therefore, $T(n)$ is asymptotically $O(n)$

Another quick method of solving this is Master Theorem