Engineer bro!

Mon May 02 2022 (2 weeks ago)

Engineer by mistake!

I am a software engineer by passion

# Recurrence Relation

Recurrence relation is an equation that expresses the nth term of a sequence as a function of k preceding terms, for some fixed k (independent from n), which is called the order of the relation. Once k initial terms of a sequence are given, the recurrence relation allows computing recursively all the remaining terms of the sequence.

Analyzing control statements are easy because they are iterative. Many algorithms are recursive in nature and when we analyze them we get a recurrence relation for time complexity.

We get running time as a function of n(input size) and we get the running time on inputs of smaller size.
A recurrence relation recursively defines a sequence where the next term is a function of previous term.

## Methods to solve recurrence relation

1. Substitution ***

2. Homogeneous (characteristic equation)

3. Inhomogeneous

4. Master Theorem ***

5. Recurrence tree ***

6. Intelligent guess work

7. Change of variable

8. Range transformations

## Substitution Method

In substitution method we make guess for the solution then we use mathematical induction to prove the guess is correct or incorrect. This is the most acceptable method as it always works for any recurrence relation, although it takes too much time in calculation.

### Example - 1

$T\left(n\right)T\left(n\right)$ is the time to solve instance of size n, and $T\left(n-1\right)T\left(n-1\right)$ is the time to solve instance of size $n-1n-1$.

Replacing $nn$ by $n-1n-1$ and $n-2n-2$, we can write the following equations.

Substituting equation 3 in 2, we'll get equation 4 as follows:

By substituting equation 4 in 1, we'll get the following equation:

From above observation we can write the general form of the equation 1 as follows.

$T\left(n\right)=T\left(n-k\right)+T\left(n-k+1\right)+T\left(n-k+2\right)+\mathrm{.}\mathrm{.}\mathrm{.}+nT\left(n\right) = T\left(n-k\right)+T\left(n-k+1\right)+T\left(n-k+2\right)+...+n$

Suppose, if we take $k=nk=n$ then,

### Example - 2

If we rewrite the equation then we'll get that,

By relpacing $nn$ by $n-1n-1$, we'll get the following

By replacing $nn$ by $n-2n-2$, we'll get the following

If we substitute eq (3) into eq (2), then we'll get the following

Substituting the value of eq (4) into eq (1), we'll get the following

From the above observation, we can write the general form of the equation as below

Suppose if we take $k=nk=n$, then we could rewrite the above equation as follows.

$T\left(n\right)=nc2+T\left(n-n\right)\phantom{\rule{0ex}{0ex}}T\left(n\right)=nc2+T\left(0\right)\phantom{\rule{0ex}{0ex}}T\left(n\right)=nc2+c1\phantom{\rule{0ex}{0ex}}T\left(n\right)=O\left(n\right)T\left(n\right) = nc2 + T\left(n-n\right) \\ T\left(n\right) = nc2 + T\left(0\right) \\ T\left(n\right) = nc2 + c1 \\ T\left(n\right) = O\left(n\right)$

## Master Theorem

The master theorem always yields asymptotically tight bounds to recurrences from divide and conquer algorithms that partition an input into smaller subproblems of equal sizes, solve the subproblems recursively, and then combine the subproblem solutions to give a solution to the original problem. The time for such an algorithm can be expressed by adding the work that they perform at the top level of their recursion (to divide the problems into subproblems and then combine the subproblem solutions) together with the time made in the recursive calls of the algorithm.

Suppose if we have the recurrence of the following form:

$T\left(n\right)=aT\left(n\mathrm{/}b\right)+f\left(n\right)T\left(n\right) = aT\left(n/b\right) + f\left(n\right)$

The above recurrence relation constitutes of three parts. $T\left(n\right)T\left(n\right)$ is the time taken to solve a problem of size $nn$, $aa$ is the number of sub-problems, $T\left(n\mathrm{/}b\right)T\left(n/b\right)$ is the time required to solve a sub-problem and $f\left(n\right)f\left(n\right)$ is the time required divide and conquer the sub-problems.

There are three cases:

1. Case 1 : if $f\left(n\right)f\left(n\right)$ is in $O\left({n}^{{\mathrm{log}}_{b}a}\right)O\left(n^\left\{ \log_ba\right\}\right)$, i.e $f\left(n\right)\le {n}^{{\mathrm{log}}_{b}a}f\left(n\right) \le n^\left\{ \log_ba\right\}$ then $T\left(n\right)={n}^{{\mathrm{log}}_{b}a}T\left(n\right) = n^\left\{ \log_ba\right\}$

2. Case 2 : if $f\left(n\right)f\left(n\right)$ is in $\mathrm{\Theta }\left({n}^{{\mathrm{log}}_{b}a}\right)\Theta\left(n^\left\{ \log_ba\right\}\right)$, i.e $f\left(n\right)={n}^{{\mathrm{log}}_{b}a}f\left(n\right) = n^\left\{ \log_ba\right\}$ then $T\left(n\right)={n}^{{\mathrm{log}}_{b}a}\mathrm{lg}nT\left(n\right) = n^\left\{ \log_ba\right\} \lg n$

