Beispiel #1
0
        public void allPaths(bool foundEnd, int numPoints)
        {
            //start timer
            System.Diagnostics.Stopwatch timeAll = new System.Diagnostics.Stopwatch();
            timeAll.Start();

            //these indices correspond with the points, keep track of points, keys, and place in the queue
            Node[] nodeInfoAll = new Node[numPoints];
            for (int i = 0; i < numPoints; i++)
            {
                nodeInfoAll[i] = new Node(points[i], double.MaxValue, -1);
            }

            BinaryHeap bhAll = new BinaryHeap(numPoints + 1, nodeInfoAll, startNodeIndex, 0);

            while (bhAll.getQueueSize() > 0)
            {
                HeapNode n = bhAll.deleteMin();
                int      i = n.getPointListIndex();

                //find out all the other nodes this is connected to, decrease keys
                PointF a = points[i];
                foreach (int edge in adjacencyList[i])
                {
                    PointF b    = points[edge];
                    double dist = Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2));
                    if ((n.getKey()) + dist < nodeInfoAll[edge].getKey())
                    {
                        nodeInfoAll[edge].setKey(n.getKey() + dist);
                        bhAll.decreaseKey(nodeInfoAll[edge].getHeapIndex(), n.getKey() + dist);

                        nodeInfoAll[edge].setPrevNode(i);
                    }
                }
            }

            //we already know if there's a path from onePath, still ran algorithm to measure time
            if (foundEnd)
            {
                //drawPath
                int    currentIndexAll = stopNodeIndex;
                double pathLeft        = nodeInfoAll[stopNodeIndex].getKey();

                while (currentIndexAll != startNodeIndex)
                {
                    int    nextIndex = nodeInfoAll[currentIndexAll].getPrevNode();
                    PointF a         = points[currentIndexAll];
                    PointF b         = points[nextIndex];
                    graphics.DrawLine(new Pen(Color.Black), points[currentIndexAll], points[nextIndex]);

                    //draw the edge length
                    double distance = pathLeft - nodeInfoAll[nextIndex].getKey();
                    pathLeft -= distance;
                    graphics.DrawString(Math.Round(distance, 2).ToString(), new Font(this.Font, FontStyle.Regular),
                                        new SolidBrush(Color.Red), new PointF((a.X + b.X) / 2, (a.Y + b.Y) / 2));

                    currentIndexAll = nextIndex;
                }

                Refresh();
            }

            //report time
            allPathTime = timeAll.Elapsed.Milliseconds;
            allPathTime = allPathTime / 1000;
            Console.Write("here");
            allTimeBox.Text = allPathTime.ToString();

            differenceBox.Text = ((allPathTime - onePathTime) * 100 / allPathTime).ToString();
        }
Beispiel #2
0
        public bool onePath(int numPoints)
        {
            //start a timer
            System.Diagnostics.Stopwatch timeOne = new System.Diagnostics.Stopwatch();
            timeOne.Start();

            //these indices correspond with the points, keep track of points, keys, and place in the queue
            Node[] nodeInfoOne = new Node[numPoints];
            for (int i = 0; i < numPoints; i++)
            {
                nodeInfoOne[i] = new Node(points[i], double.MaxValue, -1);
            }

            BinaryHeap bhOne = new BinaryHeap(numPoints + 1, nodeInfoOne, startNodeIndex);

            //need this just for One Path
            bool foundEnd = false;

            //this will run until the stop node is found, or through all the nodes if never found
            while (bhOne.getQueueSize() > 0)
            {
                HeapNode n = bhOne.deleteMin();
                int      i = n.getPointListIndex();

                //we can stop our looping
                if (i == stopNodeIndex)
                {
                    //we already know its distance and previous pointer, we're done
                    foundEnd = true;
                    break;
                }

                //find out all the other nodes this one is connected to, decrease keys if applicable
                PointF a = points[i];
                foreach (int edge in adjacencyList[i])
                {
                    PointF b    = points[edge];
                    double dist = Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2));
                    if ((n.getKey()) + dist < nodeInfoOne[edge].getKey())
                    {
                        //-------------------------------------------------------------------
                        //insert onto the queue if not already in there, decrease key if it is
                        //-------------------------------------------------------------------
                        if (nodeInfoOne[edge].getHeapIndex() != -1)
                        {
                            nodeInfoOne[edge].setKey(n.getKey() + dist);
                            bhOne.decreaseKey(nodeInfoOne[edge].getHeapIndex(), n.getKey() + dist);

                            //the previous node changes when key is decreased
                            nodeInfoOne[edge].setPrevNode(i);
                        }
                        else
                        {
                            bhOne.insert(new HeapNode(n.getKey() + dist, edge));
                            nodeInfoOne[edge].setKey(n.getKey() + dist);
                            nodeInfoOne[edge].setPrevNode(i);
                        }
                    }
                }
            }

            if (!foundEnd)
            {
                pathCostBox.Text = "unreachable";
                //report time
                onePathTime     = timeOne.Elapsed.Milliseconds;
                onePathTime     = onePathTime / 1000;
                oneTimeBox.Text = onePathTime.ToString();

                return(false);
            }
            else
            {
                //draw the path
                int    currentIndexOne = stopNodeIndex;
                double pathLeft        = nodeInfoOne[stopNodeIndex].getKey();
                while (currentIndexOne != startNodeIndex)
                {
                    int    nextIndex = nodeInfoOne[currentIndexOne].getPrevNode();
                    PointF a         = points[currentIndexOne];
                    PointF b         = points[nextIndex];
                    graphics.DrawLine(new Pen(Color.Black), a, b);

                    //draw the edge length
                    double distance = pathLeft - nodeInfoOne[nextIndex].getKey();
                    pathLeft -= distance;
                    graphics.DrawString(Math.Round(distance, 2).ToString(), new Font(this.Font, FontStyle.Regular),
                                        new SolidBrush(Color.Red), new PointF((a.X + b.X) / 2, (a.Y + b.Y) / 2));

                    currentIndexOne = nextIndex;
                }

                //report time
                onePathTime     = timeOne.Elapsed.Milliseconds;
                onePathTime     = onePathTime / 1000;
                oneTimeBox.Text = onePathTime.ToString();

                //report cost
                pathCostBox.Text = nodeInfoOne[stopNodeIndex].getKey().ToString();
                return(true);
            }
        }