Algorithm Analysis
Time Complexity
Recursion
Computer Science
Computational Efficiency

Time complexity of a recursive algorithm

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction to Time Complexity

When analyzing the efficiency of algorithms, time complexity is one of the most essential concepts. It measures the amount of computational time that an algorithm takes to complete as a function of the length of the input. For recursive algorithms, calculating time complexity often involves solving recurrence relations, which express the overall time complexity as a function of the input size and the complexity of solving smaller sub-problems.

Basics of Recursive Algorithms

Recursive algorithms solve a problem by dividing it into smaller sub-problems of the same type. The algorithm calls itself with these smaller inputs and uses the results of these calls to construct a solution to the original problem. Recursive algorithms commonly consist of:

  1. Base Case(s): These are simple cases that can be solved directly without further recursion.
  2. Recursive Case: This involves complex problems that are divided into smaller, manageable sub-problems. The algorithm calls itself with these sub-problems, solving them recursively.

Analyzing Time Complexity in Recursive Algorithms

The time complexity of recursive algorithms is often expressed by recurrence relations. A recurrence relation is a mathematical equation that defines a sequence based on one or more of its preceding terms. To solve these relations, various techniques such as the Master Theorem, substitution method, and recursion tree method are employed.

The Master Theorem

The Master Theorem provides a way to solve recurrence relations of the form:

T(n)=aT(nb)+f(n)T(n) = aT\left(\frac{n}{b}\right) + f(n)

Where: • `a` is the number of sub-problems into which the problem is divided. • `b` is the factor by which the sub-problem size is reduced. • `f(n)` defines the cost of dividing the problem and combining the results of its sub-problems.

According to the Master Theorem:

• If f(n)=O(nc)f(n) = O(n^c) with c<logbac < \log_b a, then T(n)=Θ(nlogba)T(n) = \Theta(n^{\log_b a}). • If f(n)=Θ(nlogba)f(n) = \Theta(n^{\log_b a}), then T(n)=Θ(nlogbalogn)T(n) = \Theta(n^{\log_b a} \log n). • If f(n)=Ω(nc)f(n) = \Omega(n^c) with c>logbac > \log_b a, then T(n)=Θ(f(n))T(n) = \Theta(f(n)).

Example: Merge Sort

Merge Sort is a classic recursive algorithm and a good example to understand time complexity. Its time complexity can be derived using the Master Theorem.

Merge Sort Recurrence Relation

  1. Divide: Split the list into two halves.
  2. Conquer: Recursively sort the two halves.
  3. Combine: Merge the sorted halves to produce the sorted result.

The recurrence relation for Merge Sort is:

T(n)=2T(n2)+cnT(n) = 2T\left(\frac{n}{2}\right) + cn

Where 2T(n2)2T(\frac{n}{2}) is the cost of recursively sorting the two halves, and cncn is the cost of merging them.

Applying the Master Theorem

Here, a=2a = 2, b=2b = 2, and f(n)=cnf(n) = cn. Since logba=1\log_b a = 1 and f(n)=Θ(n)f(n) = \Theta(n), we are in the second case of the Master Theorem, which gives:

T(n)=Θ(nlogn)T(n) = \Theta(n \log n)

Techniques for Solving Recurrence Relations

  1. Substitution Method: Guess the form of the solution and prove it by induction.
  2. Recursion Tree: Model the recurrence as a tree and sum over all levels.
  3. Master Theorem: Apply the theorem to standard forms to derive complexity classes.

Summary Table

TechniqueWhen to UseTime Complexity Expression
Master TheoremFor divide-and-conquer formsT(n)=aT(nb)+f(n)T(n) = aT(\frac{n}{b}) + f(n)
SubstitutionSimple and direct proofsGuess form, use induction
Recursion TreeVisualization of recursionSum over all levels of tree

Conclusion

Understanding the time complexity of recursive algorithms is essential for evaluating their performance and efficiency. By mastering recurrence relations and using powerful tools like the Master Theorem, developers can calculate the time complexity, optimize their algorithms, and ultimately improve the performance of their software systems.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.