Recurrence Relations (original) (raw)

Last Updated : 17 Jan, 2026

A **recurrence relation is a mathematical expression that defines a sequence in terms of its previous terms. In the context of algorithmic analysis, it is often used to model the time complexity of recursive algorithms.

General form of a **Recurrence Relation:

a_{n} = f(a_{n-1}, a_{n-2},....,a_{n-k})

where **f is a function that defines the relationship between the current term and the previous terms

Significance of Recurrence Relations in DSA

Recurrence Relations play a significant role in analyzing and optimizing the complexity of algorithms. Some of the common uses of Recurrence Relations are:

Common Examples of Recurrence Relations:

Types of Recurrence Relations:

Various types of Recurrence Relations are:

1. Linear Recurrence Relations:

Following are some of the examples of recurrence relations based on linear recurrence relation.

T(n) = T(n-1) + n for n > 0 and T(0) = 1

These types of recurrence relations can be easily solved using substitution method.

For example,

T(n) = T(n-1) + n
= T(n-2) + (n-1) + n
= T(n-k) + (n-(k-1))….. (n-1) + n

Substituting k = n, we get

T(n) = T(0) + 1 + 2+….. +n = n(n+1)/2 = O(n^2)

2. Divide and conquer recurrence relations:

Following are some of the examples of recurrence relations based on divide and conquer.

T(n) = 2T(n/2) + cn
T(n) = 2T(n/2) + √n

These types of recurrence relations can be easily solved using Master Method.
For recurrence relation: T(n) = 2T(n/2) + cn, the values of a = 2, b = 2 and k =1. Here log_b(a) = log_2(2) = 1 = k. Therefore, the complexity will be Θ(nlog_2(n)).
Similarly for recurrence relation T(n) = 2T(n/2) + √n, the values of a = 2, b = 2 and k =1/2. Here log_b(a) = log_2(2) = 1 > k. Therefore, the complexity will be Θ(n).

3. Substitution Recurrences:

Sometimes, recurrence relations can’t be directly solved using techniques like substitution, recurrence tree or master method. Therefore, we need to convert the recurrence relation into appropriate form before solving. For example,

**T(n) = T(√n) + 1

To solve this type of recurrence, substitute n = 2^m as:

T(2^m) = T(2^m /2) + 1Let T(2^m) = S(m),S(m) = S(m/2) + 1

Solving by master method, we get

S(m) = Θ(logm)As n = 2^m or m = log_2(n),T(n) = T(2^m) = S(m) = Θ(logm) = Θ(loglogn)

4. Homogeneous Recurrence Relations:

A homogeneous recurrence relation is one in which the right-hand side is equal to zero. Mathematically, a homogeneous recurrence relation of order k is represented as:

a_{n} = f(a_{n-1}, a_{n-2},..., a_{n-k})

with the condition that the above equation equates to 0.

Example: a_{n} = 2*a_{n-1} - a_{n-2}

5. Non-Homogeneous Recurrence Relations:

A non-homogeneous recurrence relation is one in which the right-hand side is not equal to zero. It can be expressed as:

a_{n} = f(a_{n-1}, a_{n-2},...,a_{n-k}) + g(n)

where g(n) is a function that introduces a term not dependent on the previous terms. The presence of g(n) makes the recurrence non-homogeneous.
Example: a_{n} = 2*a_{n-1}- a_{n-2} + 3^n

In this case, the term 3^n on the right-hand side makes the recurrence non-homogeneous.

Ways to Solve Recurrence Relations:

There are mainly three ways of solving recurrences. Please refer analysis of recursion for more details.

  1. Substitution Method
  2. Recurrence Tree Method
  3. Master Method

1. Substitution Method:

We make a guess for the solution and then we use mathematical induction to prove the guess is correct or incorrect.

T(n) = 2T(n/2) + n
<= 2cn/2Log(n/2) + n
= cnLogn – cnLog2 + n
= cnLogn – cn + n
<= cnLogn

2. Recurrence Tree Method:

In this method, we draw a recurrence tree and calculate the time taken by every level of the tree. Finally, we sum the work done at all levels. To draw the recurrence tree, we start from the given recurrence and keep drawing till we find a pattern among levels. The pattern is typically arithmetic or geometric series.

Consider the recurrence relation, T(n) = T(n/4) + T(n/2) + cn^2

cn^2
/ \
T(n/4) T(n/2)

If we further break down the expression T(n/4) and T(n /2), we get the following recursion tree.

cn^2
/ \
c(n^2 )/16 c(n^2 )/4
/ \ / \
T(n/16) T(n/8) T(n/8) T(n/4)

Breaking down further gives us following

cn^2
/ \
c(n^2 )/16 c(n^2 )/4
/ \ / \
c(n^2 )/256 c(n^2 )/64 c(n^2 )/64 c(n^2 )/16
/ \ / \ / \ / \

To know the value of T(n), we need to calculate the sum of tree nodes level by level. If we sum the above tree level by level, we get the following series T(n) = cn^2(1 + 5/16 + 25/256 ) + ….
The above series is a geometrical progression with a ratio of 5/16.

To get an upper bound, we can sum the infinite series. We get the sum as (cn^2 )/(1 – 5/16) which is O(n^2 )

3. Master Method:

**Master Method is a direct way to get the solution. The master method works only for the following type of recurrences or for recurrences that can be transformed into the following type.

T(n) = aT(n/b) + f(n) where a >= 1 and b > 1

There are the following three cases:

How does this work?

The master method is mainly derived from the recurrence tree method. If we draw the recurrence tree of **T(n) = aT(n/b) + f(n), we can see that the work done at the root is **f(n), and work done at all leaves is **Θ(nc) where **c is Log_ba. And the height of the recurrence tree is Log_bn

Master Theorem

In the **Recurrence Tree Method, we calculate the total work done. If the work done at leaves is polynomial more, then leaves are the dominant part, and our result becomes the work done at leaves (Case 1). If work done at leaves and root is asymptotically the same, then our result becomes height multiplied by work done at any level (Case 2). If work done at the root is asymptotically more, then our result becomes work done at the root (Case 3).

Examples of some standard algorithms whose time complexity can be evaluated using the Master Method

Also Check:

To know more about ways to solve Recurrence Relations, Click Here