What Is The Master Theorem?
The Master Theorem is a recurrence relation solver that is a very helpful tool to use when evaluating the performance of recursive algorithms. Using The Master Theorem, we can easily deduce the Big-O complexity of divide-and-conquer algorithms.
Recurrence Relations
A recurrence relation is a type of equation where each element depends on a previous outcome of the equation. A good example of a recurrence relation is the fibonacci sequence:
<div style="width:100%;text-align:center;font-size:24px;font-style:italic;"> F<sub>n</sub> = F<sub>n - 1</sub> + F<sub>n - 2</sub> </div>
When we write code in such a way, it typically makes use of recursion to allow a function to call itself over and over again.
Assumptions
Before we dive in further, it'll help to familiarize yourself with the following concepts:
- Knowledge of <a href="https://www.devmaking.com/learn/algorithms/big-o-notation/" target="_blank" style="color:inherit">Big-O notation</a>
- Familiar with divide and conquer
- Diminish and conquer is a plus!
The Master Theorem
<div style="width:100%; margin:auto;text-align:center;"> <img src="https://www.devmaking.com/img/topics/algs/Alg_MasterThm_01.png" alt="master theorem formula." style="max-width:95%;width:500px;"> </div>
The general form of the master theorem is as follows:
<div style="width:100%;text-align:center;font-size:24px;font-style:italic;"> T(n) = aT(n / b)+ O(n<sup>d</sup>) </div>
Let's break down what each variable represents:
- n / b: this represents the number of sub-problems that an algorithm breaks into each iteration. For example, Merge Sort divides an array into two halves until the array only has one item before reconstructing the array. Therefore it would be represented as
n/2
. - aT(..): while b represents the number of sub-problems that are divided into, a represents the number of sub problems that the algorithm will use. This is important as some algorithms divide a problem into three pieces, but may only operate on two of them. Binary search also utilizes this idea to make itself efficient.
- O(n<sup>d</sup>): The final part of the theorem states the amount of work that is done by the algorithm at each iteration. Mainly, it comes in the form of what work is done during the conquer portion when the solution is being assembled from the pieces.
Once you have a, b, and d, we plug the numbers into the equation and hold onto the result, T(n). With the result, we'll take a look at the system of equations in the above image. If we once again plug our numbers into the 3 comparisons on the right-side column, we'll notice only one of those comparisons are true. With the true comparison, the answer to the equation is the result of plugging our numbers into the corresponding Big-O equation on the left-side column.
The end result of this theorem is the upper bound of an algorithms execution time, represented in Big-O notation.
Examples
Merge Sort
<a href="https://www.devmaking.com/learn/algorithms/merge-sort/" target="_blank" style="color:inherit">Merge Sort</a> works to sort an array by dividing it into "left" and "right" halves recursively until only one element remains in each sub list. Once that has been achieved, the left and right arrays are "merged" back together in sorted order. Taking it step by step for each variable:
- We divide the array in half each time;
b = 2
. - Once the arrays have been divided, both halves are reassembled in sorted order;
a = 2
. - When "merging" the arrays, we need to look at every element;
O(n)
,d = 1
.
Representing this by the master theorem results in T(n) = 2T(n/2) + O(n).
When we plug these numbers into the equation:
<div style="width:100%;text-align:center;font-style:italic;font-size:150%;">d ? log<sub>b</sub>a</div> It equates to the following:
<div style="width:100%;text-align:center;font-style:italic;font-size:150%;">1 = log<sub>2</sub>2</div> Therefore, by matching the system, we know that the correct formula is:
<div style="width:100%;text-align:center;font-style:italic;font-size:150%;">n<sup>d</sup>log<sub>b</sub>n = n<sup>1</sup>log<sub>2</sub>n</div> Which evaluates to a final result of:
<div style="width:100%;text-align:center;font-style:italic;font-size:150%;">O(n log n)∎</div>
Binary Search
<a href="https://www.devmaking.com/learn/algorithms/binary-search/" target="_blank" style="color:inherit">Binary search</a> is a diminish and conquer algorithm that takes advantage of the master theorem properties to find a specific number in a sorted structure. It works by finding the midpoint of a list; if the number being looked for is higher, the bottom half of the list is discarded. The same is true in the opposite case. This occurs until the number is discovered and returned. Taking this step by step:
- We jump to the midpoint of the remaining array each iteration;
b = 2
. - We discard the half of the array that does not contain the value;
a = 1
. - Once the value is found, it is returned and no more work is done.
O(1)
,d = 0
.
This results in T(n) = (1)T(n/2) + O(1).
Plugging this into the equation results in 0 = log<sub>2</sub>1.
Using the corresponding formula yields:
<div style="width:100%;text-align:center;font-style:italic;font-size:150%;">n<sup>d</sup>log<sub>b</sub>n = n<sup>0</sup>log<sub>2</sub>n</sup></div> Which evaluates to a final result of:
<div style="width:100%;text-align:center;font-style:italic;font-size:150%;">O(log n)∎</div> <br>
> Challenge: If a recurrence relation results in a tree structure where the depth has at most 2<sup>d</sup> leaves, and a decision tree of size n
has at most n!
leaves, find the lower bound for sorting algorithms in Big-O notation!
Further Resources
- <a href="https://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X/ref=as_li_ss_tl?_encoding=UTF8&pd_rd_i=032157351X&pd_rd_r=37c135e7-c5c4-4ed1-94ab-c2aaec60d692&pd_rd_w=PEuac&pd_rd_wg=xSJkC&pf_rd_p=1c11b7ff-9ffb-4ba6-8036-be1b0afa79bb&pf_rd_r=H3XK6W265MFMNYZ7PV3Z&psc=1&refRID=H3XK6W265MFMNYZ7PV3Z&linkCode=ll1&tag=devmaking-20&linkId=9f9600161b287607f80038c5fd85b020&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Algorithms (Sedgwick)</a> An excellent resource and reference for learning algorithms in computer science.
- <a href="https://www.amazon.com/Algorithms-Sanjoy-Dasgupta-ebook-dp-B006Z0QR3I/dp/B006Z0QR3I/ref=as_li_ss_tl?_encoding=UTF8&me=&qid=1566246918&linkCode=ll1&tag=devmaking-20&linkId=26b190aacb9c02d1a0815d4066f9d80e&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Algorithms (Dasgupta)</a> Concise and clear overview on various types and designs of algorithms in a digestible format.
- <a href="https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=devmaking-20&linkId=1dd5be6066b180c12926dc03e2659215&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Introduction to Algorithms (CLRS)</a> An in depth and comprehensively detailed explanation of algorithms and how to design them.
- <a href="https://www.amazon.com/Introduction-Graph-Theory-Dover-Mathematics/dp/0486678709/ref=as_li_ss_tl?keywords=graph+theory&qid=1566248143&s=books&sr=1-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExUVdSUTU4TjVPRzdIJmVuY3J5cHRlZElkPUEwNzM2MDIyMkIxUUdNVENLMVJFUiZlbmNyeXB0ZWRBZElkPUEwNzc2NTcyNDBEMlFGWDJRRU5XJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==&linkCode=ll1&tag=devmaking-20&linkId=3a3d02f5e5b338587f4e4e4be75fe2f2&language=en_US" target="_blank" style="color:#fff;border-radius:3px;background-color:#888;padding:1px 5px">Introduction to Graph Theory</a> An accessible, mathematic-centered approach to graph theory for new programmers and mathematicians.