[ACE] Recurrence Relation
Recurrence Relation
Acknowledgement
The following content is summarized by G52ACE class and MIT open source course(Introduction to Algorithms).Main Body
We don’t care the official definition of recurrence relation, but we need to know:Suppose that the runtime of a program is T(n), then a recurrence relation will express T(n) in terms of its values at other (smaller) values of n.Example:
Suppose the runtime of merge-sort of an array of n integers is T(n). Then T(n) = 2 T(n/2) + b + a n:
• “2 T(n/2)” is due to having to sort the two sub-arrays each of size n/2
• “b” is the cost of doing the split
• “a n” is the cost of doing the merge (and any copying to/from the workspace, etc.)
Also, what is our base case? ===> T(1) = 1
Thus, how can we solve this recurrence relation?
Solving Recurrence using induction(introduced by G52ACE course)
General pattern1. Starting from the base case, use the recurrence to work out many cases, by directly substituting and working upwards in values of n
2. Inspect the results, look for a pattern and make a hypothesis for the general results
3. Attempt to prove the hypothesis – typically using some form of induction
If we regard 2^k to n
T(n) = n + n * log n * a +(n-1) * b = O(n log n)
Often then extract the large n behavior using big-Oh family
Solving Recurrence using Recurrence Tree(introduced by Book)
First we should cast the formula to thisPut the θ(n) to cn then we can draw a recursion tree
What we need to do is to figure out how many leaves are in the lowest level, what is the height of the tree and what is the total of each level
Because the height is logn, the total of each level is cn for internal leaves and θ(n) for external leaves.
Thus, the total is (cn) * lgn + θ(n), we ignore θ(n), it is θ(n log n)
There is not a usual way to solving recurrence, so in terms of different question, we should use different method, here are two other important method– Substitution method and Master Theorem
Substitution method
First, guess the form of solutionSecond, verify by induction
Third, solve the constant
It is somehow similar to the method we learned in the class and i need to further check with my tutor.
But i’d like to introduce this method here:
Here is a example:
In my opinion, it is similar to the method we learned before but it is not so precise. It just can work out O(n^3) in this example but it is not Θ(n^3) because it is O(n^2) in this example too. So it just can narrow your assumption down to some range but not exact complexity.
The master theorem(Both introduced by lecture and book)
MT applies toT(n) = a T(n/b) + f(n) where a>=1, b>1, f(n) is asymptotically positive
- what does it mean by “asymptotically positive”?
- It is when n is bigger enough, f(n) should be positive===> f(n) > 0 for n>= 0;
There are a lot of similar format of MT but i pick the one on Wikipedia
- Case 1:
- Case 2:
- Case 3:
Keep in mind that there are a lot of situation that MT can not applies to.Here are two examples solving actually question in MT(Binary Search and Merge Sort)
So how to prove MT? why f(n) should be compared to n ^ (log b a)?
The prove in Introduction to Algorithm is using tree, but i tried to prove it using mathematical expressions:Here we can see we work out n ^ (log b a), and each case, what we do is to compare it to see which is the dominance of the expression
Okay, it is just my opinion, let’s see how it should be proved officially
We are using a recursive tree.
If we add the total work of the tree, what we will get?!
Cool! Its what i proved above in the last equation!
n ^ (log b a) is the sum of the leaves at the bottom
The above sum of each level will be showed as the summation formula!
It is the same as induction by mathematic
In Case 1: the weight or dominance is on the external leaves than the roots(and internal leaves), so we should use the sum of leaves which is n ^ (log b a)
In Case 2: the weight is similar between them so we should add them up
In Case 3: the weight is mostly on the internal leaves so we should ignore external leaves and it should be O( f(n) )
评论
发表评论