证明优化归并排序的运行时间是 theta(NK + Nlog(N/K))?
好的,我知道合并排序的最坏情况时间为 theta(NlogN),但其开销很高,并且出现在进行合并的递归树底部附近。有人建议,一旦大小达到 K,我们就停止递归,并在此时切换到插入排序。我需要证明这个修改后的递推关系的运行时间是theta(NK + Nlog(N/k))?我对如何解决这个问题感到空白。
Okay, I know Mergesort has a worst case time of theta(NlogN) but its overhead is high and manifests near the bottom of the recursion tree where the merges are made. Someone proposed that we stop the recursion once the size reaches K and switch to insertion sort at that point. I need to prove that the running time of this modified recurrence relation is theta(NK + Nlog(N/k))? I am blanking as to how to approach this problem..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许一个好的开始是查看这个问题的递归关系。我想象对于典型的合并排序,它看起来像这样:
即,您将问题分为 2 个大小一半的子问题,然后执行 N 工作(合并)。我们有一个需要恒定时间的基本情况。
将其建模为一棵树:
这给出了 的扩展
所以实际上我们只需要看看它有多深。我们知道第 i 层处理大小为 N / 2^i 的子问题。所以我们的叶节点 (
T(1)
) 出现在L
级别,其中N / 2^L = 1
:所以我们的运行时间是
>N log N
。现在我们介绍插入排序。我们的树看起来像这样
换句话说,我们将在某种程度上
L
必须解决N/K
大小为K
的插入排序问题。插入排序的最坏情况运行时间为K^2
。因此,在叶子处,我们总共需要完成这么多工作:但是我们还需要进行大量合并,每层树的成本为 N,如下所示:之前解释过。回到我们之前的方法,让我们找到
L
(到达大小为K
的子问题并因此切换到插入之前的级别数):所以我们总共
有自从我用算法给你提供证明草图以来已经太久了,但这应该会让你的神经元兴奋起来。
Maybe a good start is to look at the recurrence relation for this problem. I imagine for typical mergesort it would look something like this:
i.e. you are dividing the problem into 2 subproblems of half the size, and then performing N work (the merge). We have a base case that takes constant time.
Modelling this as a tree we have:
This gives an expansion of
So really we just need to see how deep it goes. We know that the
i
th level operates on subproblemsN / 2^i
in size. So our leaf nodes (T(1)
) occur on levelL
whereN / 2^L = 1
:So our runtime is
N log N
.Now we introduce insertion sort. Our tree will look something like this
In other words, we will at some level
L
have to solveN/K
insertion sort problems of sizeK
. Insertion sort has a worst-case runtime ofK^2
. So at the leaves we have this much work in total:But we also have a bunch of merging to do as well, at a cost of
N
per level of the tree as explained before. Going back to our previous method, let's findL
(the number of levels before we reach subproblems of sizeK
and thus switch to insertion):So in total we have
It's been too long since I took algorithms to give you a proof sketch, but that should get your neurons firing.