Ejemplo n.º 1
0
        /// <summary>
        /// Get path from @from to @to
        /// </summary>
        /// <param name="from">Start node</param>
        /// <param name="to">End node</param>
        /// <returns>Value with path</returns>
        public virtual IShortestPathResult Process(uint from, uint to)
        {
            var result = new DijkstraResult(from, to);

            Graph[from].Distance = 0;
            var q       = new SortedSet <INode <T, TEdgeCustom> >(new[] { Graph[from] }, new NodeComparer <T, TEdgeCustom>());
            var current = new HashSet <uint>();

            while (q.Count > 0)
            {
                INode <T, TEdgeCustom> u = q.Deque();
                current.Remove(u.Key);

                if (u.Key == to)
                {
                    result.Distance = u.Distance;
                    break;
                }

                u.EachChild((in Edge <T, TEdgeCustom> e) =>
                {
                    if (e.Node.Distance > u.Distance + e.Cost)
                    {
                        if (current.Contains(e.Node.Key))
                        {
                            q.Remove(e.Node);
                        }

                        e.Node.Distance = u.Distance + e.Cost;
                        q.Add(e.Node);
                        current.Add(e.Node.Key);
                        result.Path[e.Node.Key] = u.Key;
                    }
                });
Ejemplo n.º 2
0
        //calculates the shorted path
        public static void shortest(TextBox cost, TextBox arrayTimeBox, TextBox heapTimeBox, TextBox xspeedupBox, int startingIndex, List <PointF> points
                                    , List <HashSet <int> > connections, int stopIndex, Graphics graphics, bool compare)
        {
            DijkstraResult heapResult = dijkstra(startingIndex, points, connections, PriorityQueueFactory.QueueType.Heap);
            //Grabbing the results of dijkstra
            List <int>    prev         = heapResult.getPrev();
            List <double> dist         = heapResult.getDist();
            int           currentIndex = stopIndex;
            bool          isPath       = true;

            //iterating through the result to make sure a complete path exists and to draw the path
            while (currentIndex != startingIndex)
            {
                //if the path doesn't exist then there is no reason to continue, and the cost is unreachable
                if (currentIndex != startingIndex && prev[currentIndex] == -1)
                {
                    isPath = false;
                    break;
                }
                PointF previousNode = points[prev[currentIndex]];
                PointF currentNode  = points[currentIndex];
                graphics.DrawLine(Pens.Black, currentNode, previousNode);
                double distance = dist[currentIndex] - dist[prev[currentIndex]];
                graphics.DrawString(String.Format("{0}", (int)distance), SystemFonts.DefaultFont, Brushes.Black, midpoint(currentNode, previousNode));
                currentIndex = prev[currentIndex];
            }
            if (isPath)
            {
                //if the user wants to compare the array to the binary heap times we have to do dijkstras with the array
                if (compare)
                {
                    DijkstraResult arrayResult = dijkstra(startingIndex, points, connections, PriorityQueueFactory.QueueType.Array);
                    arrayTimeBox.Text = String.Format("{0}", arrayResult.getTime());
                    xspeedupBox.Text  = String.Format("{0}", arrayResult.getTime() / heapResult.getTime());
                }
                cost.Text        = String.Format("{0}", (int)dist.Last());
                heapTimeBox.Text = String.Format("{0}", heapResult.getTime());
            }
            else
            {
                cost.Text = "Unreachable";
            }
        }