3. Case 3 : if $f\left(n\right)f\left(n\right)$ is in $\mathrm{\Omega }\left({n}^{{\mathrm{log}}_{b}a}\right)\Omega\left(n^\left\{ \log_ba\right\}\right)$, i.e $f\left(n\right)\ge {n}^{{\mathrm{log}}_{b}a}f\left(n\right) \ge n^\left\{ \log_ba\right\}$ then $T\left(n\right)=f\left(n\right)T\left(n\right) = f\left(n\right)$

### Example - 1

$T\left(n\right)=2T\left(n\mathrm{/}2\right)+\mathrm{\Theta }\left(n\right)T\left(n\right) = 2T\left(n/2\right) + \Theta\left(n\right)$

Here, $a=2,b=2a=2, b=2$. So, ${n}^{{\mathrm{log}}_{b}a}=nn^\left\{\log_ba\right\}=n$
Also, $f\left(n\right)=\mathrm{\Theta }\left(n\right)=cnf\left(n\right)=\Theta\left(n\right)=cn$.
Case 2 applies : $T\left(n\right)=\mathrm{\Theta }\left(n\mathrm{lg}n\right)T\left(n\right) = \Theta\left(n \lg n\right)$

### Example - 2

$T\left(n\right)=T\left(n\mathrm{/}2\right)+\mathrm{\Theta }\left(1\right)T\left(n\right) = T\left(n/2\right) + \Theta\left(1\right)$

Here, $a=1,b=2a=1, b=2$. So, ${n}^{{\mathrm{log}}_{b}a}={n}^{{\mathrm{log}}_{2}1}={n}^{0}=1n^\left\{\log_ba\right\}=n^\left\{\log_21\right\}=n^0=1$
Also, $f\left(n\right)=\mathrm{\Theta }\left(1\right)=1f\left(n\right)=\Theta\left(1\right)=1$.
Case 2 applies : $T\left(n\right)=\mathrm{\Theta }\left(\mathrm{log}n\right)T\left(n\right) = \Theta\left(\log n\right)$

### Example - 3

$T\left(n\right)=4T\left(n\mathrm{/}2\right)+nT\left(n\right) = 4T\left(n/2\right)+n$

Here, $a=4,b=2a=4, b=2$. So, ${\mathrm{log}}_{b}a=2\log_ba=2$ and ${n}^{{\mathrm{log}}_{b}a}={n}^{2}n^\left\{\log_ba\right\}=n^2$

$f\left(n\right)=nf\left(n\right) = n$
So,
Case 1 applies : $T\left(n\right)=\mathrm{\Theta }\left({n}^{2}\right)T\left(n\right) = \Theta\left(n^2\right)$

## Recurrence Tree Method

In recurrence tree method, each node represents the cost of a single sub-problem in the set of recursive function invocations. We sum the cost within each level of the tree to obtain a set of per level cost.

We sum the per level cost in-order to determine the total cost of the all levels of the recursion.

Here while solving the problem, we divide the problem into sub-problems of equal size.

E.g,

$T\left(n\right)=aT\left(n\mathrm{/}b\right)+f\left(n\right)T\left(n\right) = aT\left(n/b\right) + f\left(n\right)$

where $a>1,b>1a>1, b>1$ and $f\left(n\right)f\left(n\right)$ is a given function.

$f\left(n\right)f\left(n\right)$ is the cost of splitting and combining the sub-problems.

In the above diagram, a problem of size n has been divided into two sub-problems of size $n\mathrm{/}bn/b$.

### Example

$T\left(n\right)=2T\left(n\mathrm{/}2\right)+nT\left(n\right) = 2T\left(n/2\right) + n$

The recurrence tree for this recurrence is:

When we add the values across the levels of the recursion tree, we get a value of n for every level. The bottom level has ${2}^{\mathrm{log}n}2^\left\{\log n\right\}$ nodes and each contributing of $T\left(1\right)T\left(1\right)$.

We have
So,

$T\left(n\right)=\sum _{i=0}^{{\mathrm{log}}_{2}n-1}n+{2}^{\mathrm{log}n}T\left(1\right)\phantom{\rule{0ex}{0ex}}T\left(n\right)=n\mathrm{log}n+n\phantom{\rule{0ex}{0ex}}T\left(n\right)=O\left(n\mathrm{log}n\right)T\left(n\right) = \sum_\left\{i=0\right\}^\left\{\log_2n-1\right\} n+2^\left\{\log n\right\} T\left(1\right) \\ T\left(n\right) = n \log n + n \\ T\left(n\right) = O\left(n \log n\right)$

## Conclusion

We have seen what are the recurrence relations and popular methods to solve them. We have also seen some examples for each method.

AlgorithmsData StructureDesign And Analysis of Algorithms