示例#1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////  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);
        }
示例#2
0
        //-----------------------------------------------------------------------------//
        //-------------------------- Dijkstra's Algorithm -----------------------------//
        //-----------------------------------------------------------------------------//


        /**
         * This Dijkstra's is implemented based off of the pseudocode found in the text.
         * Time complexity for the Binary Heap Priority queue is O(|V| * |logV|)
         * because the implementation requires all the nodes to be visited but the
         * update methods require |log V| time.  The array priority queue is O(|V|^2)
         * because it has to visit all the nodes on implementation and has to itterate
         * through the nodes in order to delete and update the queue.  The space complexity
         * for both is O(|V|) because each queue stores all of the nodes generated
         **/
        private List <int> dijkstrasAlgorithm(PriorityQueue queue, bool isArray)
        {
            queue.makeQueue(points.Count);

            List <int>    previous  = new List <int>();
            List <double> distances = new List <double>();

            // Sets all distance values to "infinity"
            // sets previous as 'undefined'
            for (int i = 0; i < points.Count; i++)
            {
                previous.Add(-1);
                distances.Add(double.MaxValue);
            }

            // distance to start node
            distances[startNodeIndex] = 0;

            // checks which priority queue will be used
            if (isArray)
            {
                queue.insert(startNodeIndex, 0);
            }
            else
            {
                queue.insert(ref distances, startNodeIndex);
            }

            // main loop
            // loops until there arent any nodes with a permanent distance to the end node
            while (!queue.isEmpty())
            {
                int minIndex;

                if (isArray)
                {
                    minIndex = queue.deleteMin();
                }
                else
                {
                    minIndex = queue.deleteMin(ref distances);
                }

                PointF u = points[minIndex];

                // finds the best path amongst its neighbors, if it exists
                foreach (int index in adjacencyList[minIndex])
                {
                    PointF alt         = points[index];
                    double altDistance = distances[minIndex] + distanceBetween(u, alt);

                    if (altDistance < distances[index])
                    {
                        previous[index]  = minIndex;
                        distances[index] = altDistance;

                        if (isArray)
                        {
                            queue.decreaseKey(index, altDistance);
                        }
                        else
                        {
                            queue.decreaseKey(ref distances, index);
                        }
                    }
                }
            }

            //queue.printQueue();

            // gives the path of the nodes
            return(previous);
        }