Recursive Algorithms Complexity: Difference between revisions
No edit summary |
|||
Line 11: | Line 11: | ||
A '''recurrence''', or a <span id='Recurrence_Equation'></span>'''recurrence equation''', is an equation or an inequality that describe the running time of a recursive algorithm for a problem of size n in terms of the running time on smaller inputs. Once the running time is expressed this way, we can use mathematical tools or other methods to [[#Solving_Recurrences|solve the recurrence]], which means obtaining asymptotic bounds on the running time of the algorithm. | A '''recurrence''', or a <span id='Recurrence_Equation'></span>'''recurrence equation''', is an equation or an inequality that describe the running time of a recursive algorithm for a problem of size n in terms of the running time on smaller inputs. Once the running time is expressed this way, we can use mathematical tools or other methods to [[#Solving_Recurrences|solve the recurrence]], which means obtaining asymptotic bounds on the running time of the algorithm. | ||
=Solving Recurrences= | |||
There are at least three method to solve recurrences: | There are at least three method to solve recurrences: | ||
==Substitution Method== | |||
The method consists in guessing a bound and then using [[Mathematical_Induction|mathematical induction]] on the [[#Recurrence|recurrence]] to prove the solution correct. We can guess a solution using a [[#Recursion_Tree|recursion tree]]. <font color=darkgray>Examples on how to use mathematical induction to prove the bound is correct are available in [[CLRS]] page 83.</font> | The method consists in guessing a bound and then using [[Mathematical_Induction|mathematical induction]] on the [[#Recurrence|recurrence]] to prove the solution correct. We can guess a solution using a [[#Recursion_Tree|recursion tree]]. <font color=darkgray>Examples on how to use mathematical induction to prove the bound is correct are available in [[CLRS]] page 83.</font> | ||
==<span id='Recursion_Tree'></span>Recursion-Tree Method== | |||
The method consists in converting the recurrence into graphical tree representation whose nodes represent costs incurred at various level of the recursion. After we lay out the tree, we sum the costs within each level of the tree to obtain a set of per-level costs, then we sum all the per-level costs to determine the total cost of the recursion. The bound such guessed can be proven with the [[#Substitution_Method|substitution method]]. <font color=darkgray>Examples on how to build recursion trees are available in [[CLRS]] page 37 and page 88.</font> | The method consists in converting the recurrence into graphical tree representation whose nodes represent costs incurred at various level of the recursion. After we lay out the tree, we sum the costs within each level of the tree to obtain a set of per-level costs, then we sum all the per-level costs to determine the total cost of the recursion. The bound such guessed can be proven with the [[#Substitution_Method|substitution method]]. <font color=darkgray>Examples on how to build recursion trees are available in [[CLRS]] page 37 and page 88.</font> | ||
==<span id='The_Master_Theorem'></span><span id='Master_Theorem'></span>The Master Method (Master Theorem)== | |||
The <span id='Master_Method'></span>'''master method''' 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 [[#Recurrence_Equation|recurrence equation]]. A generic form of a recurrence equation is: | The <span id='Master_Method'></span>'''master method''' 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 [[#Recurrence_Equation|recurrence equation]]. A generic form of a recurrence equation is: |
Revision as of 17:22, 20 September 2021
Internal
Overview
The running time of recursive algorithms can be analyzed using recurrences.
Recurrence
A recurrence, or a recurrence equation, is an equation or an inequality that describe the running time of a recursive algorithm for a problem of size n in terms of the running time on smaller inputs. Once the running time is expressed this way, we can use mathematical tools or other methods to solve the recurrence, which means obtaining asymptotic bounds on the running time of the algorithm.
Solving Recurrences
There are at least three method to solve recurrences:
Substitution Method
The method consists in guessing a bound and then using mathematical induction on the recurrence to prove the solution correct. We can guess a solution using a recursion tree. Examples on how to use mathematical induction to prove the bound is correct are available in CLRS page 83.
Recursion-Tree Method
The method consists in converting the recurrence into graphical tree representation whose nodes represent costs incurred at various level of the recursion. After we lay out the tree, we sum the costs within each level of the tree to obtain a set of per-level costs, then we sum all the per-level costs to determine the total cost of the recursion. The bound such guessed can be proven with the substitution method. Examples on how to build recursion trees are available in CLRS page 37 and page 88.
The Master Method (Master Theorem)
The master method 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 recurrence equation. 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:
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.