# Master Theorem

# Master Theorem

## Introduction:

** The Master Theorem is a mathematical formula for analyzing and solving recursive problems.** In a nutshell, it estimates the time or complexity of algorithms that break a problem into subproblems.

The theorem is widely utilized in computer science and algorithm design. It contains three situations that are determined by the ratio of the size of the issue to the size of the subproblems. These cases help to determine the overall time or complexity of the algorithm.

To apply the Master Theorem, compare the sizes of the subproblems to the original problem size and decide which case applies. The duration or complexity of the method may then be determined based on the situation.

In summary, *the Master Theorem is a tool that helps analyze and estimate the efficiency of recursive algorithms by considering the relationship between the problem size and the subproblem sizes.*

### The Master Theorem Formula:

The above image depicts the formula of the Master Theorem for solving recursive equations.

where:

**T(n)**represents the time or complexity of the algorithm for a problem of size n.**a**is the number of recursive subproblems.**n/b**represents the size of each subproblem.**f(n)**represents the time or complexity spent outside of the recursive calls.

### The Master Theorem formula has three cases:

**Case 1:**For some constant c< log_b(a) if f(n) is O)n^c), then T(n) is equals to O(n^log_b(a).

**Case 2:**If f(n) is Theta(n^c) for any constant c = log_b(a), then T(n) equals O(n^c log n).

**Case 3:**If f(n) is Omega(n^c) for some constant c> log_b(a), and af(n/b) <= kf(n) for some constant k < 1 and sufficiently big n, T(n) equals Theta(f(n)).

- In Case 1, (For some constant c< log_b(a) if f(n) is O)n^c), then T(n) is equals to O(n^log_b(a)), the time spent on the work outside of the recursive calls is trivial compared to the time spent on the recursive calls. Hence, the running time can be expressed in terms of the time spent on the recursive calls, which is O(n^log_b(a)).
- In Case 2, (If f(n) is Theta(n^c) for some constant c = log_b(a), then T(n) is O (n^c log n)), the time spent on tasks other than recursive calls is proportional to the time spent on recursive calls. As a result, the running time may be defined in terms of the time spent on work other than recursive calls, Theta(n^c), multiplied by the time spent on recursive calls, O (log n). As a result, the running time is O (n^c log n).
- In Case 3, the time spent on the work outside of the recursive calls dominates the time spent on the recursive calls. As a result, the running time, Omega(n^c), may be described in terms of the time spent on the task outside of the recursive calls.

**Examples:**

Let us see some examples of all the three different cases so, we will get a clarity.

**Example1:** T(n) = 2.T(n/2) + n

Here, according to the master theorem formula:

a = 2

b = 2

f(n) = n.

According to the master theorem, this function falls into Case 2,

since c = log_b(a) = log_2(2) = 1. **The time complexity of this approach is hence O (n log n).**

**Example2: ** T(n) = 4T(n/2) + n^2

Here, according to the master theorem formula:

a = 4

f(n) : n^2

b = 2

According to the master theorem, this function falls into Case 1,

since c = 2 > log_b(a) = log_2(4) = 2**. Hence, the time complexity is hence O(n^2).**

**Example3: ** T(n) = 3T(n/4) + n^2

Here, according to the master theorem formula:

a = 3

f(n): n^2

b = 4

According to the master theorem, this function falls into Case 3, since c = 2 > log_b(a) = log_4(3) ≈ 0.79, and a.f(n/b) = 3(n/4) ^2 = (3/16) n^2 <= kf(n) for k = 1/2 and sufficiently large n. **The time complexity of this algorithm is Theta(n^2).**

## Conclusion:

To summarize, the Master Theorem is a useful tool for analyzing and estimating how long it takes specific types of algorithms to solve tasks. It has several situations depending on the magnitude of the problem and the work done within and outside of recursive calls. By using the proper scenario, we can immediately determine the algorithm's efficiency without performing any hard computations.

The Master Theorem is useful because it enables algorithm designers to make informed decisions and predict how their algorithms will perform. It saves time by allowing you to estimate time or complexity more quickly.

The Master Theorem is a helpful tool that predicts how long an algorithm will take to solve a problem. It assists algorithm designers in making better selections and saves them from performing complex mathematics.