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


C# PriorityQueue.Enqueue方法代码示例

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


在下文中一共展示了PriorityQueue.Enqueue方法的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: findShortestPath

    //-------------------------------------Movement-----------------------------------------//
    // A* algorithm to find shortest path to desired tile.
    private Path<Tile> findShortestPath(Tile start, Tile end)
    {
        PriorityQueue<int, Path<Tile>> open = new PriorityQueue<int, Path<Tile>>();
        HashSet<Tile> closed = new HashSet<Tile>();
        open.Enqueue(0, new Path<Tile>(start));
        int cost = 1;

        while (!open.isEmpty())
        {
            var path = open.Dequeue();
            if (closed.Contains(path.LastStep))
            {
                continue;
            }
            if (path.LastStep.Equals(end))
            {
                return path;
            }
            closed.Add(path.LastStep);
            foreach (Tile t in path.LastStep.connectedTiles)
            {
                if (t.isBlocked)
                {
                    closed.Add(t);
                    continue;
                }

                var newPath = path.AddStep(t, cost);
                open.Enqueue(newPath.TotalCost, newPath);
            }
        }
        return null;
    }
开发者ID:romanlarionov,项目名称:Sabotage,代码行数:35,代码来源:GameBoardPlayer.cs

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

示例4: SimpleWithPriority

        public void SimpleWithPriority()
        {
            var priorityQueue = new PriorityQueue<string, int>(PriorityQueueType.Minimum);

            int priority;

            priorityQueue.Enqueue("g", 6);

            var item = priorityQueue.Peek(out priority);

            Assert.AreEqual(item, "g");
            Assert.AreEqual(priority, 6);
            Assert.AreEqual(priorityQueue.Count, 1);

            priorityQueue.Enqueue("h", 5);

            item = priorityQueue.Peek(out priority);

            Assert.AreEqual(item, "h");
            Assert.AreEqual(priority, 5);
            Assert.AreEqual(priorityQueue.Count, 2);

            priorityQueue.Enqueue("i", 7);

            item = priorityQueue.Peek(out priority);

            Assert.AreEqual(item, "h");
            Assert.AreEqual(priority, 5);
            Assert.AreEqual(priorityQueue.Count, 3);
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:30,代码来源:Peek.cs

示例5: CorrectPriorityTest1

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

            queue.Enqueue(-12);
            queue.Enqueue(512);
            queue.Enqueue(77);
            queue.Enqueue(-1);
            queue.Enqueue(82);
            queue.Enqueue(92);
            queue.Enqueue(-111);
            queue.Enqueue(-151);
            queue.Enqueue(512);
            queue.Enqueue(55);

            Assert.AreEqual(10, queue.Count);
            Assert.AreEqual(512, queue.Dequeue());
            Assert.AreEqual(512, queue.Dequeue());
            Assert.AreEqual(92, queue.Dequeue());
            Assert.AreEqual(82, queue.Dequeue());
            Assert.AreEqual(77, queue.Dequeue());
            Assert.AreEqual(55, queue.Dequeue());
            Assert.AreEqual(-1, queue.Dequeue());
            Assert.AreEqual(-12, queue.Dequeue());
            Assert.AreEqual(-111, queue.Dequeue());
            Assert.AreEqual(-151, queue.Dequeue());
        }
开发者ID:vladislav-karamfilov,项目名称:TelerikAcademy,代码行数:27,代码来源:EnqueueOperationTests.cs

示例6: DijkstraAlgorithm

    private static Dictionary<char, string> DijkstraAlgorithm(Node<char> startingNode, HashSet<char> visitedNodes)
    {
        Dictionary<char, string> paths = new Dictionary<char, string>();
        paths[startingNode.Symbol] = startingNode.Symbol.ToString();

        PriorityQueue<Node<char>> queue = new PriorityQueue<Node<char>>();
        startingNode.Weight = 0;
        queue.Enqueue(startingNode);

        while (queue.Count > 0)
        {
            Node<char> currentNode = queue.Dequeue();
            visitedNodes.Add(currentNode.Symbol);
            foreach (var connection in currentNode.Connections)
            {
                Node<char> toNode = connection.ToNode;
                if (!visitedNodes.Contains(toNode.Symbol))
                {
                    long temporaryWeight = currentNode.Weight + connection.Distance;
                    if (temporaryWeight < toNode.Weight)
                    {
                        toNode.Weight = temporaryWeight;
                        queue.Enqueue(toNode);

                        paths[toNode.Symbol] = paths[currentNode.Symbol] + " -> " +
                            toNode.Symbol + "(" + toNode.Weight + ")";
                    }
                }
            }
        }

        return paths;
    }
开发者ID:RamiAmaire,项目名称:TelerikAcademy,代码行数:33,代码来源:Demo.cs

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

