Skip to main content

Master’s Theorem Explained with Examples Vijay Sinha The Crazy Programmer

In this article, we will have a look at the famous Master’s Theorem. This is very useful when it comes to the Design and analysis of Algorithms following Divide and Conquer Technique. We will cover the theorem with its working and look at some examples related to it.

Master’s Theorem is Used For?

Master’s Method is functional in providing the solutions in Asymptotic Terms (Time Complexity) for Recurrence Relations. In simpler terms, it is an efficient and faster way in providing tight bound or time complexity without having to expand the relation. However, it is mainly responsible for Recurrence Relations based on Divide and Conquer Technique. Recurrence Relation is basically an equation where the next term of a function is dependent on its previous terms. We will have a look at it with some examples.

The Theorem is applicable for relations of the form:

T(n)= a T( n/b ) + f(n)

where a>=1, b>1.

Let us look at each term, here:

n-> Indicates the Size of Problem.

a -> Number of Subproblems in the Recursive Relation.

b -> Factor by which size of each Subproblem is reduced in each call.

n/b -> Size of each Subproblem  (Usually Same).

f(n) ->  Θ(nk logp  n ) ,where k >= 0 and p is a real number; It is cost or work done in merging each subproblem to get solution.

T(n) -> Indicates the total time taken to evaluate the Recurrence.

Master’s Theorem Cases

Now, Master’s Method determines the Asymptotic Tight Bound (Θ or Theta) on these recurrences considering 3 Cases:

Case 1

If  a > bk , then T(n)= Θ (n log b a )   [  log b a = log a / log b. ].

Let us understand this Case with example:

Suppose we are given a Recurrence Relation, T(n) =  16 T(n/4) + n .

Solution: 

For this relation, a = 16 , b = 4 , f(n) = Θ (nk logp  n) = n, where p=0 and k=1.

As we can see, value of a = 16 and value of bk  = 41 = 4. So a > b which falls under this Case 1 so the solution of this Recurrence Relation, T(n) = Θ ( n log b a ) = Θ ( n log 4 16  ).

Now  log 4 16 = log 16 / log 4 = log 42 / log 4 = 2 log 4 / log 4 = 2.

So, T(n) = Θ ( n2 ), is the tight bound runtime for this relation.

Case 2

If a = b, then there are again three possibilities to determine T(n) :

 1. If p > -1

In this case, the value of T(n) = Θ (n log b a logp+1  n).

Let us also look at this case with Recurrence Relation:

T(n) = 2 T(n/2) + n.

Solution:

For this relation,  a = 2, b = 2, f(n) = Θ (nk logp  n) = n, where k=1 and p=0.

Here, value of bk = 2, so a = bk , This falls under Case 2 moreover p > -1 so the solution for this relation will be:

T(n) = Θ (n log b a logp+1  n) = Θ (n log 2 2 log0+1  n) = Θ (n log n), is the tight bound for this relation.

Note: The above equation is the Recurrence relation of Merge Sort Algorithm, which has the time complexity of O(n log n), in all cases. So we can see with Master Theorem we easily determine the running time of any algorithm.

2. If p = -1

For this case, T(n) = Θ (n log b a log log n).

Let us evaluate this case with an example too.

Consider the following Recurrence Relation :

T(n) = 2 T(n/2) + n/log n.

Solution:

In this relation, a = 2, b = 2, f(n) = Θ (nk logp  n) = n/log n, where k =1 and p=-1.

Value of bk = 2, so a = bk , This falls under Case 2 moreover p = -1, so the solution is :

T(n) = Θ (n log 2 2 log log n) = Θ (n  log (log n)), is the tight bound for this recurrence.

Note: In this type of recurrences, at each step, the size of the subproblem shrinks by Square Root of n. The difference between and  is not polynomial, this is an extended version.

3. If p < -1

In this case, T(n) = Θ (nlog b a ).

Consider this Recurrence: T(n) = 8 T(n/2) + n3 / logn.

Here a = 8, b = 2, f(n) = Θ (nk logp  n) = n3  log n, where k=3, p = -2.

So, Value of bk = 8 and a = bk , falls under Case 2 .

Therefore, T(n) = Θ (nlog 2 8 ) [ log 2 8 = log 8/ log 2 = 3 log 2/log 2= 3] .

So, T(n) = Θ (n3 ).

This type of condition ( p < -1) is not generally encountered.

Case 3

For the last case, If a < b the asymptotic bounds are rounded again to two possibilities :

1. If p >= 0

When a < b and p is greater than 0, then solution, T(n) = Θ (n  log p n).
Let us consider an example to have a clear idea :

T(n) = 2 T(n/4) + n 0.62 .

Solution:

So for this Recurrence relation, a = 2, b = 4, f(n) = Θ (nk logp  n) = n 0.62 , where k = 0.62 and p=0.

Hence, b= 4 0.62 = 2.36 and a < bk . This make the recurrence fall under Case 3. Along with this, p >= 0.

Thus, T(n) = Θ (n  log p n) = Θ (n0.62  log0  n) = Θ (n0.62 ), is the tight bound Asymptotic Notation for this type of recurrence.

2. If p < 0

The Solution is given by, T(n) = Θ ( nk ).

Considering this Relation : T(n) = 2 T(n/4) + n0.51 / log n . Let’s outline the solution.

Solution:

Here, a = 2, b = 4, f(n) = Θ (nk logp  n) = n0.51 log-1 n, where k = 0.51 and p = -1. The condition a < bsatisfies as b = 2.027, falls under case 3 and p = -1 < 0.

So solution is T(n) = Θ ( n0.51 ).

So we have discussed all the cases for Master Method related to Divide and Conquer Recurrences. Now let us have a quick look at the Limitations of Master Method before ending this article.

Limitations of Master’s Theorem

  • Master’s theorem cannot be used if f(n), the combination/merging time is not positive. For Ex: T(n) = 16 T(n/2) – n, cannot be evaluated using this method as f(n) = – n.
  • The value of a should be constant and always greater than 1 . a denotes the number of sub-problems which should be fixed cannot be a function of n. E.g. T(n) = 2T(n/2) + n, cannot be solved using Master Method, a is not constant.
  • There should be at least one subproblem. Eg. T(n) = 0.5 T(n/2) + 2, cannot be evaluated as a = 0.5 < 1 .
  • The value of b must be > 1 and constant. Otherwise, the subproblems will keep increasing at each step and we never reach the base case for the recursion to end.

So that’s it for the article you can practice the examples discussed above. You can also practice some examples for better understanding here. Let us know your doubts or any suggestions in the comment section below.

The post Master’s Theorem Explained with Examples appeared first on The Crazy Programmer.



from The Crazy Programmer https://ift.tt/38EOLas

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20