找到你要的答案

Q:recurrence relation on a Merge Sort algorithm

Q:归并排序算法的递推关系

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.

问题是:

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.

answer1: 回答1:

I like to look at it as "runs", where the ith "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 of i.

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

我喜欢看它的“运行”,在“运行”是对所有的递归步骤与深度完全一

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

你会发现我= log_a(N)-一些基地

所以,让我们更正式:

索赔:

Each element is being processed by at most one recursive call at depth i, for all values of i.

证明:

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.

结论:

因为在每一个深度我的递归,每个元素处理的最多一次,和递归的最大深度为对数,我们得到一个上限O(nlogn)。

我们同样可以证明的下限Ω(nlogn),但这是不必要的,因为排序已经是Ω(nlogn)的问题,所以我们可以得出改进的算法仍然是θ(nlogn)。


如果你想用“基本算法”的证明,它也可以做,通过感应。

索赔: 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

证明:

基地: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

(**)是因为日志(3 / 5)~ = -0.22,所以5nlog(3 / 5)& lt;N,和5nlog(3/5)+2N <;n

algorithm  data-structures  mergesort  recurrence