示例8: DjikstraAlgo

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

        foreach (var node in graph)
        {
            node.Key.DjikstraDistance = long.MaxValue;
        }

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

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

            if (currentNode.DjikstraDistance == long.MaxValue)
            {
                break;
            }

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

                if (potDistance < connection.ToNode.DjikstraDistance)
                {
                    connection.ToNode.DjikstraDistance = potDistance;
                    queue.Enqueue(connection.ToNode);
                }
            }
        }
    }
开发者ID:hristian-dimov,项目名称:TelerikAcademy,代码行数:33,代码来源:Program.cs

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

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

示例11: EnqueueAndDequeueOutOfOrder

        public void EnqueueAndDequeueOutOfOrder()
        {
            PriorityQueue queue = new PriorityQueue();
            Assert.False(queue.IsDataAvailable);
            queue.Enqueue(new PriorityQueueEntry(Priority.Pri1));
            queue.Enqueue(new PriorityQueueEntry(Priority.Ping));
            queue.Enqueue(new PriorityQueueEntry(Priority.Pri3));
            Assert.True(queue.IsDataAvailable);

            PriorityQueueEntry output;
            Assert.True(queue.TryDequeue(out output));
            Assert.NotNull(output);
            Assert.Equal(Priority.Ping, output.Priority);
            Assert.True(queue.IsDataAvailable);

            Assert.True(queue.TryDequeue(out output));
            Assert.NotNull(output);
            Assert.Equal(Priority.Pri1, output.Priority);
            Assert.True(queue.IsDataAvailable);

            Assert.True(queue.TryDequeue(out output));
            Assert.NotNull(output);
            Assert.Equal(Priority.Pri3, output.Priority);

            Assert.False(queue.IsDataAvailable);
        }
开发者ID:Tratcher,项目名称:HTTP-SPEED-PLUS-MOBILITY,代码行数:26,代码来源:PriorityQueueTests.cs

示例12: Count_Returns_Correct_Count

        public void Count_Returns_Correct_Count()
        {
            var queue = new PriorityQueue<int>(intComparer);
            queue.Enqueue(1);
            queue.Enqueue(2);

            Assert.AreEqual(2, queue.Count);
        }
开发者ID:WaveServiceBus,项目名称:WaveServiceBus,代码行数:8,代码来源:PriorityQueueTests.cs

示例13: DijkstraAlgorithm

    public static List<int> DijkstraAlgorithm(Dictionary<Node, Dictionary<Node, int>> graph, Dictionary<int, Node> nodes, int sourceNode, int destinationNode)
    {
        int[] previous = new int[graph.Count];
        bool[] visited = new bool[graph.Count];
        PriorityQueue<Node> priorityQueue = new PriorityQueue<Node>();
        var startNode = nodes[sourceNode];
        startNode.DistanceFromStart = 100000;

        for (int i = 0; i < previous.Length; i++)
        {
            previous[i] = -1;
        }

        priorityQueue.Enqueue(startNode);

        while (priorityQueue.Count > 0)
        {
            var currentNode = priorityQueue.ExtractMin();

            if (currentNode.Index == destinationNode)
            {
                break;
            }

            foreach (var edge in graph[currentNode])
            {
                if (!visited[edge.Key.Index])
                {
                    priorityQueue.Enqueue(edge.Key);
                    visited[edge.Key.Index] = true;
                }

                var distance = ((double)currentNode.DistanceFromStart / 100000) * ((double)edge.Value / 100000) * 100000;
                if (distance > edge.Key.DistanceFromStart && edge.Key.Index != sourceNode)
                {
                    edge.Key.DistanceFromStart = (int)distance;
                    previous[edge.Key.Index] = currentNode.Index;
                    priorityQueue.DecreaseKey(edge.Key);
                }
            }
        }

        if (previous[destinationNode] == -1)
        {
            return null;
        }

        List<int> path = new List<int>();
        int current = destinationNode;
        while (current != -1)
        {
            path.Add(current);
            current = previous[current];
        }

        path.Reverse();
        return path;
    }
开发者ID:vangelov-i,项目名称:Fundamentals,代码行数:58,代码来源:Program.cs

示例14: EnqueueTwoElementsTest

        public void EnqueueTwoElementsTest()
        {
            PriorityQueue<string> queue = new PriorityQueue<string>();

            queue.Enqueue("Pesho");
            queue.Enqueue("Gosho");

            Assert.AreEqual(2, queue.Count);
        }
开发者ID:vladislav-karamfilov,项目名称:TelerikAcademy,代码行数:9,代码来源:EnqueueOperationTests.cs

示例15: PriorityQueuePeekTest

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

            queue.Enqueue (5, 3);
            queue.Enqueue (6, 2);

            Assert.AreEqual (6, queue.Peek ());
        }
开发者ID:patrickuhlmann,项目名称:Stuff,代码行数:9,代码来源:PriorityQueueTest.cs


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