# Substitution Method in Data Structure

Data StructureAnalysis of AlgorithmsAlgorithms

Here we will see how to use substitution method to solve recurrence relations. We will take two examples to understand it in better way.

Suppose we are using the binary search technique. In this technique, we check whether the element is present at the end or not. If that is present at middle, then the algorithm terminates, otherwise we take either the left and right subarray from the actual array again and again. So in each step the size of the array decreases by n / 2. Suppose the binary search algorithm takes T(n) amount of time to execute. The base condition takes O(1) amount of time. So the recurrence equation will be like below −

$$T(n)=\begin{cases}T(1) & for\:n \leq 1\\2T(\frac{n}{2})+c & for\:n > 1\end{cases}$$

Solve − We will substitute the formula in each step to get the result −

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

By substituting T(N/2) we can write,

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

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

$$T(n)=T(\frac{n}{8})+3c$$

$$T(n)=T(\frac{n}{2^{k}})+kc$$

Now if $$(\frac{n}{2^{k}})$$ reaches to 1, it indicates that 2k is n. So the value of k is log2𝑛

The complexity of T(n) = ϴ(log n)

Similarly, if we choose another example like merge sort, then in that case we divide the list into two parts. This division is taking place until the list size is only 1. After that we merge them in sorted order. The merging algorithm takes O(n) amount of time. So if the merge sort algorithm is taking T(n) amount of time, then by dividing it into two halves, and do the same task for each of them, they will take T(n/2) and so on. So the recurrence relation will be like below −

$$T(n)=\begin{cases}T(1) & for\:n = 1\\2T(\frac{n}{2})+cn & for\:n > 1\end{cases}$$

Solve − We will substitute the formula in each step to get the result −

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

By substituting T(N/2) we can write,

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

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

$$T(n)=8T(\frac{n}{8}) +3cn$$

$$T(n)=2^{k}T(\frac{n}{2^{k}}) +kcn$$

Now if $$(\frac{n}{2^{k}})$$ reaches to 1, it indicates that 2k is n. So the value of k is log2𝑛. And T(n) will be −

𝑇(𝑛) = 𝑛𝑇(1) + 𝑐𝑛 log2𝑛

The complexity is θ(n log n)