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


C# PriorityQueue类代码示例

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


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

示例1: TestPriorityQueue

        public void TestPriorityQueue()
        {
            int operationsCount = 100000;

            Random rand = new Random(0);

            PriorityQueue<double> queue = new PriorityQueue<double>();

            for (int op = 0; op < operationsCount; ++op)
            {
                int opType = rand.Next(0, 2);

                if (opType == 0) // Enqueue
                {
                    double item = (100.0 - 1.0) * rand.NextDouble() + 1.0;
                    queue.Enqueue(item);

                    Assert.IsTrue(queue.IsConsistent(), "Test fails after enqueue operation # " + op);
                }
                else // Dequeue
                {
                    if (queue.Count > 0)
                    {
                        double item = queue.Dequeue();
                        Assert.IsTrue(queue.IsConsistent(), "Test fails after dequeue operation # " + op);
                    }
                }
            }
        }
开发者ID:Ivan-Dimitrov-bg,项目名称:.Net-framework,代码行数:29,代码来源:PriorityQueueTest.cs

示例2: CorrectPriorityTest2

        public void CorrectPriorityTest2()
        {
            PriorityQueue<int> queue = new PriorityQueue<int>();

            queue.Enqueue(4);
            queue.Enqueue(1);
            queue.Enqueue(3);
            queue.Enqueue(2);
            queue.Enqueue(16);
            queue.Enqueue(9);
            queue.Enqueue(10);
            queue.Enqueue(14);
            queue.Enqueue(8);
            queue.Enqueue(7);

            Assert.AreEqual(10, queue.Count);
            Assert.AreEqual(16, queue.Dequeue());
            Assert.AreEqual(14, queue.Dequeue());
            Assert.AreEqual(10, queue.Dequeue());
            Assert.AreEqual(9, queue.Dequeue());
            Assert.AreEqual(8, queue.Dequeue());
            Assert.AreEqual(7, queue.Dequeue());
            Assert.AreEqual(4, queue.Dequeue());
            Assert.AreEqual(3, queue.Dequeue());
            Assert.AreEqual(2, queue.Dequeue());
            Assert.AreEqual(1, queue.Dequeue());
        }
开发者ID:vladislav-karamfilov,项目名称:TelerikAcademy,代码行数:27,代码来源:EnqueueOperationTests.cs

示例3: Main

    /* 1 Implement a class PriorityQueue<T> based
     * on the data structure "binary heap".
     * */
    static void Main(string[] args)
    {
        var heap = new Heap<int>();
        heap.Add(1);
        heap.Add(2);
        heap.Add(3);

        Debug.Assert(heap.SameContents(new[] { 1, 2, 3 }));
        Console.WriteLine(string.Join(",", heap));

        Debug.Assert(heap.ChopHead() == 3);
        Debug.Assert(heap.ChopHead() == 2);
        Debug.Assert(heap.ChopHead() == 1);
        Debug.Assert(heap.IsEmpty);

        // higher string means lower priority
        var pqueue = new PriorityQueue<string, string>((s1, s2) => -s1.CompareTo(s2));

        pqueue.Enqueue("18:00", "Buy food");
        pqueue.Enqueue("06:00", "Walk dog");
        pqueue.Enqueue("21:00", "Do homework");
        pqueue.Enqueue("09:00", "Go to work");
        pqueue.Enqueue("21:00", "Drink beer");

        Debug.Assert(pqueue.Count == 5);

        Debug.Assert(pqueue.Dequeue() == "Walk dog");
        Debug.Assert(pqueue.Dequeue() == "Go to work");
        Debug.Assert(pqueue.Dequeue() == "Buy food");
        Debug.Assert(new[] { "Do homework", "Drink beer" }.Contains(pqueue.Dequeue()));
        Debug.Assert(new[] { "Do homework", "Drink beer" }.Contains(pqueue.Dequeue()));
    }
开发者ID:staafl,项目名称:ta-hw-dsa,代码行数:35,代码来源:program.cs

示例4: 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:Alex-Tsvetanov,项目名称:Informatics,代码行数:37,代码来源:DijkstraPriorityQueue.cs

