Master theorem is a direct way to get the solution of a recurrence relation, provided that it is of the following type:
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
The theorem consists of the following three cases:
1.If f(n) = () where c < then T(n) = (n)
2.If f(n) = () where c = then T(n) = (Log n)
3.If f(n) = () where c > then T(n) = (f(n))
Imagine the recurrence aT(n/b) + f(n) in the form of a tree.
Case 1 covers the case when the children nodes does more work than the parent node.
For example the equation T(n)=2T(n/2)+1 falls under the category of case 1 and we can clearly see from it’s tree below that at each level the children nodes perform twice as much work as the parent node.
T(n) ------(1) / \ T(n/2) T(n/2) ------(2) / \ / \
Case 2 covers the case when the children node and the parent node does an equal amount of work.
For example the equation T(n)=2T(n/2)+n falls under the category of case 2 and we can clearly see from it’s tree below that at each level the children nodes perform as much work as the parent node.
T(n) ------(n) / \ T(n/2) T(n/2) ------(n) / \ / \
Case 3 covers the scenario that the parent node does more work than the children nodes.
T(n)=T(n/2)+n is a example of case 3 where the parent performs more work than the child.
T(n) ------(n) | T(n/2) ------(n/2) |
In case 1 and case 2 the case conditions themselves make sure that work done by children is either more or equal to the parent but that is not the case with case 3.
In case 3 we apply a regulatory condition to make sure that the parent does at least as much as the children.
The regulatory condition for case 3 is
This says that f(n) (the amount of work done in the root) needs to be at least as big as the sum of the work done in the lower levels.
The equation T(n) = T(n/2) + n(sin(n – /2) + 2) is a example where regulatory condition makes a huge difference. The equation isn’t satisfying case 1 and case 2. In case 3 for large values of n it can never satisfy the regulatory condition. Hence this equation is beyond the scope of master theorem.
This article is contributed by Vineet Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Master Theorem For Subtract and Conquer Recurrences
- Advanced master theorem for divide and conquer recurrences
- Paranthesis Theorem
- Count of pairs satisfying the given condition
- Partition the digits of an integer such that it satisfies a given condition
- Loop Invariant Condition with Examples of Sorting Algorithms
- Maximize profit in buying and selling stocks with Rest condition
- Determine winner of the Game by arranging balls in a row
- Difference between Big Oh, Big Omega and Big Theta
- Check if Pascal's Triangle is possible with a complete layer by using numbers upto N
- Count subarrays having sum of elements at even and odd positions equal
- Sorting algorithm visualization : Heap Sort
- Find position of non-attacking Rooks in lexicographic order that can be placed on N*N chessboard
- Split numbers from 1 to N into two equal sum subsets