Solving Recurrences

advertisement
Solving Recurrences
Since divide-n-conquer approach produces recursive code, we need to somehow calculate
the running time of recursive code such as T(n) = 2T(n/2)+n (this example is for
MergeSort).
There are other recursive algorithms that are not divide-n-conquer, and there are special
cases of divide-n-conquer, which produce formulas such as T(n)=T(n-1)+n (this example
is for the worst case of QuickSort).
What do we have at our disposal?
1. bean-counting, by hand, how many times each line of code is executed. Well, that
works well for simple iterative code, such as InsertionSort, but it doesn’t work for
recursive code such as MergeSort.
2. drawing a tree and counting the nodes of the tree. Each node in the tree represents
one instruction, so by summing up all the nodes, we can get the total running
time.
Please look over section 4.2 and the handout on Trees to see how that is done.
MergeSort handout has the detailed description of how to draw a formula as a
tree. It is also in the book, fig. 4.1.
3. using Master Theorem (see below).
4. using substitution method (not covered in this class).
Master Theorem
Master Theorem works only for recurrences in the form T(n) = aT(n/b) + f(n). This is the
form that divide-n-conquer algorithms produce. In that case, T(n) is the expression for
the running time of an algorithm that splits the original problem of size n into “a” pieces,
where each piece is of size n/b. Each piece is solved independently and then the solutions
are merged at the end. f(n) is then the cost of dividing and/or merging the pieces.
QUESTIONS:
Can we use Master Theorem on the formula T(n) = 3T(n) +1? On T(n) = 3T(n-1)+n?
Why or why not?
When we see a recursive formula, the first thing is to determine what is a, b and f(n).
Then we know if we can apply the Master Theorem or not.
The book has the text and 3 examples for Master Theorem in section 4.3 and subsection
“Understanding Master Theorem”, please read it.
The MASTER THEOREM
If T(n) = aT(n/b) + f(n)
If f(n) = O (n logba– ) for some constant 
T(n) = Theta(n logba)
If f(n) = Theta(n logba )
T(n) = Theta(n logba lgn)
If f(n) = Omega(n logba + ) for some constant 
If af(n/b) <= cf(n) for some constant c <1 and all sufficiently large n
T(n) = Theta(f(n))
For all three cases, the formula means that we have to find some number that is
greater than 0 that will make at least one statement correct. For the 3 rd case, we
also need to find some number less than 1 that will make the second condition
true.
Reference:
http://www.cag.lcs.mit.edu/~thies/6.046-web/master.pdf
MIT page with sample Master’s theorem problems.
Download