示例5: Main

    static void Main()
    {
        Console.WriteLine("Creating an instance of the custom PriorityQueue class and adding 8 integers to it in non-increasing randomized order.");
        var priorityQueue = new PriorityQueue<int>();
        priorityQueue.Enqueue(5);
        priorityQueue.Enqueue(3);
        priorityQueue.Enqueue(2);
        priorityQueue.Enqueue(5);
        priorityQueue.Enqueue(15);
        priorityQueue.Enqueue(6);
        priorityQueue.Enqueue(0);
        priorityQueue.Enqueue(-8);

        Console.WriteLine("Printing the queue:");
        PrintQueue(priorityQueue);

        Console.WriteLine("Dequeuing and displaying the first 5 elements");
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Dequeuing the number {0}", priorityQueue.Dequeue());
        }

        Console.WriteLine("Printing the queue after the removal:");
        PrintQueue(priorityQueue);

        Console.WriteLine("Adding 5 more integers to the queue in non-increasing randomized order.");
        priorityQueue.Enqueue(9);
        priorityQueue.Enqueue(20);
        priorityQueue.Enqueue(13);
        priorityQueue.Enqueue(2);
        priorityQueue.Enqueue(7);

        Console.WriteLine("Printing the queue after the addition:");
        PrintQueue(priorityQueue);
    }
开发者ID:klimentt,项目名称:Telerik-Academy,代码行数:35,代码来源:TestProgram.cs

示例6: FindPath

    public static Path<Tile> FindPath(
        Tile start,
        Tile destination)
    {
        var closed = new HashSet<Tile>();
        var queue = new PriorityQueue<double, Path<Tile>>();
        queue.Enqueue(0, new Path<Tile>(start));

        while (!queue.IsEmpty)
        {
            var path = queue.Dequeue();

            if (closed.Contains(path.LastStep))
                continue;
            if (path.LastStep.Equals(destination))
                return path;

            closed.Add(path.LastStep);

            foreach (Tile n in path.LastStep.Neighbours)
            {
                double d = distance(path.LastStep, n);
                var newPath = path.AddStep(n, d);
                queue.Enqueue(newPath.TotalCost + estimate(n, destination), 
                    newPath);
            }
        }

        return null;
    }
开发者ID:VicBoss,项目名称:KR,代码行数:30,代码来源:PathFinder.cs

示例7: PrimAlgorithm

        static ICollection<Node> PrimAlgorithm(Node startNode)
        {
            var mst = new List<Node>();
            PriorityQueue queue = new PriorityQueue();
            startNode.Used = true;

            mst.Add(startNode);
            foreach (var connection in neighbourhood[startNode])
            {
                queue.Enqueue(connection);
            }

            while (mst.Count != houses.Count)
            {
                var current = queue.Dequeue();

                current.ToNode.Used = true;
                mst.Add(current.ToNode);

                foreach (var connection in neighbourhood[current.ToNode])
                {
                    if (!connection.ToNode.Used)
                    {
                        queue.Enqueue(connection);
                    }
                }
            }

            return mst;
        }
开发者ID:ilkodzhambazov,项目名称:Telerik-Academy,代码行数:30,代码来源:Program.cs

示例8: Main

    private static void Main()
    {
        var items = new[] { 2, 6, 3, 2, 1, 7, 4, 9, 5, 1, 8 };
        Console.WriteLine("Items: [{0}]", string.Join(", ", items));


        // Priority queue of integers, where a lower number means higher priority
        var queue = new PriorityQueue<int>();        

        // Add each item to the priority queue and 
        // check if the item with the highest priority is at the top of the queue
        var minItem = int.MaxValue;
        foreach (var item in items)
        {
            queue.Enqueue(item);
            minItem = Math.Min(item, minItem);            
            Debug.Assert(queue.Peek() == minItem);
        }

        // Now check if after each dequeue, the items come out ranked by priority
        var sorted = new List<int>();
        while (queue.Count > 0)
        {
            sorted.Add(queue.Dequeue());
        }

        // Items should be sorted in ascending order
        Console.WriteLine("Queue items: [{0}]", string.Join(", ", sorted));
    }
