{
  "id": "master-theorem",
  "title": "Master Theorem",
  "lang": "en",
  "source_lang": "en",
  "summary": "The master theorem gives asymptotic bounds for divide-and-conquer recurrences by comparing nonrecursive work with the growth generated by recursive branching.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Master method",
    "Divide-and-conquer master theorem"
  ],
  "generated_at": "2026-11-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/master-theorem/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/master-theorem/",
      "json": "https://aiwiki.page/zh/master-theorem.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "The master theorem is a result in the analysis of algorithms that gives asymptotically tight bounds for many recurrence relations of the form\n\n\\[T(n)=aT(n/b)+f(n).\\]\n\nSuch recurrences commonly describe the time complexity of divide-and-conquer algorithms: a problem of size \\(n\\) produces \\(a\\) smaller problems, each of size \\(n/b\\), while \\(f(n)\\) represents the work done outside the recursive calls. The theorem compares \\(f(n)\\) with \\(n^{\\log_b a}\\), the growth associated with the recursive branching, and identifies whether the total cost is dominated by small subproblems, distributed across recursion levels, or dominated by work near the original problem."
    },
    {
      "heading": "Recurrence model and assumptions",
      "text": "In the standard formulation:\n\n- \\(a\\geq 1\\) is a constant representing the number of recursive subproblems.\n- \\(b>1\\) is a constant representing the reduction in problem size.\n- \\(f(n)\\) is an asymptotically nonnegative function describing nonrecursive work, including splitting, combining, and other overhead.\n- \\(T(n)=\\Theta(1)\\) for input sizes below a fixed positive threshold.\n\nIn an algorithm, \\(a\\) is normally an integer, although the mathematical recurrence can be studied for real \\(a\\geq 1\\).\n\nThe notation \\(O\\), \\(\\Omega\\), and \\(\\Theta\\) belongs to asymptotic notation. Respectively, these express an asymptotic upper bound, lower bound, and matching upper and lower bounds, up to constant factors.\n\nFor a clean statement, \\(n\\) may be restricted to powers of \\(b\\), so that repeated division reaches the base case exactly. Algorithmic recurrences often instead use floors or ceilings, such as\n\n\\[T(n)=aT(\\lfloor n/b\\rfloor)+f(n).\\]\n\nThese rounding changes preserve the usual bounds under standard regularity assumptions, but an arbitrary recurrence with irregular costs should not be treated as automatically equivalent to the unrounded version.\n\nThe theorem applies to the recurrence that has actually been established. An inequality such as\n\n\\[T(n)\\leq aT(n/b)+f(n)\\]\n\ncan support an upper bound, but does not by itself establish a matching lower bound."
    },
    {
      "heading": "Standard three-case theorem",
      "text": "Define the critical exponent\n\n\\[p=\\log_b a.\\]\n\nThe simplest standard version has three cases.\n\nCase 1: Recursive branching dominates\n\nIf there exists a constant \\(\\varepsilon>0\\) such that\n\n\\[f(n)=O\\!\\left(n^{p-\\varepsilon}\\right),\\]\n\nthen\n\n\\[T(n)=\\Theta(n^p).\\]\n\nHere \\(f(n)\\) is smaller than \\(n^p\\) by a polynomial factor. The work near the leaves of the recursion dominates the total.\n\nFor example,\n\n\\[T(n)=4T(n/2)+n.\\]\n\nSince \\(p=\\log_2 4=2\\), and \\(n=O(n^{2-1})\\),\n\n\\[T(n)=\\Theta(n^2).\\]\n\nCase 2: Work is balanced across levels\n\nIf\n\n\\[f(n)=\\Theta(n^p),\\]\n\nthen\n\n\\[T(n)=\\Theta(n^p\\log n).\\]\n\nEach recursion level contributes the same asymptotic amount of work, and there are \\(\\Theta(\\log n)\\) levels.\n\nA frequently taught extension includes logarithmic factors: if\n\n\\[f(n)=\\Theta\\!\\left(n^p(\\log n)^k\\right)\\]\n\nfor a constant \\(k\\geq 0\\), then\n\n\\[T(n)=\\Theta\\!\\left(n^p(\\log n)^{k+1}\\right).\\]\n\nFor instance,\n\n\\[T(n)=2T(n/2)+n\\log n\\]\n\nhas \\(p=1\\) and \\(k=1\\), giving\n\n\\[T(n)=\\Theta(n\\log^2 n).\\]\n\nCase 3: Nonrecursive work dominates\n\nIf there exists a constant \\(\\varepsilon>0\\) such that\n\n\\[f(n)=\\Omega\\!\\left(n^{p+\\varepsilon}\\right),\\]\n\nand there exists a constant \\(c<1\\) such that, for all sufficiently large \\(n\\),\n\n\\[a f(n/b)\\leq c f(n),\\]\n\nthen\n\n\\[T(n)=\\Theta(f(n)).\\]\n\nThe second condition is the regularity condition. It ensures that the combined nonrecursive work decreases geometrically as the recursion descends. A polynomial lower bound on \\(f(n)\\) alone does not guarantee this behavior for an irregular function.\n\nFor example,\n\n\\[T(n)=2T(n/2)+n^2.\\]\n\nHere \\(p=1\\), and\n\n\\[2f(n/2)=2(n/2)^2=\\frac12 n^2.\\]\n\nThus the regularity condition holds with \\(c=1/2\\), and\n\n\\[T(n)=\\Theta(n^2).\\]"
    },
    {
      "heading": "Why the theorem works",
      "text": "The theorem can be understood using a recursion tree, which represents the calls made by a recursive computation.\n\nFor simplicity, let \\(n=b^L\\), and stop the recurrence at size \\(1\\). At depth \\(i\\):\n\n- there are \\(a^i\\) subproblems;\n- each subproblem has size \\(n/b^i\\);\n- the total nonrecursive work is \\(a^i f(n/b^i)\\).\n\nExpanding the recurrence gives\n\n\\[T(n)=a^L T(1)+\\sum_{i=0}^{L-1}a^i f(n/b^i).\\]\n\nBecause \\(L=\\log_b n\\),\n\n\\[a^L=n^{\\log_b a}=n^p.\\]\n\nThe leaves therefore contribute \\(\\Theta(n^p)\\).\n\nIf \\(f(n)=\\Theta(n^q)\\), then the cost at level \\(i\\) is\n\n\\[\\Theta\\!\\left(n^q\\left(\\frac{a}{b^q}\\right)^i\\right).\\]\n\nThe resulting geometric series has three possible behaviors:\n\n- If \\(q<p\\), its terms grow with depth, so the bottom levels dominate.\n- If \\(q=p\\), its terms have equal asymptotic size, adding a logarithmic factor.\n- If \\(q>p\\), its terms decrease with depth, so the top levels dominate.\n\nThe theorem generalizes this comparison beyond exact power functions."
    },
    {
      "heading": "Applications",
      "text": "Binary search\n\nThe usual worst-case recurrence for binary search is\n\n\\[T(n)=T(n/2)+\\Theta(1).\\]\n\nHere \\(a=1\\), \\(b=2\\), and \\(p=0\\). The nonrecursive work matches \\(n^0=1\\), so Case 2 gives\n\n\\[T(n)=\\Theta(\\log n).\\]\n\nMerge sort\n\nMerge sort recursively sorts two approximately equal halves and merges them in linear time:\n\n\\[T(n)=2T(n/2)+\\Theta(n).\\]\n\nSince \\(p=1\\), Case 2 yields\n\n\\[T(n)=\\Theta(n\\log n).\\]\n\nClassical recursive matrix multiplication\n\nA direct recursive method for multiplying two square matrices performs eight multiplications on half-sized matrices, together with quadratic addition work:\n\n\\[T(n)=8T(n/2)+\\Theta(n^2).\\]\n\nHere \\(p=\\log_2 8=3\\), so Case 1 gives\n\n\\[T(n)=\\Theta(n^3).\\]\n\nThis recurrence describes that particular method, not a lower bound for every matrix multiplication algorithm."
    },
    {
      "heading": "Logarithmic boundary cases",
      "text": "The polynomial-gap requirements in Cases 1 and 3 are essential. A function can be smaller or larger than \\(n^p\\) without differing from it by a polynomial factor.\n\nFor example,\n\n\\[T(n)=2T(n/2)+\\frac{n}{\\log n},\\]\n\nwith a constant-size cutoff above \\(1\\), does not fit any of the three basic cases:\n\n- \\(n/\\log n\\) is not polynomially smaller than \\(n\\);\n- it is not \\(\\Theta(n)\\);\n- it is not polynomially larger than \\(n\\).\n\nIts solution is\n\n\\[T(n)=\\Theta(n\\log\\log n).\\]\n\nA broader logarithmic extension handles\n\n\\[f(n)=\\Theta\\!\\left(n^p(\\log n)^k\\right)\\]\n\nfor any fixed real \\(k\\). Under the same constant-size base-case assumptions,\n\n\\[T(n)= \\begin{cases} \\Theta\\!\\left(n^p(\\log n)^{k+1}\\right), & k>-1,\\\\[4pt] \\Theta(n^p\\log\\log n), & k=-1,\\\\[4pt] \\Theta(n^p), & k<-1. \\end{cases}\\]\n\nThese bounds follow by summing the level costs; they extend, rather than contradict, the basic theorem."
    },
    {
      "heading": "Limitations and related methods",
      "text": "The master theorem is not a universal method for solving recurrences. Its standard form does not directly cover:\n\n- Unequal subproblem sizes, such as\n\n\\[T(n)=T(n/3)+T(2n/3)+n.\\]\n- Subtractive size reductions, such as\n\n\\[T(n)=T(n-1)+n.\\]\n- A nonconstant number of recursive calls or size reductions that depend on \\(n\\).\n- Irregular toll functions that fail the required comparisons or regularity condition.\n- Recurrences involving many possible partition sizes, such as the expected-time recurrence for quicksort.\n\nThe Akra–Bazzi theorem handles a broader class of divide-and-conquer recurrences, including many with unequal subproblem sizes. Other approaches include repeated expansion, recursion-tree summation, and proving a proposed bound by induction.\n\nThe master theorem measures the resource represented by \\(T(n)\\); it does not determine that resource automatically. In particular, adding the costs of recursive calls models total work, not necessarily elapsed time in parallel computing. Likewise, the running-time recurrence does not directly determine space complexity, because recursive subproblems may execute sequentially and reuse memory."
    }
  ],
  "links": [
    "algorithm",
    "recurrence-relation",
    "time-complexity",
    "divide-and-conquer",
    "recursion",
    "geometric-series",
    "binary-search",
    "merge-sort",
    "matrix",
    "quicksort",
    "parallel-computing",
    "space-complexity"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}