当前位置: 首页>>代码示例>>C#>>正文


C# PriorityQueue.ExtractHighestPriorityElement方法代码示例

本文整理汇总了C#中System.Collections.Generic.PriorityQueue.ExtractHighestPriorityElement方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.ExtractHighestPriorityElement方法的具体用法?C# PriorityQueue.ExtractHighestPriorityElement怎么用?C# PriorityQueue.ExtractHighestPriorityElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Collections.Generic.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.ExtractHighestPriorityElement方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Find

        public static IEnumerable<int?> Find(DirectedWeightedGraph graph, int source)
        {
            var dist = new int?[graph.NodesCount];
            dist[source] = 0;

            var closestNodes = new PriorityQueue<int, int>(dist.Select((d, i) => new KeyValuePair<int, int>(i, d.GetValueOrDefault(int.MaxValue))));
            var exploredNodes = new HashSet<int>();

            while (closestNodes.Count != 0)
            {
                var node = closestNodes.ExtractHighestPriorityElement();
                exploredNodes.Add(node);
                foreach (var edge in graph.GetEdges(node).Where(e => !exploredNodes.Contains(e.EndNode)))
                {
                    if (dist[node] != null)
                    {
                        var alt = dist[node].Value + edge.Weight;
                        if (alt < dist[edge.EndNode].GetValueOrDefault(int.MaxValue))
                        {
                            dist[edge.EndNode] = alt;
                            closestNodes.ChangePriority(edge.EndNode, alt);
                        }
                    }
                }
            }

            return dist;
        }
开发者ID:korzenikov,项目名称:Projects,代码行数:28,代码来源:Dijkstra.cs

示例2: GetMinimumSpanningTreeLength

        public static int GetMinimumSpanningTreeLength(DirectedWeightedGraph graph)
        {
            int length = 0;
            var dist = new int[graph.NodesCount];
            const int StartNode = 0;
            dist[StartNode] = 0;
            for (int i = 0; i < dist.Length; i++)
            {
                if (i != StartNode)
                {
                    dist[i] = int.MaxValue;
                }
            }

            var closestNodes = new PriorityQueue<int, int>(dist.Select((d, i) => new KeyValuePair<int, int>(i, d)));
            var exploredNodes = new HashSet<int>();

            while (exploredNodes.Count != graph.NodesCount)
            {
                var node = closestNodes.ExtractHighestPriorityElement();
                length += dist[node];
                exploredNodes.Add(node);
                foreach (var edge in graph.GetEdges(node).Where(e => !exploredNodes.Contains(e.EndNode)))
                {
                    var alt = edge.Weight;
                    if (alt < dist[edge.EndNode])
                    {
                        dist[edge.EndNode] = alt;
                        closestNodes.ChangePriority(edge.EndNode, alt);
                    }
                }
            }

            return length;
        }
开发者ID:korzenikov,项目名称:Projects,代码行数:35,代码来源:PrimsAlgorithm.cs

示例3: ExtractHighestPriorityElementTest

        public void ExtractHighestPriorityElementTest()
        {
            const int N = 100;
            var minHeap = new PriorityQueue<int, int>(Enumerable.Range(0, N).Select(x => new KeyValuePair<int, int>(x, x)).Reverse());

            for (int i = 0; i < N; i++)
            {
                minHeap.ExtractHighestPriorityElement().Should().Be(i);
            }
        }
开发者ID:korzenikov,项目名称:Projects,代码行数:10,代码来源:PriorityQueueTest.cs

示例4: GetMaxSpacing

        public static int GetMaxSpacing(DirectedWeightedGraph graph, int clusterCount)
        {
            int currentClusterCount = graph.NodesCount;
            var unionFind = new UnionFind(currentClusterCount);
            var edges = new PriorityQueue<WeightedEdge, int>(graph.GetEdges().Select(e => new KeyValuePair<WeightedEdge, int>(e, e.Weight)));
            while (currentClusterCount >= clusterCount)
            {
                var edge = edges.ExtractHighestPriorityElement();
                var cluster1 = unionFind.Find(edge.StartNode);
                var cluster2 = unionFind.Find(edge.EndNode);
                if (cluster1 != cluster2)
                {
                    if (currentClusterCount == clusterCount)
                    {
                        return edge.Weight;
                    }

                    unionFind.Union(cluster1, cluster2);
                    currentClusterCount--;
                }
            }

            return 0;
        }
开发者ID:korzenikov,项目名称:Projects,代码行数:24,代码来源:Clustering.cs


注:本文中的System.Collections.Generic.PriorityQueue.ExtractHighestPriorityElement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。