Master Method: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 47: Line 47:
* [[Merge_Sort#Time_Complexity_Analysis|Merge Sort]]
* [[Merge_Sort#Time_Complexity_Analysis|Merge Sort]]
* [[Binary_Search#Running_Time_Analysis_with_Master_Theorem|Binary search on a sorted array]]
* [[Binary_Search#Running_Time_Analysis_with_Master_Theorem|Binary search on a sorted array]]
* [[Karatsuba multiplication algorithm]]
* [[Karatsuba_Multiplication#Time_Complexity|Karatsuba multiplication algorithm]]





Revision as of 18:41, 21 September 2021

Internal

Overview

The master method or master theorem is a general mathematical tool for analyzing the running time of divide and conquer algorithms that divide a problem in equal subproblems and call themselves recursively. The fact that the subproblems are equal in size is important: master method does not apply to divide and conquer algorithms that divide the problem in unequal parts. There are generalization of the master method that apply to unbalanced problems, but they are not addressed in this article.

Also, the master theorem offers running time predictions as Θ() - bounded from above and below - as long as the running time for the combine phase of the divide and conquer algorithm is offered as Θ(). If the running time for the combine phase of the algorithm is given as O(), then the master theorem can be used to predict O().

Master Method and Recurrences

Master method can be applied to predict running time for recursive algorithms whose running time can be expressed as a recurrence (or recurrence equation). The first step in applying the master method is to identify the recurrence that characterizes the algorithm.

We denote the maximum number of operations the algorithm needs to solve the computational problem for a problem of size n with T(n). This is the quantity we want an upper bound for. The recurrence is a way to express T(n) in terms of T() of smaller numbers, specifically the size of the subproblems the recursive algorithm calls itself recursively on.

Every recurrence has two ingredients:

  • a base case that describes the running time where there is no further recursion.
  • the general case, that describes the running time done by the recursive calls invoked at that level plus the work that is done by the current level.

In the most generic case, a recurrence looks like this:

       │ ≤ c               # base case, running time bounded by a constant for sufficiently small n
T(n) = │         
       │ ≤ aT(n/b) + O(nd) # general case, for larger n

For the general case:

  • a is the number of subproblems (recursive calls).
  • b is the factor by the input size shrinks before the recursive call is applied. b is constant bigger than 1. For example, if we call recursively on each half of the current problem, then b is 2.
  • d is the exponent in the running time of the "combine" phase, the amount of work that is done outside the recursive calls. d can be zero or larger.

The fact that we express the subproblem size as n/b shows that for master theorem to apply, all subproblems must have the same size.

Formal Definition

       │ O(ndlog n)  if a = bd # Case 1
       │ 
T(n) = │ O(nd)       if a < bd # Case 2
       │ 
       │ O(nlogba)    if a > bd # Case 3

Intuition

The conditions expressed in terms of a and bd can be understood intuitively as follows:

Proof

Examples







TODO

is a method of analyzing running time of recursive algorithms by using the "master theorem". The master theorem applies to algorithms whose running times can be expressed as a A generic form of a recurrence equation is:

T(n) = a⋅T(n/b) + f(n)

where a ≥ 1, b > 1 and f(n) is a function that approximates the non-recursive cost. Note that the above expression does include a boundary condition for n = 1. The reasons is that T(1) does not significantly change the solution to the recurrence, it may change it with a constant factor, but the order of growth will stay unchanged.

TODO CLRS page 93.

TODO integrate after class:


Integrate class https://www.coursera.org/learn/algorithms-divide-conquer/lecture/HkcdO/formal-statement


Analyzing Divide-and-Conquer Algorithms

TODO integrate after class:

For divide-and-conquer recursive problems, where solving the problem involves dividing the problem into a subproblems of size n/b of the original', applying the algorithm recursively a times on n/b-size problems and then combining the result, the run time can be expressed by the generic recurrence presented below. If the problem size is small enough (n ≤ c) the straightforward solution takes constant time, which is expressed as a constant function Θ(1).

       | Θ(1)                     if n ≤ c
T(n) = |
       | aT(n/b) + D(n) + C(n)    otherwise

where the D(n) is the cost of dividing the n-size problem into a subproblems, and the C(n) is the cost of combining the results.

[Next in Algorithm Complexity]