合并ķ排序的链表 - 分析 [英] Merging k sorted linked lists - analysis

查看:160
本文介绍了合并ķ排序的链表 - 分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想不同的解决方案有一个问题。假设我们的K排序的链表,我们将它们合并成一个。所有这些名单在一起有N个元素。

I am thinking about different solutions for one problem. Assume we have K sorted linked lists and we are merging them into one. All these lists together have N elements.

众所周知的解决方案是使用优先级队列和流行/推第一要素,从每一个名单,我可以理解为什么需要 O(N日志K)的时间。

The well known solution is to use priority queue and pop / push first elements from every lists and I can understand why it takes O(N log K) time.

但是,让我们来看看另一种方法。假设我们有一些 MERGE_LISTS(LIST1,列表2)程序,即合并两个排序列表,它会采取 O(T1 + T2)时间,其中<​​code> T1 和 T2 代表 LIST1 列表2 尺寸。

But let's take a look at another approach. Suppose we have some MERGE_LISTS(LIST1, LIST2) procedure, that merges two sorted lists and it would take O(T1 + T2) time, where T1 and T2 stand for LIST1 and LIST2 sizes.

我们确实现在通常是指配对这些列表和合并它们配对逐对(如果数量是奇数,最后列表,例如,可在第一步骤被忽略)。这通常意味着我们必须做如下树的合并操作的:

What we do now generally means pairing these lists and merging them pair-by-pair (if the number is odd, last list, for example, could be ignored at first steps). This generally means we have to make the following "tree" of merge operations:

N1,N2,N3 ... 代表 LIST1,列表2,项目list3 尺寸

  • O(N1 + N2)+ O(N3 + N4)+ O(N5 + N6)+ ...
  • O(N1 + N2 + N3 + N4)+ O(N5 + N6 + N7 + N8)+ ...
  • O(N1 + N2 + N3 + N4 + ... + NK)
  • O(N1 + N2) + O(N3 + N4) + O(N5 + N6) + ...
  • O(N1 + N2 + N3 + N4) + O(N5 + N6 + N7 + N8) + ...
  • O(N1 + N2 + N3 + N4 + .... + NK)

看起来很明显,将有日志(K)这行的,他们每个人实施 O(N)操作,所以时间合并(LIST1,列表2,...,LISTK)操作实际上等于 O(N 1o9氏)

It looks obvious that there will be log(K) of these rows, each of them implementing O(N) operations, so time for MERGE(LIST1, LIST2, ... , LISTK) operation would actually equal O(N log K).

我的朋友告诉我(两天前),将采取 O(KN)的时间。所以,问题是 - 做我F%CK某处或者是他真的错了吗?如果我是正确的,为什么不这样分而放;治?方法不能用来代替优先级队列的做法

My friend told me (two days ago) it would take O(K N) time. So, the question is - did I f%ck up somewhere or is he actually wrong about this? And if I am right, why doesn't this 'divide&conquer' approach can't be used instead of priority queue approach?

推荐答案

如果你有一个小数目清单合并,这种配对方案是可能的,因为你必须每合并极少的操作比优先级队列的方法更快:基本上只是一个比较和每个项目二指针重新分配(转移到一个新的单链表)。正如你所展示的,是 O(N日志K) 1o9氏/ code>步骤处理 N 项目每个)。

If you have a small number of lists to merge, this pairwise scheme is likely to be faster than a priority queue method because you have extremely few operations per merge: basically just one compare and two pointer reassignments per item (to shift into a new singly-linked list). As you've shown, it is O(N log K) (log K steps handling N items each).

但是,最好的优先级队列算法,我相信, O(开方(日志K)) O(log日志U)插入和删除(其中 U 是可以不同的优先级数) - 如果你可以用一个价值优先,而不必使用compare- - 因此,如果要合并的项目,可给予如整数重点和 K 大,那么你最好使用优先级队列。

But the best priority queue algorithms are, I believe, O(sqrt(log K)) or O(log log U) for insert and remove (where U is the number of possible different priorities)--if you can prioritize with a value instead of having to use a compare--so if you are merging items that can be given e.g. integer priorities, and K is large, then you're better off with a priority queue.

这篇关于合并ķ排序的链表 - 分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