Lably
Skip to content

Master theorem (analysis of algorithms)

Tool for analyzing divide-and-conquer algorithms

  • 4 min read
  • 4 chapters

Related on Lably

Through Recurrence relation, Recursion (computer science) and Big O notation

Not all recurrence relations can be solved by this theorem; its generalizations include the Akra–Bazzi method.

Introduction

Consider a problem that can be solved using a recursive algorithm such as the following:

<b id="mwHg">procedure</b> p(input <i id="mwHw">x</i> of size <i id="mwIA">n</i>): <b id="mwIQ">if</b> <i id="mwIg">n</i> < some constant <i id="mwIw">k</i>: Solve <i id="mwJA">x</i> directly without recursion <b id="mwJQ">else</b>: Create <i id="mwJg">a</i> subproblems of <i id="mwJw">x</i>, each having size <i id="mwKA">n</i>/<i id="mwKQ">b</i> Call procedure <i id="mwKg">p</i> recursively on each subproblem Combine the results from the subproblems

The above algorithm divides the problem into a number (a) of subproblems recursively, each subproblem being of size n/b. The factor by which the size of subproblems is reduced (b) need not, in general, be the same as the number of subproblems (a). Its solution tree has a node for each recursive call, with the children of that node being the other calls made from that call. The leaves of the tree are the base cases of the recursion, the subproblems (of size less than k) that do not recurse. The above example would have a child nodes at each non-leaf node. Each node does an amount of work that corresponds to the size of the subproblem n passed to that instance of the recursive call and given by f(n). The total amount of work done by the entire algorithm is the sum of the work performed by all the nodes in the tree.

The runtime of an algorithm such as the p above on an input of size n, usually denoted T(n), can be expressed by the recurrence relation

T(n)=aT(nb)+f(n),

where f(n) is the time to create the subproblems and combine their results in the above procedure. This equation can be successively substituted into itself and expanded to obtain an expression for the total amount of work done. The master theorem allows many recurrence relations of this form to be converted to Θ-notation directly, without doing an expansion of the recursive relation.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication