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


C# PriorityQueue.ChangePriority方法代码示例

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


在下文中一共展示了PriorityQueue.ChangePriority方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: PriorityQueue

        public void PriorityQueue()
        {
            PriorityQueue<int> pq = new PriorityQueue<int>(LinqExt.TakeMore);
            pq.Enqueue(1, 1);
            pq.Enqueue(9, 9);
            pq.Enqueue(2, 2);
            pq.Enqueue(5, 5);

            Assert.IsTrue(pq.First() == 9, "Wrong Priority");
            Assert.IsTrue(pq.Dequeue() == 9, "Wrong Priority");
            Assert.IsTrue(pq.Dequeue() == 5, "Wrong Priority");

            pq.ChangePriority(1, 29);
            Assert.IsTrue(pq.First() == 1, "Wrong Priority, 1 should be first priority now with priority 29");

            pq.Enqueue(3, 2); //add item with same priority
            pq.Enqueue(4, 2); //add item with same priority

            Assert.IsTrue(pq.Last() == 4, "Wrong Priority, 4 should be last");

            pq.Enqueue(30, 30); // mix case
            Assert.IsTrue(pq.First() == 30, "Wrong Priority, 30 should be first priority");

            pq.Enqueue(31, 30); // mix case
            Assert.IsTrue(pq.First() == 30, "Wrong Priority, 30 should still be first priority");

            pq.Enqueue(5, 2); //add item with same priority
            pq.Enqueue(6, 2); //add item with same priority

            Assert.IsTrue(pq.Last() == 6, "Wrong Priority, 6 should be last");
        }
开发者ID:nantcom,项目名称:projecteuler,代码行数:31,代码来源:UtilityTest.cs

示例3: 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


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