开发者ID:MarKamenov,项目名称:TelerikAcademy,代码行数:29,代码来源:Program.cs

示例9: getObjectsObservableBy

    public PriorityQueue<IHearable> getObjectsObservableBy(IHearing listener)
    {
        //Brute force approach = O(n)
        //For all hearing objects it's O(m*n) ~ Acceptable.
        PriorityQueue<IHearable> queue = new PriorityQueue<IHearable>(true);
        int i;

        IHearable target;
        for (i=0;i<hearableObjects.Count;++i)
        {
            double distanceSquared,priority; //Hopefully this will be optimized out.

            target = hearableObjects[i];

            //Calculates Distance^2
            distanceSquared = (listener.getLocation() - target.getLocation()).sqrMagnitude;

            //Store in variable for use as queue priority
            priority = target.getVolume().volumeFromDistanceSquared(distanceSquared).Intensity;

            //Put in queue if louder than hearing threshold.
            //Debug.Log(Volume.fromIntensity(priority).Decibels + " " + target.getGameObject().name);
            if (priority >= listener.getHearingThreshold().Intensity)
            {
                //Debug.Log(Volume.fromIntensity(priority).Decibels + " " + target.getGameObject().name);
                queue.enqueueWithPriority(target,priority);
            }
        }

        return queue;
    }
开发者ID:Syclamoth,项目名称:GamesAI6,代码行数:31,代码来源:SoundManager.cs

示例10: GetShortestPath

        public Route GetShortestPath(Vertex from, Vertex to)
        {
            ResetExploration();

            var queue = new PriorityQueue<double, Route>();

            queue.Enqueue(0, new Route { @from });
            while (!queue.IsEmpty)
            {
                var route = queue.Dequeue();
                if (route.Last().IsExplored)
                    continue;

                if (route.Last().Equals(to))
                    return route;

                route.Last().IsExplored = true;
                foreach (Edge n in route.Last().AdjacentEdges)
                {
                    var newRoute = route.Clone();
                    newRoute.Add(n.Tail);
                    queue.Enqueue(newRoute.Cost, newRoute);
                }
            }
            return null;
        }
开发者ID:Wisehorn,项目名称:ShortestPath,代码行数:26,代码来源:Graph.cs

示例11: DijkstraAlgorithm

    public static void DijkstraAlgorithm(Graph graph, Node source)
    {
        foreach (var node in graph)
        {
            node.MinDistance = double.PositiveInfinity;
        }

        source.MinDistance = 0;

        var pQueue = new PriorityQueue<Node>();
        pQueue.Enqueue(source);

        while (pQueue.Count != 0)
        {
            Node currentNode = pQueue.Dequeue();

            foreach (var neighbour in graph[currentNode.Id].Neighbors)
            {
                double newDist = currentNode.MinDistance + neighbour.Distance;

                if (newDist < neighbour.Node.MinDistance)
                {
                    neighbour.Node.MinDistance = newDist;
                    pQueue.Enqueue(neighbour.Node);
                }
            }
        }
    }
开发者ID:MarKamenov,项目名称:TelerikAcademy,代码行数:28,代码来源:Dijkstra.cs

示例12: FindMinimalPaths

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

            foreach (var node in graph)
            {
                if (source.ID != node.Key.ID)
                {
                    node.Key.DijkstraDistance = ulong.MaxValue;
                }
            }

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

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

                foreach (var neighbour in graph[currentNode])
                {
                    ulong potDistance = currentNode.DijkstraDistance + neighbour.Distance;

                    if (potDistance < neighbour.Node.DijkstraDistance)
                    {
                        neighbour.Node.DijkstraDistance = potDistance;
                        // adds only modified elements in the queue
                        // thus reordering the queue after each iteration is avoided
                        queue.Enqueue(neighbour.Node);
                    }
                }
            }
        }
开发者ID:psotirov,项目名称:TelerikAcademyProjects,代码行数:33,代码来源:Dijkstra.cs

