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


C# PriorityQueue.deleteMin方法代码示例

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


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

示例1: Dijkstras

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////  Dijktra's Algorithm ////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /**
        * This function will implement Dijkstra's Algorithm to find the shortest path to all the nodes from the source node
        * Time Complexity: O((|V| + |E|) log|V|) with a heap as it iterates over all the nodes and all the edges and uses a queue to go
        * through them where the heap queue has a worst case of log|V|. Whereas if the queue was implemented with an array, the complexity
        * would be O((|V|^2) since the queue has a worst case of |V| and |E| is upper bounded by |V|^2 and so |V|^2 dominates.
        * Space Complexity: O(|V|) as it creates arrays as big as the number of nodes in the graph
        */
        private List<int> Dijkstras(ref PriorityQueue queue, bool isArray)
        {
            // Create Queue to track order of points
            queue.makeQueue(points.Count);
            // Set up prev node list
            List<int> prev = new List<int>();
            List<double> dist = new List<double>();
            for (int i = 0; i < points.Count; i++)
            {
                prev.Add(-1);
                dist.Add(double.MaxValue);
            }

            // Initilize the start node distance to 0
            dist[startNodeIndex] = 0;
            
            // Update Priority Queue to reflect change in start point distance
            if (isArray)
                queue.insert(startNodeIndex, 0);
            else
                queue.insert(ref dist, startNodeIndex);

            // Iterate while the queue is not empty
            while (!queue.isEmpty())
            {
                // Grab the next min cost Point
                int indexOfMin;
                if (isArray)
                    indexOfMin = queue.deleteMin();
                else
                    indexOfMin = queue.deleteMin(ref dist);

                PointF u = points[indexOfMin];
                
                // For all edges coming out of u
                foreach (int targetIndex in adjacencyList[indexOfMin])
                {
                    PointF target = points[targetIndex];
                    double newDist = dist[indexOfMin] + computeDistance(u, target);
                    if (dist[targetIndex] > newDist)
                    {
                        prev[targetIndex] = indexOfMin;
                        dist[targetIndex] = newDist;
                        if (isArray)
                            queue.decreaseKey(targetIndex, newDist);
                        else
                            queue.decreaseKey(ref dist, targetIndex);
                    }
                }
            }
            return prev;
        }
开发者ID:YazanHalawa,项目名称:CS-312,代码行数:66,代码来源:Form1.cs


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