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


C# Generic.PriorityQueue类代码示例

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


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

示例1: DijkstraAlgorithm

        public static void DijkstraAlgorithm(Dictionary<Node, List<Connection>> graph, Node source)
        {
            PriorityQueue<Node> queue = new PriorityQueue<Node>();

            foreach (var item in graph)
            {
                item.Key.DijkstraDistance = long.MaxValue;
            }

            queue.Enqueue(source);
            source.DijkstraDistance = 0;

            while (queue.Count != 0)
            {
                var currentNode = queue.Dequeue();

                foreach (var connection in graph[currentNode])
                {
                    var dist = connection.Distance + currentNode.DijkstraDistance;

                    if (dist < connection.ToNode.DijkstraDistance)
                    {
                        connection.ToNode.DijkstraDistance = dist;
                        queue.Enqueue(connection.ToNode);
                    }
                }
            }
        }
开发者ID:atanas-georgiev,项目名称:Data-Structures-and-Algorithms-Homeworks,代码行数:28,代码来源:EntryPoint.cs

示例2: PathFinder

        public PathFinder(Grid graph, Cell start, Cell goal)
        {
            //  Priority Queue which contains cells that are candidates for examining, lowest priority to the node with the lowest f value
            var frontier = new PriorityQueue<Cell>();
            frontier.Enqueue(start, 0);

            cameFrom[start] = start;
            costSoFar[start] = 0;

            while (frontier.Count > 0)
            {
                var current = frontier.Dequeue();

                // Exit the search if goal have discovered
                if (current.Equals(goal)) { break; }

                // discovers the neighbours
                foreach (var next in graph.Neighbors(current))
                {
                    int newCost = costSoFar[current] + graph.Cost(current, next);

                    if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next])
                    {
                        costSoFar[next] = newCost;

                        // f = g + h
                        int priority = newCost + Heuristic(next, goal);

                        frontier.Enqueue(next, priority);
                        cameFrom[next] = current;
                    }
                }
            }
        }
开发者ID:TeamViperz,项目名称:Tanks,代码行数:34,代码来源:PathFinder.cs

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

示例4: ForegroundNotificationService

        public ForegroundNotificationService()
        {
            _workQueue = new PriorityQueue();
            _lastProcessedTimeInMS = Environment.TickCount;

            Task.Factory.SafeStartNewFromAsync(ProcessAsync, CancellationToken.None, TaskScheduler.Default);
        }
开发者ID:elemk0vv,项目名称:roslyn-1,代码行数:7,代码来源:ForegroundNotificationService.cs

示例5: Remove

        public void Remove()
        {
            PriorityQueue<int> p = new PriorityQueue<int>(delegate(int i, int j) { return i - j; });
            Random r = new Random();
            for(int i = 0; i < 10000; ++i) {
                p.Enqueue(r.Next(1000));
            }

            int removed = 0;
            while(removed < 100) {
                int count = p.Count;
                p.Remove(r.Next(1000));
                if(count > p.Count) {
                    ++removed;
                }
            }

            int item = p.Dequeue();
            while(p.Count > 0) {
                int next = p.Dequeue();
                if(next < item) {
                    Assert.Fail("bad order detected");
                }
            }
        }
开发者ID:maximmass,项目名称:DReAM,代码行数:25,代码来源:PriorityQueueTests.cs

示例6: Main

        static void Main(string[] args)
        {
            var pq = new PriorityQueue<int, int>();

            pq.Insert(new KeyValuePair<int, int>(0, 0));
            pq.Insert(new KeyValuePair<int, int>(1, 1));
            pq.Insert(new KeyValuePair<int, int>(4, 5));
            pq.Insert(new KeyValuePair<int, int>(1, 1));
            pq.Insert(new KeyValuePair<int, int>(3, 3));
            pq.Insert(new KeyValuePair<int, int>(2, 2));
            pq.Insert(new KeyValuePair<int, int>(5, 5));
            pq.Insert(new KeyValuePair<int, int>(4, 4));

            KeyValuePair<int, int> pair;

            for (var i = 0; i < 10; i++)
            {
                if (pq.TryExtractMinimum(out pair))
                {
                    Console.WriteLine("key: {0}, value: {1}", pair.Key, pair.Value);
                }
                else
                {
                    Console.WriteLine("Not extracted");
                }
            }

            Console.ReadLine();
        }
开发者ID:OleksandrKudinov,项目名称:Education,代码行数:29,代码来源:Program.cs

示例7: Solve

        static void Solve(Dictionary<int, Node> allNodes, Node startNode)
        {
            foreach (var node in allNodes.Values)
            {
                node.Distance = int.MaxValue;
            }

            startNode.Distance = 0;

            PriorityQueue<Node> nodes = new PriorityQueue<Node>();
            nodes.Enqueue(startNode);

            while (nodes.Count > 0)
            {
                var node = nodes.Dequeue();

                if (node.Distance == int.MaxValue)
                {
                    break;
                }

                for (int i = 0; i < node.Connections.Count; i++)
                {
                    var newDistance = node.Distance + node.Connections[i].Distance;
                    if (newDistance < node.Connections[i].ToNode.Distance)
                    {
                        node.Connections[i].ToNode.Distance = newDistance;
                        nodes.Enqueue(node.Connections[i].ToNode);
                    }
                }
            }
        }
开发者ID:nim-ohtar,项目名称:TelerikAkademy,代码行数:32,代码来源:Program.cs

