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


C# PriorityQueue.ExtractMin方法代码示例

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


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

示例1: Compute

        /// <summary>
        /// The dijkstra's shortest path procedure for a directed &quot; non-negative &quot; weighted graphs.
        /// </summary>
        /// <param name="directedGraph">
        /// a directed graph containing a set V of n vertices 
        /// and a set E of m directed edges with nonnegative weights.
        /// </param>
        /// <param name="sourceVertex">a source vertex in V.</param>
        /// <remarks>
        /// Result:
        ///     For each non-source vertex v in V, shortest[v] is the weight sp(s,v) of a shortest path from s to v and pred(v) is the vertex
        ///     preceding v on some shortest path.
        ///     For the source vertex s, shortest(s) = 0 and pred(s) = NULL.
        ///     If there is no path from s to v, then shortest[v] = infinity, and pred(v) = NULL.
        /// </remarks>
        public void Compute(IGraph directedGraph, int sourceVertex)
        {
            Graph.Shortest = new double[directedGraph.N];
            Graph.Predecessors = new int?[directedGraph.N];

            for (int i = 0; i < directedGraph.N; ++i)
            {
                Graph.Shortest[i] = double.PositiveInfinity;
                Graph.Predecessors[i] = null;
            }

            Graph.Shortest[sourceVertex] = 0;

            var queue = new PriorityQueue<int>();

            foreach (var v in directedGraph.Vertices)
                queue.Insert(v);

            while (queue.Count > 0)
            {
                var u = queue.ExtractMin();
                double tempShortest;

                foreach (var v in directedGraph[u])
                {
                    tempShortest = Shortest[v];

                    Graph.Relax(directedGraph, u, v);

                    if (tempShortest != Shortest[v])
                        queue.DecreaseKey(v);
                }
            }
        }
开发者ID:ioab,项目名称:AU,代码行数:49,代码来源:Dijkstra.cs

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

示例3: FindPathTo

        public LinkedList<Vector3> FindPathTo(Node end)
        {
            HashSet<Node> closedSet = new HashSet<Node>();
            PriorityQueue openSet = new PriorityQueue();
            Dictionary<Node, int> g_score = new Dictionary<Node, int>();
            Dictionary<Node, Node> cameFrom = new Dictionary<Node, Node>();

            g_score.Add(this, 0);
            openSet.Insert(heuristicDistance(this, end), this);

            while (openSet.Length > 0)
            {
                Node currentNode = openSet.ExtractMin();

                if (currentNode == end)
                {
                    LinkedList<Vector3> path = ReconstructPath(cameFrom, end);
                    return path;
                }

                closedSet.Add(currentNode);

                if (currentNode.neighbours != null && currentNode.neighbours.Length > 0)
                {
                    foreach (var neighbour in currentNode.neighbours)
                    {
                        if (neighbour.cost >= 100)
                            continue;

                        int tentative_g_score = g_score[currentNode] + RealDistance(currentNode, neighbour);

                        if (closedSet.Contains(neighbour))
                        {
                            if(tentative_g_score >= g_score[currentNode])
                                continue;
                            else
                            {
                                Debug.Log("Remove");
                                closedSet.Remove(currentNode);
                            }
                        }

                        if (openSet.Contains(neighbour))
                        {
                            if (tentative_g_score >= g_score[neighbour])
                                continue;
                            else
                                openSet.DecreaseKey(tentative_g_score + heuristicDistance(neighbour, end), neighbour);
                        }
                        else
                            openSet.Insert(tentative_g_score + heuristicDistance(neighbour, end), neighbour);

                        cameFrom[neighbour] = currentNode;
                        g_score[neighbour] = tentative_g_score;
                    }
                }
            }

            return null;
        }
开发者ID:Tb95,项目名称:CellularAutomata,代码行数:60,代码来源:Pathfinding.cs


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