Recursive Algorithms Complexity: Difference between revisions
Line 13: | Line 13: | ||
As an example, the following recurrence expresses the running time for merge sort: | As an example, the following recurrence expresses the running time for merge sort: | ||
<font size=-1> | <font size=-1> | ||
│ c | │ c if n == 1 | ||
T(n) = │ | T(n) = │ | ||
│ 2T(n/2) + cn | │ 2T(n/2) + cn if n > 1 | ||
</font> | </font> | ||
Revision as of 17:40, 21 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.
As an example, the following recurrence expresses the running time for merge sort:
│ c if n == 1 T(n) = │ │ 2T(n/2) + cn if n > 1
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 Master Method (Master Theorem)