示例8: Dijkstra

        private static void Dijkstra(Graph<int> graph, Node<int> source)
        {
            var queue = new PriorityQueue<Node<int>>();
            foreach (var node in graph.NodesCollection.Keys)
            {
                node.DijkstraDistance = double.PositiveInfinity;
            }

            source.DijkstraDistance = 0.0d;
            queue.Enqueue(source);

            while (queue.Count != 0)
            {
                var currentNode = queue.Dequeue();

                if (double.IsPositiveInfinity(currentNode.DijkstraDistance))
                {
                    break;
                }

                foreach (var neighbor in currentNode.Collection)
                {
                    var potDistance = currentNode.DijkstraDistance + neighbor.Distance;
                    if (potDistance < neighbor.Target.DijkstraDistance)
                    {
                        neighbor.Target.DijkstraDistance = potDistance;
                        queue.Enqueue(neighbor.Target);
                    }

                }
            }
        }
开发者ID:Producenta,项目名称:TelerikAcademy,代码行数:32,代码来源:Startup.cs

示例9: Enqueue_into_empty_queue

 public void Enqueue_into_empty_queue()
 {
     var queue = new List<PriorityQueue<string>.Element>();
     var sut = new PriorityQueue<string>(queue);
     sut.Enqueue("a", 10);
     Assert.That(queue, Is.EqualTo(new[]{new PriorityQueue<string>.Element("a", 10)}));
 }
开发者ID:ralfw,项目名称:TDDaiymiByExample,代码行数:7,代码来源:test_PriorityQueue.cs

示例10: Main

        public static void Main()
        {
            // 1.
            var priorityQueue = new PriorityQueue<int>();
            priorityQueue.Enqueue(2);
            priorityQueue.Enqueue(7);
            priorityQueue.Enqueue(17);
            priorityQueue.Enqueue(19);
            priorityQueue.Enqueue(26);
            priorityQueue.Enqueue(29);
            priorityQueue.Dequeue();
            priorityQueue.Enqueue(3);
            priorityQueue.Enqueue(1);
            priorityQueue.Dequeue();
            priorityQueue.Enqueue(25);
            priorityQueue.Enqueue(36);
            priorityQueue.Dequeue();

            Console.WriteLine();

            // 2.
            PrintFirst20Products();

            Console.WriteLine();

            // 3.
            CountWords();
        }
开发者ID:TeeeeeC,项目名称:TelerikAcademy2015-2016,代码行数:28,代码来源:Startup.cs

示例11: DijkstraAlgorithm

        // Dijkstra's shortest paths algorithm, implemented
        // with priority queue. Running time: O(M * log M)
        // Learn more at: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Using_a_priority_queue
        public static void DijkstraAlgorithm(
            Dictionary<Node, List<Edge>> graph,
            Node sourceNode)
        {
            var queue = new PriorityQueue<Node>();

            foreach (var node in graph)
            {
                node.Key.Distance = double.PositiveInfinity;
            }
            sourceNode.Distance = 0.0d;
            queue.Enqueue(sourceNode);

            while (queue.Count != 0)
            {
                var currentNode = queue.Dequeue();

                if (double.IsPositiveInfinity(currentNode.Distance))
                {
                    // All nodes processed --> algorithm finished
                    break;
                }

                foreach (var childEdge in graph[currentNode])
                {
                    var newDistance = currentNode.Distance + childEdge.Distance;
                    if (newDistance < childEdge.Node.Distance)
                    {
                        childEdge.Node.Distance = newDistance;
                        childEdge.Node.PreviousNode = currentNode;
                        queue.Enqueue(childEdge.Node);
                    }
                }
            }
        }
开发者ID:iliankostov,项目名称:Algorithms,代码行数:38,代码来源:DijkstraPriorityQueue.cs

示例12: Main

        static void Main(string[] args)
        {
            var queue = new PriorityQueue<int>();

            queue.Enqueue(5);
            queue.Enqueue(8);
            queue.Enqueue(3);
            queue.Enqueue(4);
            queue.Enqueue(11);

            foreach (var i in queue)
            {
                Console.WriteLine(i);
            }


            Console.WriteLine("*******Removed value:{0}",queue.Dequeue());
            foreach (var i in queue)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("*******Removed value:{0}", queue.Dequeue());
            foreach (var i in queue)
            {
                Console.WriteLine(i);
            }
        }
开发者ID:GenoGenov,项目名称:TelerikAcademyAssignments,代码行数:27,代码来源:Program.cs

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

示例14: EventQueue

 /// <summary>
 /// Creates a new EventQueue instance and starts the main thread.
 /// </summary>
 public EventQueue()
 {
     q = new PriorityQueue<INaoEvent>(4);
     thread = new Thread(new ThreadStart(Run));
     thread.IsBackground = true;
     thread.Start();
 }
开发者ID:boschbc,项目名称:NaoRobot,代码行数:10,代码来源:EventQueue.cs

示例15: Main

        public static void Main()
        {
            //Heap test

            //Heap<int> testHeap = new Heap<int>();
            //testHeap.Push(3);
            //testHeap.Push(4);
            //testHeap.Push(5);
            //testHeap.Push(1);
            //testHeap.Push(6);

            //while (testHeap.Count > 0)
            //{
            //    testHeap.Pop();
            //    testHeap.Print();
            //}

            //Queue test
            PriorityQueue<int> queue = new PriorityQueue<int>();
            queue.Enqueue(3);
            queue.Enqueue(4);
            queue.Enqueue(5);
            queue.Enqueue(1);
            queue.Enqueue(6);

            queue.Print();

            while (queue.Count > 0)
            {
                queue.Dequeue();
                queue.Print();
            }
        }
开发者ID:vanndann,项目名称:TelerikAcademy,代码行数:33,代码来源:Program.cs


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