问题
The question is :
UNBALANCED MERGE SORT is a sorting algorithm, which is a modified version of the standard MERGE SORT algorithm. The only difference is that instead of dividing the input into 2 equal parts in each stage, we divide it into two unequal parts – the first 2/5 of the input, and the other 3/5.
a. Write the recurrence relation for the worst case time complexity of the UNBALANCED MERGE SORT algorithm.
b. What is the worst case time complexity of the UNBALANCEDMERGESORT algorithm? Solve the recurrence relation from the previous section.
So i'm thinkin the recurrence relation is : T(n) <= T(2n/5) + T(3n/5) + dn. Not sure how to solve it. Thanks in advance.
回答1:
I like to look at it as "runs", where the i
th "run" is ALL the recursive steps with depth exactly i
.
In each such run, at most n
elements are being processed (we will prove it soon), so the total complexity is bounded by O(n*MAX_DEPTH)
, now, MAX_DEPTH
is logarithmic, as in each step the bigger array is size 3n/5
, so at step i
, the biggest array is of size 3^i/5^i * n
.
Sovle the equation:
3^i/5^i * n = 1
and you will find out that i = log_a(n)
- for some base a
So, let's be more formal:
Claim:
Each element is being processed by at most one recursive call at depth
i
, for all values ofi
.
Proof:
By induction, at depth 0, all elements are processed exactly once by the first call.
Let there be some element x
, and let's have a look on it at step i+1. We know (induction hypothesis) that x
was processed at most once in depth i
, by some recursive call. This call later invoked (or not, we claim at most once) the recursive call of depth i+1
, and sent the element x
to left OR to right, never to both. So at depth i+1
, the element x
is proccessed at most once.
Conclusion:
Since at each depth i
of the recursion, each element is processed at most once, and the maximal depth of the recursion is logarithmic, we get an upper bound of O(nlogn)
.
We can similarly prove a lower bound of Omega(nlogn)
, but that is not needed, since sorting is already an Omega(nlogn)
problem - so we can conclude the modified algorithm is still Theta(nlogn)
.
If you want to prove it with "basic arithmetics", it can also be done, by induction.
Claim: T(n) = T(3n/5) + T(2n/5) + n <= 5nlog(n) + n
It will be similar when replacing +n
with +dn
, I simplified it, but follow the same idea of proof with T(n) <= 5dnlogn + dn
Proof:
Base: T(1) = 1 <= 1log(1) + 1 = 1
T(n) = T(3n/5) + T(2n/5) + n
<= 5* (3n/5) log(3n/5) +3n/5 + 5*(2n/5)log(2n/5) +2n/5 + n
< 5* (3n/5) log(3n/5) + 5*(2n/5)log(3n/5) + 2n
= 5*nlog(3n/5) + 2n
= 5*nlog(n) + 5*nlog(3/5) + 2n
(**)< 5*nlog(n) - n + 2n
= 5nlog(n) + n
(**) is because log(3/5)~=-0.22, so 5nlog(3/5) < -n
, and 5nlog(3/5) + 2n < n
来源:https://stackoverflow.com/questions/30005437/recurrence-relation-on-a-merge-sort-algorithm