maths.free › Discrete Math & Logic › Asymptotic analysis, recursion trees and the master theorem
Asymptotic analysis, recursion trees and the master theorem
Big O, Omega and Theta, counting steps with sums, divide-and-conquer recurrences, recursion trees and the master theorem.
Running times are compared by their growth, ignoring constant factors and small inputs. We write \( f(n) = O(g(n)) \) if there are constants \( c > 0 \) and \( n_0 \) with \( f(n) \le c\,g(n) \) for all \( n \ge n_0 \); \( f = \Omega(g) \) for the matching lower bound, and \( f = \Theta(g) \) when both hold. For example \( 3n^2 + 10n = O(n^2) \), with witnesses \( c = 4 \) and \( n_0 = 10 \). A convenient test: if \( f(n)/g(n) \) tends to a finite positive limit then \( f = \Theta(g) \), and if it tends to 0 then \( f = O(g) \) but not \( \Theta(g) \). The example confirms that \( n\log n \) grows strictly more slowly than \( n^2 \).
The running time of loops is a sum. A pair of nested loops, the inner one running from \( i + 1 \) to \( n \), performs \( \sum_{i=1}^{n}(n - i) = n(n-1)/2 = \Theta(n^2) \) steps. A loop that halves its variable each time runs about \( \log_2 n \) times, which is why binary search is \( \Theta(\log n) \).
Divide and conquer gives recurrences instead. Merge sort splits the input in half, sorts each half recursively and merges in linear time: \( T(n) = 2T(n/2) + n \). A recursion tree solves it by drawing the calls: the root does \( n \) work, its two children do \( n/2 \) each, the four grandchildren \( n/4 \) each, so every level does \( n \) work in total, and there are \( \log_2 n + 1 \) levels. Hence \( T(n) = \Theta(n\log n) \); exactly, with \( T(1) = 1 \), \( T(2^k) = (k+1)2^k \).
The master theorem packages the recursion tree for \( T(n) = a\,T(n/b) + f(n) \) with \( a \ge 1 \), \( b > 1 \). The number of leaves is \( a^{\log_b n} = n^{\log_b a} \), and the answer depends on how \( f(n) \) compares with \( n^{\log_b a} \). If \( f(n) = O(n^{\log_b a - \varepsilon}) \) for some \( \varepsilon > 0 \), the leaves dominate and \( T = \Theta(n^{\log_b a}) \). If \( f(n) = \Theta(n^{\log_b a}) \), every level contributes equally and \( T = \Theta(n^{\log_b a}\log n) \). If \( f(n) = \Omega(n^{\log_b a + \varepsilon}) \) and the regularity condition \( a\,f(n/b) \le c\,f(n) \) holds for some constant \( c < 1 \) and all large \( n \), the root dominates and \( T = \Theta(f(n)) \). The cases do not cover everything: \( f(n) = n^{\log_b a}\log n \), for instance, falls between the second and third, and needs the recursion tree directly. Karatsuba multiplication, \( T(n) = 3T(n/2) + n \), is in the first case: \( \Theta(n^{\log_2 3}) \approx \Theta(n^{1.585}) \).
Picture it: the recursion tree as a triangle of work. If the levels get heavier towards the bottom, the leaves win; if they are all equal, the total is one level times the height; if they get lighter, the root wins.
Think it: constant factors matter in practice and asymptotics matter at scale. An algorithm that is \( \Theta(n^2) \) with a small constant can beat a \( \Theta(n\log n) \) one on short inputs, which is why library sorts switch to insertion sort on small pieces. But the growth rate decides what is feasible for large \( n \), and no constant factor rescues an exponential algorithm.
Worked example · limit of (n log n)/n^2 as n -> oo
Step by step
- \lim_{n \to \infty^+-} \frac{\log{\left(n \right)}}{n}
Try direct substitution first.
- \
As x grows without bound, compare the fastest-growing terms (or divide top and bottom by the highest power).
- = 0
Take the limit.
Reveal the answer
Now you Pick a problem, or type or draw your own. Every step, a picture, the answer hidden until you ask.
Symbols used here
Tap any symbol for the full definition, a picture, and what every letter in it means.
How to: Asymptotic analysis, recursion trees and the master theorem
- For loops, write the number of steps as a sum and find its closed form or its leading term.
- For a recursive algorithm, write T(n) = a T(n/b) + f(n) from the number of calls, their size, and the work outside them.
- Compute the critical exponent log_b a and compare f(n) with n^(log_b a).
- Apply the matching case of the master theorem, or sum the levels of the recursion tree directly.
- State the answer in Theta notation, and check it on a small value of n.
Questions people ask
Does the base of the logarithm matter in O(log n)?
No. Logarithms to different bases differ by a constant factor, log_b n = ln n / ln b, and O ignores constant factors.
What if the recurrence does not fit the master theorem?
Draw the recursion tree and add up the work level by level, or guess a bound and prove it by induction. Recurrences with unequal splits, like T(n) = T(n/3) + T(2n/3) + n, are handled that way.
What makes mathematics "discrete"?
It deals with separate, countable objects (integers, graphs, statements) rather than continuous quantities. No limits, no infinitesimals; instead induction, counting and logic.
How does a proof by induction work?
Show the statement for the first case, then show that whenever it holds for n it holds for n + 1. Like dominoes: the first falls, and each knocks over the next.
What order should I take these lessons in?
Logic and proof first, because every later lesson proves things. Then induction, sets, relations and functions, counting, recurrences, graphs, Boolean algebra and automata, and finally algorithms and complexity, which use nearly everything before them.
Is discrete mathematics the same as the maths of computer science?
Largely. Data structures are graphs and trees, correctness arguments are induction, running times are recurrences and sums, circuits are Boolean algebra, and what a computer can do efficiently is the subject of the last lessons. It is also ordinary mathematics in its own right.
Do I need calculus for this course?
No. School algebra is enough. A little familiarity with limits helps when comparing growth rates, and the lesson on asymptotic analysis explains the one limit fact it uses.
More in Discrete Math & Logic
Truth tablesSums and inductionProof by inductionAlgorithms and growth of functionsPredicate logic and quantifiersDirect proof, contrapositive and contradictionStrong induction and the well-ordering principleRelations: equivalence relations and partial ordersFunctions and cardinalityCounting: rules, choices and bijectionsThe pigeonhole principleSolving linear recurrence relationsGraphs: degrees, paths and connectivityTrees and spanning trees