Divide and Conquer/Master Theorem: Difference between revisions
From charlesreid1
(Fix math errors: Big Omega definition missing constant c, set definition using f(n) instead of h(n) and unquantified n_0, non-symmetric notation now in math mode, add missing parentheses around factorial argument, replace \quad with \cdot for multiplication, fix "n >= n0" to proper math mode (via update-page on MediaWiki MCP Server)) |
|||
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==Statement of Master Theorem== | ==Statement of Master Theorem== | ||
| Line 5: | Line 7: | ||
'''Case 1: Subproblems Dominate (Leaf-Heavy)''' | '''Case 1: Subproblems Dominate (Leaf-Heavy)''' | ||
If <math>f(n) = O(n^{ \ | If <math>f(n) = O( n^{\log_b a - \epsilon} )</math> for some constant <math>\epsilon > 0</math> then <math>T(n) = \Theta( n^{\log_b a} )</math> | ||
'''Case 2: Split/Recombine ~ Subproblems''' | '''Case 2: Split/Recombine ~ Subproblems''' | ||
If <math>f(n) = \Theta( n^{\log_b | If <math>f(n) = \Theta( n^{\log_b a} \log^k n )</math>, then <math>T(n) = \Theta( n^{\log_b a} \log^{k+1} n )</math> (note that usually <math>k=0</math>) | ||
'''Case 3: Split/Recombine Dominates (Root Heavy)''' | '''Case 3: Split/Recombine Dominates (Root Heavy)''' | ||
If <math>f(n) = \Omega( n^{\log_b | If <math>f(n) = \Omega( n^{\log_b a + \epsilon} )</math> for some constant <math>\epsilon > 0</math> , and if <math>a f(n/b) \leq cf(n)</math> for some <math>c < 1</math>, then <math>T(n) = \Theta( f(n) )</math> | ||
==Revisiting Big O, Big Theta, Big Omega== | ==Revisiting Big O, Big Theta, Big Omega== | ||
| Line 31: | Line 33: | ||
</math> | </math> | ||
for all sufficiently large < | for all sufficiently large <math>n \geq n_0</math> | ||
We say that f(n) is ''bounded above'' by g(n). | We say that f(n) is ''bounded above'' by g(n). | ||
| Line 39: | Line 41: | ||
Big O corresponds roughly to less than or equal to | Big O corresponds roughly to less than or equal to | ||
Note that this equal sign notation is NOT symmetric - n^3 | Note that this equal sign notation is NOT symmetric - <math>n^3 \neq O(n^2)</math> | ||
We can also think about a ''set definition'' - f(n) is in some set of functions that are like g(n) | We can also think about a ''set definition'' - f(n) is in some set of functions that are like g(n) | ||
| Line 46: | Line 48: | ||
<math> | <math> | ||
{h(n) : \ | \{ h(n) : \exists c > 0, n_0 > 0, 0 \leq h(n) \leq c g(n) \} | ||
</math> | </math> | ||
| Line 61: | Line 63: | ||
</math> | </math> | ||
This means there is a function h(n) in the set O( | This means there is a function h(n) in the set O(n^2) such that f(n) = n^3 + h(n) | ||
they represent SOME func in that set | they represent SOME func in that set | ||
| Line 71: | Line 73: | ||
</math> | </math> | ||
is a true statement, but the reverse is not | is a true statement, but the reverse is not true. | ||
This means, for any f(n) in O(n), there exists some function g(n) in O(n^2) such that if you add n^2 to the function f(n), you get g(n). | This means, for any f(n) in O(n), there exists some function g(n) in O(n^2) such that if you add n^2 to the function f(n), you get g(n). | ||
| Line 84: | Line 86: | ||
<math> | <math> | ||
\exists c > 0, n_0 > 0 : 0 \leq g(n) \leq f(n) | \exists c > 0, n_0 > 0 : 0 \leq c g(n) \leq f(n) | ||
</math> | </math> | ||
| Line 94: | Line 96: | ||
<math> | <math> | ||
f(n) = \Theta( g(n) ) = \Omega( g(n) ) \ | f(n) = \Theta( g(n) ) = \Omega( g(n) ) \cap O(g(n)) | ||
</math> | </math> | ||
| Line 103: | Line 105: | ||
</math> | </math> | ||
for n > | for <math>n \geq n_0</math> | ||
==Examples== | ==Examples== | ||
| Line 118: | Line 120: | ||
<math> | <math> | ||
\log_{b}{a} = log_{2}{2} = 1 | \log_{b}{a} = \log_{2}{2} = 1 | ||
</math> | </math> | ||
| Line 128: | Line 130: | ||
<math> | <math> | ||
f(n) = \Theta( n^c \log^{k} | f(n) = \Theta( n^c \log^{k} n ) | ||
</math> | </math> | ||
| Line 144: | Line 146: | ||
<math> | <math> | ||
T(n) = 2 T(\frac{n}{2}) + \frac{n}{log n} | T(n) = 2 T(\frac{n}{2}) + \frac{n}{\log n} | ||
</math> | </math> | ||
| Line 158: | Line 160: | ||
<math> | <math> | ||
c = \log_b{a} = log_{4}{2} = \frac{1}{2} | c = \log_b{a} = \log_{4}{2} = \frac{1}{2} | ||
</math> | </math> | ||
| Line 171: | Line 173: | ||
so we have Case 3 | so we have Case 3 | ||
We need to check the regularity condition: | |||
<math> | |||
2 f( \frac{n}{4} ) \leq c f(n) | |||
</math> | |||
<math> | <math> | ||
\ | \frac{2}{4^{0.51}} n^{0.51} < c n^{0.51} | ||
2 | |||
</math> | </math> | ||
| Line 188: | Line 192: | ||
===Example 4=== | ===Example 4=== | ||
<math> | |||
T(n) = 0.5 T(\frac{n}{2}) + \frac{1}{n} | |||
</math> | |||
Careful - this case does not apply! <math>a < 1</math> violates one of the assumptions of the theorem. | |||
===Example 5=== | |||
<math> | |||
T(n) = 16 T(\frac{n}{4}) + n! | |||
</math> | |||
Intuitively, you can identify off the bat that the factorial is probably going to dominate. Proving it mathematically: | |||
<math> | |||
a = 16, b = 4, c = \log_{b}{a} = 2 | |||
</math> | |||
Now we compare <math>f(n) = n!</math> to <math>n^c = n^2</math>: | |||
<math> | |||
f(n) = \Omega(n^2) | |||
</math> | |||
We have case 3, so we check the regularity condition: | |||
<math> | |||
16 f(\frac{n}{4}) \leq c f(n) | |||
</math> | |||
<math> | |||
16 \left( \dfrac{n}{4} \right)! \leq c n! | |||
</math> | |||
This holds for <math>n>4</math>, since <math>4! > 16</math> | |||
Therefore, final answer is: | |||
Case 3 | |||
<math> | |||
T(n) = \Theta(n!) | |||
</math> | |||
===Example 6=== | |||
<math> | |||
T(n) = \sqrt{2} \cdot T \left( \dfrac{n}{2} \right) + \log{n} | |||
</math> | |||
<math> | |||
a = \sqrt{2} = 2^{\frac{1}{2}}, b = 2, \log_{2}{2^{\frac{1}{2}}} = \frac{1}{2} | |||
</math> | |||
Now we compare <math>f(n) = \log{n}</math> with <math>n^c = n^{\frac{1}{2}}</math> | |||
If we compare the graphs of these two functions, the square root grows slightly faster: <math>\log{n} = O(\sqrt{n})</math> | |||
That means <math>n^c</math> provides an upper bound, and <math>f(n) = O(n^c) = O(\sqrt{n})</math> | |||
Final answer: | |||
Case 1 | |||
<math> | |||
T(n) = \Theta(\sqrt{n}) | |||
</math> | |||
===Example 7=== | |||
<math> | |||
T(n) = 3 T \left( \frac{n}{2} \right) + n | |||
</math> | |||
Using values of a and b, <math>\log_{b}{a} = \log_2{3} \approx 1.5</math> | |||
Now we compare <math>f(n) = n</math> with <math>n^{\log_2{3}}</math> | |||
<math> | |||
f(n) \leq n^c | |||
</math> | |||
Therefore we have Case 1: | |||
<math> | |||
T(n) = \Theta(n^{\log_2{3}}) | |||
</math> | |||
=Flags= | =Flags= | ||
{{AlgorithmsFlag}} | {{AlgorithmsFlag}} | ||
[[Category:Divide and Conquer]] | |||
[[Category:Master Theorem]] | |||
Latest revision as of 00:16, 15 June 2026
Statement of Master Theorem
The Master Theorem is used to analyze three distinct cases for divide-and-conquer recurrences:
Case 1: Subproblems Dominate (Leaf-Heavy)
If $ f(n) = O( n^{\log_b a - \epsilon} ) $ for some constant $ \epsilon > 0 $ then $ T(n) = \Theta( n^{\log_b a} ) $
Case 2: Split/Recombine ~ Subproblems
If $ f(n) = \Theta( n^{\log_b a} \log^k n ) $, then $ T(n) = \Theta( n^{\log_b a} \log^{k+1} n ) $ (note that usually $ k=0 $)
Case 3: Split/Recombine Dominates (Root Heavy)
If $ f(n) = \Omega( n^{\log_b a + \epsilon} ) $ for some constant $ \epsilon > 0 $ , and if $ a f(n/b) \leq cf(n) $ for some $ c < 1 $, then $ T(n) = \Theta( f(n) ) $
Revisiting Big O, Big Theta, Big Omega
Big O
When we say
$ f(n) = O(g(n)) $
what we mean is, there are some constants $ c > 0, n_0 > 0 $ such that
$ 0 \leq f(n) \leq c g(n) $
for all sufficiently large $ n \geq n_0 $
We say that f(n) is bounded above by g(n).
Example: $ 2n^2 = O(n^3) $
Big O corresponds roughly to less than or equal to
Note that this equal sign notation is NOT symmetric - $ n^3 \neq O(n^2) $
We can also think about a set definition - f(n) is in some set of functions that are like g(n)
O(g(n)) can be defined as a set of functions h(n) such that:
$ \{ h(n) : \exists c > 0, n_0 > 0, 0 \leq h(n) \leq c g(n) \} $
Macro Convention
Macro convention - a set in a function represents an anonymous function in that set.
Note that this observation is the key mathematical underpinning of functional programming and function-based languages.
Example:
$ f(n) = n^3 + O(n^2) $
This means there is a function h(n) in the set O(n^2) such that f(n) = n^3 + h(n)
they represent SOME func in that set
Can also read statements as having a lead "for all" and read the equal sign as "is":
$ n^2 + O(n) = O(n^2) $
is a true statement, but the reverse is not true.
This means, for any f(n) in O(n), there exists some function g(n) in O(n^2) such that if you add n^2 to the function f(n), you get g(n).
Big Omega Notation
$ f(n) = \Omega( g(n) ) $
means f(n) is AT LEAST cost g(n),
$ \exists c > 0, n_0 > 0 : 0 \leq c g(n) \leq f(n) $
for sufficiently large n.
Example: $ \sqrt{n} = \Omega(\log{n}) $
Big Theta Notation
$ f(n) = \Theta( g(n) ) = \Omega( g(n) ) \cap O(g(n)) $
means f(n) grows the same as g(n):
$ \exists c_1 > 0, c_2 > 0, n_0 > 0 : c_1 g(n) \leq f(n) \leq c_2 g(n) $
for $ n \geq n_0 $
Examples
Example 1
Suppose we have a recursion relation:
$ T(n) = 2T(\frac{n}{2}) + 10n $
For the master theorem, we start by evaluating log_b(a)
$ \log_{b}{a} = \log_{2}{2} = 1 $
$ n^c = n $
Comparing this with $ f(n) = 10n $ we find that these two functions have the same form. Specifically, we have:
$ f(n) = \Theta( n^c \log^{k} n ) $
for c = 1 and k = 0.
Now our final statement is that we have case 2 (split/merge and subproblem solution steps are balanced):
$ f(n) = \Theta( n \log n) $
This corresponds to the case where the function $ n^c $, which represents the computational cost of solving the subproblems, grows at the same rate as the merge step, f(n), which in this case was 10n.
Example 2
$ T(n) = 2 T(\frac{n}{2}) + \frac{n}{\log n} $
In this case, the Master Theorem does not apply - there is a non-polynomial difference between $ f(n) $ and $ n^{\log_b{a}} $
Example 3
$ T(n) = 2T(\frac{n}{4}) + n^{0.51} $
We start with calculating c = log_b a:
$ c = \log_b{a} = \log_{4}{2} = \frac{1}{2} $
So now we compare $ f(n) = n^{0.51} $ to $ n^c = \sqrt{n} $
We can see that
$ f(n) = \Omega(n^{0.5}) $
so we have Case 3
We need to check the regularity condition:
$ 2 f( \frac{n}{4} ) \leq c f(n) $
$ \frac{2}{4^{0.51}} n^{0.51} < c n^{0.51} $
which is true.
So the final answer is, Case 3:
$ T(n) = \Theta(n^{0.51}) $
Example 4
$ T(n) = 0.5 T(\frac{n}{2}) + \frac{1}{n} $
Careful - this case does not apply! $ a < 1 $ violates one of the assumptions of the theorem.
Example 5
$ T(n) = 16 T(\frac{n}{4}) + n! $
Intuitively, you can identify off the bat that the factorial is probably going to dominate. Proving it mathematically:
$ a = 16, b = 4, c = \log_{b}{a} = 2 $
Now we compare $ f(n) = n! $ to $ n^c = n^2 $:
$ f(n) = \Omega(n^2) $
We have case 3, so we check the regularity condition:
$ 16 f(\frac{n}{4}) \leq c f(n) $
$ 16 \left( \dfrac{n}{4} \right)! \leq c n! $
This holds for $ n>4 $, since $ 4! > 16 $
Therefore, final answer is:
Case 3
$ T(n) = \Theta(n!) $
Example 6
$ T(n) = \sqrt{2} \cdot T \left( \dfrac{n}{2} \right) + \log{n} $
$ a = \sqrt{2} = 2^{\frac{1}{2}}, b = 2, \log_{2}{2^{\frac{1}{2}}} = \frac{1}{2} $
Now we compare $ f(n) = \log{n} $ with $ n^c = n^{\frac{1}{2}} $
If we compare the graphs of these two functions, the square root grows slightly faster: $ \log{n} = O(\sqrt{n}) $
That means $ n^c $ provides an upper bound, and $ f(n) = O(n^c) = O(\sqrt{n}) $
Final answer:
Case 1
$ T(n) = \Theta(\sqrt{n}) $
Example 7
$ T(n) = 3 T \left( \frac{n}{2} \right) + n $
Using values of a and b, $ \log_{b}{a} = \log_2{3} \approx 1.5 $
Now we compare $ f(n) = n $ with $ n^{\log_2{3}} $
$ f(n) \leq n^c $
Therefore we have Case 1:
$ T(n) = \Theta(n^{\log_2{3}}) $
Flags
| Algorithms Part of Computer Science Notes
Series on Algorithms
Algorithms/Sort · Algorithmic Analysis of Sort Functions · Divide and Conquer · Divide and Conquer/Master Theorem Three solid O(n log n) search algorithms: Merge Sort · Heap Sort · Quick Sort Algorithm Analysis/Merge Sort · Algorithm Analysis/Randomized Quick Sort
Algorithms/Search · Binary Search · Binary Search Modifications
Algorithms/Combinatorics · Algorithms/Combinatorics and Heuristics · Algorithms/Optimization · Divide and Conquer
Algorithms/Strings · Algorithm Analysis/Substring Pattern Matching
Algorithm complexity · Theta vs Big O Amortization · Amortization/Aggregate Method · Amortization/Accounting Method Algorithm Analysis/Matrix Multiplication
Estimation Estimation · Estimation/BitsAndBytes
Algorithm Practice and Writeups Project Euler · Five Letter Words · Letter Coverage
|