We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(12)
您可能喜欢 C5 通用集合库中的 IntervalHeap。 引用用户指南
API 非常简单
从 Nuget 安装 https://www.nuget.org/packages/C5或 GitHub https://github.com/sestoft/C5/
You might like IntervalHeap from the C5 Generic Collection Library. To quote the user guide
The API is simple enough
Install from Nuget https://www.nuget.org/packages/C5 or GitHub https://github.com/sestoft/C5/
这是我在 .NET 堆上的尝试
一些测试:
Here's my attempt at a .NET heap
Some tests:
.NET 6+: 正如 @rustyx 评论的那样,.NET 6 添加了 System.Collections.Generic.PriorityQueue 类。 FWIW它是开源的并且 用 C# 实现。
早期 .NET Core 版本和 .NET Framework: Microsoft 已编写(并在线共享)2 个内部 PriorityQueue 类。 然而,正如 @mathusum-mut 评论的那样,其中一个有一个错误(SO 社区当然已经提供了修复程序): Microsoft 内部 PriorityQueue中存在错误?
.NET 6+: As @rustyx commented, .NET 6 adds a System.Collections.Generic.PriorityQueue<TElement,TPriority> class. And FWIW it is open-source and implemented in c#.
Earlier .NET Core versions and .NET Framework: Microsoft has written (and shared online) 2 internal PriorityQueue classes within the .NET Framework. However, as @mathusum-mut commented, there is a bug in one of them (the SO community has, of course, provided fixes for it): Bug in Microsoft's internal PriorityQueue<T>?
我喜欢在 PowerCollectionsOrderedBag 和
OrderedSet
类> 作为优先级队列。I like using the
OrderedBag
andOrderedSet
classes in PowerCollections as priority queues.一个简单的最大堆实现。
https://github.com/bharathkumarms/AlgorithmsMadeEasy/blob/master/算法MadeEasy/MaxHeap.cs
A Simple Max Heap Implementation.
https://github.com/bharathkumarms/AlgorithmsMadeEasy/blob/master/AlgorithmsMadeEasy/MaxHeap.cs
AlgoKit
我编写了一个名为 AlgoKit 的开源库,可通过 NuGet。 它包含:
该代码已经过广泛的测试。 我绝对推荐你尝试一下。
示例
为什么是这三个堆?
实现的最佳选择强烈依赖于输入 - 正如 Larkin、Sen 和 Tarjan 在优先级队列的回归基础实证研究中所示,arXiv:1403.0252v1 [cs.DS]。 他们测试了隐式 d 进制堆、配对堆、斐波那契堆、二项式堆、显式 d 进制堆、等级配对堆、地震堆、违规堆、等级松弛弱堆和严格斐波那契堆。
AlgoKit 具有三种类型的堆,在测试的堆中似乎效率最高。
选择提示
对于相对较少数量的元素,您可能会对使用隐式堆感兴趣,尤其是四元堆(隐式 4 元)。 如果在较大的堆大小上进行操作,二项式堆和配对堆等摊销结构应该表现更好。
AlgoKit
I wrote an open source library called AlgoKit, available via NuGet. It contains:
The code has been extensively tested. I definitely recommend you to give it a try.
Example
Why those three heaps?
The optimal choice of implementation is strongly input-dependent — as Larkin, Sen, and Tarjan show in A back-to-basics empirical study of priority queues, arXiv:1403.0252v1 [cs.DS]. They tested implicit d-ary heaps, pairing heaps, Fibonacci heaps, binomial heaps, explicit d-ary heaps, rank-pairing heaps, quake heaps, violation heaps, rank-relaxed weak heaps, and strict Fibonacci heaps.
AlgoKit features three types of heaps that appeared to be most efficient among those tested.
Hint on choice
For a relatively small number of elements, you would likely be interested in using implicit heaps, especially quaternary heaps (implicit 4-ary). In case of operating on larger heap sizes, amortized structures like binomial heaps and pairing heaps should perform better.
我在 Julian Bucknall 的博客上找到了一篇文章 - http://www.boyet.com/Articles/ PriorityQueueCSharp3.html
我们稍微修改了它,以便队列中的低优先级项目最终会随着时间的推移“冒泡”到顶部,这样它们就不会遭受饥饿。
I found one by Julian Bucknall on his blog here - http://www.boyet.com/Articles/PriorityQueueCSharp3.html
We modified it slightly so that low-priority items on the queue would eventually 'bubble-up' to the top over time, so they wouldn't suffer starvation.
简单的。
easy.
这是我刚刚写的,也许它没有那么优化(只是使用排序的字典)但很容易理解。
您可以插入不同类型的对象,因此没有通用队列。
here's one i just wrote, maybe it's not as optimized (just uses a sorted dictionary) but simple to understand.
you can insert objects of different kinds, so no generic queues.
以下
PriorityQueue
实现使用系统库中的SortedSet
。The following implementation of a
PriorityQueue
usesSortedSet
from the System library.在 Java Collections 框架中的 Java 实现 (java.util.PriorityQueue) 上使用 Java 到 C# 转换器,或者更智能地使用算法和核心代码,并将其插入您自己制作的遵循 C# Collections 框架的 C# 类中队列 API,或者至少是集合 API。
Use a Java to C# translator on the Java implementation (java.util.PriorityQueue) in the Java Collections framework, or more intelligently use the algorithm and core code and plug it into a C# class of your own making that adheres to the C# Collections framework API for Queues, or at least Collections.
我最近遇到了同样的问题,最终为此创建了一个 NuGet 包。
这实现了标准的基于堆的优先级队列。 它还具有 BCL 集合的所有常见优点:
ICollection
和IReadOnlyCollection
实现、自定义IComparer
支持、指定初始容量的能力以及使集合在调试器中更易于使用的 DebuggerTypeProxy 。还有一个 内联 版本的包,它仅将单个 .cs 文件安装到您的项目(如果您想避免采用外部可见的依赖项,则很有用)。
更多信息请访问 github 页面。
I had the same issue recently and ended up creating a NuGet package for this.
This implements a standard heap-based priority queue. It also has all the usual niceties of the BCL collections:
ICollection<T>
andIReadOnlyCollection<T>
implementation, customIComparer<T>
support, ability to specify an initial capacity, and aDebuggerTypeProxy
to make the collection easier to work with in the debugger.There is also an Inline version of the package which just installs a single .cs file into your project (useful if you want to avoid taking externally-visible dependencies).
More information is available on the github page.