$\def\gets{:=} \def\N{\mathbb{N}} \def\Z{\mathbb{Z}} \def\R{\mathbb{R}} \def\Q{\mathbb{Q}} \def\O{\mathrm{O}} \def\Prop{\mathbb{P}} \def\Algorithm{\textbf{Algorithm}~} \def\Subroutine{\textbf{Subroutine}~} \def\Let{\textbf{Let}~} \def\Find{\textbf{Find}~} \def\Compute{\textbf{Compute}~} \def\For{\textbf{For}~} \def\While{\textbf{While}~} \def\If{\textbf{If}~} \def\Else{\textbf{Else}~} \def\ElseIf{\textbf{Else If}~} \def\Return{\textbf{Return}~} \def\return{~\textbf{return}~} \def\Output{\textbf{Output}~} \def\output{~\textbf{output}~} \def\tot{~\textrm{to}~} \def\andt{~\textrm{and}~} \def\ort{~\textrm{or}~} \def\Forallt{\textbf{For all}~} \def\forallt{~\textbf{for all}~} \def\Foreacht{\textbf{For each}~} \def\foreacht{~\textbf{for each}~} \newcommand{\abs}{|#1|} \newcommand{\lrabs}{\left|#1\right|} \newcommand{\parens}{\left(#1\right)} \newcommand{\brackets}{\left[#1\right]} \newcommand{\floor}{\left\lfloor#1\right\rfloor} \newcommand{\ceil}{\left\lceil#1\right\rceil} \def\gcd{\mathrm{gcd}} \def\qst{q_{start}} \def\qacc{q_{accept}} \def\qrej{q_{reject}} \newcommand{\lang}{L_\mathrm{#1}} \def\atm{\lang{ACC}} \def\htm{\lang{HALT}} \def\ehtm{\lang{\varepsilon\text{-}HALT}} \def\eqtm{\lang{EQ}} \def\etm{\lang{\varnothing}} \def\oninput{\text{ on input }} \def\oninputs{\text{ on inputs }} \def\Run{\textbf{Run}~} \def\ont{~\textrm{on}~} \def\Accept{\textbf{Accept}~} \def\accept{~\textbf{accept}~} \def\accepts{~\textrm{accepts}~} \def\Reject{\textbf{Reject}~} \def\reject{~\textbf{reject}~} \def\rejects{~\textrm{rejects}~} \def\Halt{\textbf{Halt}~} \def\halt{~\textbf{halt}~} \newcommand{\inner}{\langle #1 \rangle} \def\Tr{\le_\mathrm{T}} \def\pmr{\le_\mathrm{p}} \def\Construct{\textbf{Construct}~} \def\bquote{\text{"}} \def\equote{\text{"}} \def\RD{\mathsf{R}} \def\RE{\mathsf{RE}} \def\coRE{\mathsf{coRE}} \def\DTIME{\mathsf{DTIME}} \def\P{\mathsf{P}} \def\NP{\mathsf{NP}} \def\coNP{\mathsf{coNP}} \def\MAZE{\mathrm{MAZE}} \def\TSP{\mathrm{TSP}} \def\SAT{\mathrm{SAT}} \def\TSAT{\mathrm{3SAT}} \def\VC{\mathrm{VERTEX\text{-}COVER}} \def\SC{\mathrm{SET\text{-}COVER}} \def\HC{\mathrm{HAMCYCLE}} \def\HP{\mathrm{HAMPATH}} \def\IS{\mathrm{INDEPENDENT\text{-}SET}} \def\CLIQUE{\mathrm{CLIQUE}} \def\SSUM{\mathrm{SUBSET\text{-}SUM}} \def\KNAPSACK{\mathrm{KNAPSACK}} \def\MAXCUT{\mathrm{MAX\text{-}CUT}} \def\Ex{\mathbb{E}} \def\Var{\mathrm{Var}} \newcommand{\lrPr}{\Pr\brackets{#1}} \newcommand{\lrEx}{\Ex\brackets{#1}} \newcommand{\lrVar}{\Var\parens{#1}} \newcommand{\lrexp}{\exp\parens{#1}} \def\MSAT{\mathrm{Max}\text{-}\mathrm{E3SAT}} \def\PRIMES{\mathrm{PRIMES}} \def\RP{\mathsf{RP}} \def\coRP{\mathsf{coRP}} \def\BPP{\mathsf{BPP}} \def\ZPP{\mathsf{ZPP}} \def\Xj{X^{(j)}} \def\BQP{\mathsf{BQP}} \def\NPI{\mathsf{NPI}} \newcommand{\expp}{\exp\left(#1\right)}$

# Supplemental: Algorithms

## Non-master-theorem Recurrences

Recurrences that do not match the pattern required by the master theorem can often be manipulated to do so using substitutions. We take a look at two examples here.

Example 192

Consider the following recurrence:

$T(n) = 2T\parens{\frac{n}{2}} + \O(n \log n)$

While we solved this recurrence above using the master theorem with log factors, we can also do so through substitution. We perform the substitution $$S(n) = T(n)/\log{n}$$ to get the following:

$\begin{split}S(n) \log n &= 2S\parens{\frac{n}{2}}\log{\frac{n}{2}} + \O(n \log n)\\ &= 2S\parens{\frac{n}{2}}(\log n - \log 2) + \O(n \log n)\\ &= 2S\parens{\frac{n}{2}}(\log n - 1) + \O(n \log n)\end{split}$

To get this, we substituted $$T(n) = S(n)\log{n}$$ and $$T(n/2) = S(n/2)\log(n/2)$$ in the first step. Since $$\log n - 1 \le \log n$$, we can turn the above into the inequality:

$S(n)\log n \le 2S\parens{\frac{n}{2}}\log n + \O(n \log n)$

We can then divide out the $$\log n$$ from each term to get:

$S(n) \le 2S\parens{\frac{n}{2}} + \O(n)$

We now have something that is in the right form for applying the master theorem. Even though it is an inequality rather than an equality, because we are computing an upper bound, we can still apply the master theorem. We have $$k/b^d = 2/2^1 = 1$$, so we get:

$S(n) = \O(n \log n)$

We can then undo the substitution $$S(n) = T(n)/\log{n}$$ to get:

$\begin{split}T(n) &= S(n)\log n = \O(n \log n)\log n\\ &= \O(n \log^2 n)\end{split}$
Exercise 193

In Example 192, we assumed that if $$f(n) = \O(\log n)$$, then $$f(n)/\log{n} = \O(1)$$. Prove from the definition of big-O that this holds.