示例13: Main

        static void Main(string[] args)
        {
            IQueue CommanderDispatcherMessageQueue = new PriorityQueue(30);
            IQueue DispatcherSerialMessageQueue = new PriorityQueue(30);
            //IQueue SerialStatusMessageQueue = new PriorityQueue(30);
               // IQueue StatusCommanderMessageQueue = new PriorityQueue(30);
            //IQueue MicrocontrollerCommanderMessageBox = new PriorityQueue(30);

            Thread dispatcher = new Thread(() => Dispatcher(CommanderDispatcherMessageQueue));
            //Thread serialManager = new Thread(() => SerialManager(DispatcherSerialMessageQueue, MicrocontrollerCommanderMessageBox));
            //Thread statusUpdater = new Thread(() => StatusUpdater(SerialStatusMessageQueue));
            //Thread commandSender = new Thread(() => CommandSender(MicrocontrollerCommanderMessageBox));

            RoverCameraFactory.GetInstance().Initialize(Properties.NetworkSettings.Default.OperatorIPAddress, Properties.NetworkSettings.Default.CameraBasePort);

            dispatcher.Start();
            //serialManager.Start();
            //statusUpdater.Start();
               // commandSender.Start();

            //Start the commands listener
            var commandsListener = new GuardedMessageListener(
                Properties.NetworkSettings.Default.CommandsPort,
                CommanderDispatcherMessageQueue,
                Properties.NetworkSettings.Default.OperatorIPAddress,
                new WatchDog());
            commandsListener.StartListening();
        }
开发者ID:jpadillak,项目名称:Concordia_Capstone2013-14,代码行数:28,代码来源:Program.cs

示例14: Main

        static void Main()
        {
            var bag = new PriorityQueue<Car>();

            var opel = new Car
            {
                Model = "Opel",
                Price = 1000
            };
            var mercedes = new Car
            {
                Model = "Mercedes",
                Price = 5000
            };

            var citroen = new Car
            {
                Model = "Citroen",
                Price = 3000
            };

            bag.Enqueue(opel);
            bag.Enqueue(mercedes);
            bag.Enqueue(citroen);

            while(bag.Count > 0)
            {
                var car = bag.Dequeue();
                Console.WriteLine("{0} -> {1}",car.Model, car.Price);
            }
        }
开发者ID:radkomitev,项目名称:Telerik-Academy,代码行数:31,代码来源:StartUp.cs

示例15: Main

 static void Main(string[] args)
 {
     PriorityQueue queue = new PriorityQueue();
     queue.Enqueue("Name1", Priority.Low);
     queue.Enqueue("Name2", Priority.Low);
     queue.Enqueue("Name3", Priority.Low);
     queue.Enqueue("Name4", Priority.Low);
     queue.Enqueue("Name5", Priority.Low);
     queue.Enqueue("Name6", Priority.Low);
     queue.Enqueue("Name7", Priority.Low);
     queue.Enqueue("Name8", Priority.Low);
     queue.Enqueue("Name9", Priority.Low);
     Console.WriteLine("There are " + queue.Length + " people in line.");
     Console.WriteLine("Person in front: " + queue.PeekAtFrontName() + " with priority " + queue.PeekAtFrontPriority());
     queue.Dequeue();
     Console.WriteLine("Person in front: " + queue.PeekAtFrontName() + " with priority " + queue.PeekAtFrontPriority());
     queue.Dequeue();
     Console.WriteLine("Person in front: " + queue.PeekAtFrontName() + " with priority " + queue.PeekAtFrontPriority());
     queue.Enqueue("Name10", Priority.Medium);
     Console.WriteLine("Person in front: " + queue.PeekAtFrontName() + " with priority " + queue.PeekAtFrontPriority());
     queue.Enqueue("Name11", Priority.Medium);
     Console.WriteLine("Person in front: " + queue.PeekAtFrontName() + " with priority " + queue.PeekAtFrontPriority());
     queue.Dequeue();
     queue.Enqueue("Dad", Priority.High);
     Console.WriteLine("Person in front: " + queue.PeekAtFrontName() + " with priority " + queue.PeekAtFrontPriority());
 }
开发者ID:Artmageddon,项目名称:DataStructures,代码行数:26,代码来源:Program.cs


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