简体   繁体   English

C# 中寻找最大值的最佳数据结构?

[英]Best data structure in C# for finding maximal value?

I need to know which is the best data structure for finding a maximal value and is it implemented in C#?我需要知道哪个是找到最大值的最佳数据结构,它是在 C# 中实现的吗? For now I'm using SortedDictioniry but it sorts the keys and basically I don't need the keys to be sorted but what I need is a faster way to find the maximal value.现在我正在使用 SortedDictioniry 但它对键进行排序,基本上我不需要对键进行排序但我需要的是一种更快的方法来找到最大值。 Also after I find the maximal value I need the key or the index corresponding to this value.在找到最大值后,我还需要与该值对应的键或索引。 Also if it is possible to have a quick insertion and quick removal of the elements, hopefully not more than O(log n).此外,如果可以快速插入和快速删除元素,希望不超过 O(log n)。 Is there such a structure and how can I use it?有这样的结构吗?我该如何使用它? Thank you!谢谢!

That sounds just like a max-heap , there's plenty of on-line information on how to implement one from scratch (it isn't part of C# standard data structures). 这听起来像是一个最大堆 ,关于如何从头开始实现一个的在线信息很多(它不是C#标准数据结构的一部分)。

For instance, take a look at this implementation , complete with a detailed explanation. 例如,看一下此实现 ,并附带详细说明。

The most efficient will be max-heap, as previously stated. 如前所述,最有效的将是最大堆。 It is not a part of .Net, but you can choose from 3rd party libraries, like IntervalHeap (from http://www.itu.dk/research/c5/ ) 它不是.Net的一部分,但是您可以从第三方库中选择,例如IntervalHeap (来自http://www.itu.dk/research/c5/

You could use a SortedList<TKey, TValue> or a SortedDictionary<TKey, TValue> (as you already do). 您可以使用SortedList<TKey, TValue>SortedDictionary<TKey, TValue> (如您已经做的那样)。 Another suitable collection type would be the Max-Heap; 另一种合适的收集类型是Max-Heap; however, the .NET class library does not include one. 但是,.NET类库不包含一个。

See also SortedList and SortedDictionary Collection Types for a comparison on MSDN. 另请参见SortedList和SortedDictionary集合类型 ,以在MSDN上进行比较。 Also I don't see another possibility than sorting in some way by the key, if you want too have a fast access time on the maximum value. 另外,如果您也希望在最大值上具有快速访问时间,那么除了按键以某种方式排序外,我没有其他可能性。

As of .NET 6, the most suitable data structure for this scenario is probably the PriorityQueue<TElement, TPriority> class. This container supports only the operations Enqueue , Dequeue and Peek , with the smaller priority elements dequeued first.截至 .NET 6,最适合此场景的数据结构可能是PriorityQueue<TElement, TPriority> class。此容器仅支持操作EnqueueDequeuePeek ,优先级较小的元素首先出队。 Elements with equal priority are dequeued with undefined order.具有相同优先级的元素以未定义的顺序出队。 To make it deque first the elements with the largest priority you need to supply it with a custom IComparer<T> , like the one below:要使其首先对具有最大优先级的元素进行双端队列,您需要为其提供自定义IComparer<T> ,如下所示:

class ReverseComparer<T> : IComparer<T>
{
    public int Compare(T x, T y) => -Comparer<T>.Default.Compare(x, y);
}

Usage example, with priority of type int and elements of type string :用法示例,优先级为int类型和string类型的元素:

var queue = new PriorityQueue<string, int>(new ReverseComparer<int>());
queue.Enqueue("Red", 10);
queue.Enqueue("Green", 20);
queue.Enqueue("Yellow", 15);
queue.Enqueue("Brown", 25);
queue.Enqueue("Purple", 20);
while (queue.TryDequeue(out var element, out var priority))
{
    Console.WriteLine($"{priority} -> {element}");
}

Output: Output:

25 -> Brown
20 -> Purple
20 -> Green
15 -> Yellow
10 -> Red

Online demo .在线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM