Example #1
0
        //startNodeIndex
        //stopNodeIndex
        //points
        //adjacencylist
        private PriorityQueue Dijkstra(String type)
        {
            double    maxTime   = 0;
            String    whichType = "";
            Stopwatch myWatch   = new Stopwatch();


            PriorityQueue pq = null;

            if (type == "array")
            {
                pq = new ArrayQueue(points.Count);
            }
            else if (type == "heap")
            {
                pq = new HeapQueue(points.Count);
            }
            pq.changeDistance(startNodeIndex, 0);
            pq.formerIndexes[startNodeIndex] = startNodeIndex;
            for (int i = 0; i < points.Count - 1; i++)
            {
                myWatch.Reset();
                myWatch.Start();
                int deletedIndex = pq.deleteMinimum();
                myWatch.Stop();
                //Console.WriteLine("deleteTime: " + myWatch.Elapsed.TotalSeconds.ToString());
                if (myWatch.Elapsed.TotalSeconds > maxTime)
                {
                    maxTime = myWatch.Elapsed.TotalSeconds; whichType = "deleteTime";
                }
                foreach (int neighbor in adjacencyList[deletedIndex])
                {
                    double newDistance = distanceAtoB(points[deletedIndex], points[neighbor]) + pq.distances[deletedIndex];
                    if (newDistance < pq.distances[neighbor])
                    {
                        myWatch.Reset();
                        myWatch.Start();
                        pq.changeDistance(neighbor, newDistance);
                        myWatch.Stop();
                        //Console.WriteLine("changeDistanceTime: " + myWatch.Elapsed.TotalSeconds.ToString());
                        if (myWatch.Elapsed.TotalSeconds > maxTime)
                        {
                            maxTime = myWatch.Elapsed.TotalSeconds; whichType = "changeDistanceTime";
                        }
                        pq.formerIndexes[neighbor] = deletedIndex;
                    }
                }
            }
            //Console.WriteLine("MaxTime: " + whichType + " " + maxTime);
            return(pq);
        }
Example #2
0
        //bob
        private void solveButton_Clicked()
        {
            IQueue       AQ         = new ArrayQueue();
            IQueue       HA         = new HeapArray();
            DykstrasAlgo dykstrasAQ = new DykstrasAlgo(points, startNodeIndex, adjacencyList, AQ);
            DykstrasAlgo dykstrasHA = new DykstrasAlgo(points, startNodeIndex, adjacencyList, AQ);

            if (arrayCheckBox.Checked)
            {
                Stopwatch timer = new Stopwatch();
                timer.Start();
                dykstrasAQ.SolveDykstras();
                timer.Stop();
                arrayTimeBox.Text = "" + timer.Elapsed.TotalSeconds;

                Stopwatch timer2 = new Stopwatch();
                timer2.Start();
                dykstrasHA.SolveDykstras();
                timer2.Stop();
                heapTimeBox.Text   = "" + timer2.Elapsed.TotalSeconds;
                differenceBox.Text = "" + (timer.Elapsed.TotalSeconds / timer2.Elapsed.TotalSeconds);
            }
            else
            {
                Stopwatch timer2 = new Stopwatch();
                timer2.Start();
                dykstrasHA.SolveDykstras();
                timer2.Stop();
                heapTimeBox.Text = "" + timer2.Elapsed.TotalSeconds;
            }
            int[]  prev     = dykstrasHA.GetPrevList();
            string printMsg = "";

            foreach (int index in prev)
            {
                printMsg += index + " ";
            }
            List <int> list     = CreateOrderList(prev, stopNodeIndex);
            float      distance = 0;

            for (int i = 1; i < list.Count; ++i)
            {
                PaintTwoPoints(points[list.ElementAt(i - 1)], points[list.ElementAt(i)]);
                distance += GetDistance(points[list.ElementAt(i - 1)], points[list.ElementAt(i)]);
            }
            pathCostBox.Text = distance.ToString();
        }
Example #3
0
        int[] g_arrayHQ;            // Same function, just for the heap

        // Creates an ArrayQueue and calls Dykstra's with the ArrayQueue Arrays from above.
        private void runArrayQueue()
        {
            ArrayQueue queue = new ArrayQueue();

            Dykstra(queue, g_distanceAQ, g_previousAQ, g_arrayAQ);

            /*
             * int currIndex = stopNodeIndex;
             * string path = "Path: ";
             * while (currIndex != startNodeIndex)
             * {
             *  if (currIndex < 0)
             *      break;
             *  path += (currIndex + "-");
             *  currIndex = g_previousAQ[currIndex];
             *  if (currIndex == startNodeIndex)
             *  {
             *      path += startNodeIndex;
             *      MessageBox.Show(path);
             *  }
             * }
             */
        }
Example #4
0
        private void solveButton_Clicked()
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            double[] dist = new double[points.Count];
            double[] prev = new double[points.Count];

            for (int i = 0; i < points.Count; i++)
            {
                dist[i] = double.MaxValue;
                prev[i] = -1;
            }
            //Console.Write("I GET HERE");
            dist[startNodeIndex] = 0;
            PriorityQueue myqueue = new ArrayQueue(dist);

            //Console.WriteLine("i make it here");
            while (myqueue.getSize() > 0)
            {
                //Console.WriteLine("i check for removed");
                int removed = myqueue.remove();
                //Console.WriteLine("my removed is " + removed);

                foreach (int x in adjacencyList[removed])
                {
                    //Console.WriteLine(x);
                    double newdistance = dist[removed] + calculatedistance(points[removed], points[x]);
                    if (newdistance < dist[x])
                    {
                        dist[x] = newdistance;
                        prev[x] = removed;
                        // Console.WriteLine("my prev is "+removed);
                        myqueue.update(x, newdistance);
                    }
                }
            }
            //Console.WriteLine("I do get out of the main thing");
            List <double> reversepathstuff = new List <double>();
            double        temp             = stopNodeIndex;


            while (temp != startNodeIndex)
            {
                reversepathstuff.Add(temp);
                temp = prev[(int)temp];
            }
            reversepathstuff.Add(temp);
            //Console.WriteLine("I survive to here as well");
            double total = 0;

            for (int i = 0; i < reversepathstuff.Count; i++)
            {
                if (i != reversepathstuff.Count - 1)
                {
                    graphics.DrawLine(Pens.Blue, points[(int)reversepathstuff[i]], points[(int)reversepathstuff[i + 1]]);
                    graphics.DrawString(Convert.ToString((int)calculatedistance(points[(int)reversepathstuff[i]], points[(int)reversepathstuff[i + 1]])), new Font("Ariel", 12), new SolidBrush(System.Drawing.Color.Black), new PointF((points[(int)reversepathstuff[i]].X + points[(int)reversepathstuff[i + 1]].X) / 2, (points[(int)reversepathstuff[i]].Y + points[(int)reversepathstuff[i + 1]].Y) / 2));
                    total += calculatedistance(points[(int)reversepathstuff[i]], points[(int)reversepathstuff[i + 1]]);
                }
            }
            pathCostBox.Text = Convert.ToString(total);
            stopWatch.Stop();
            TimeSpan ts          = stopWatch.Elapsed;
            string   elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                 ts.Hours, ts.Minutes, ts.Seconds,
                                                 ts.Milliseconds / 10.0);

            arrayTimeBox.Text = elapsedTime;

            pathCostBox.Text = Convert.ToString(total);
            Console.WriteLine(elapsedTime);
            Console.WriteLine(ts.TotalMilliseconds);
            //points connect to adjacency list sets corresponding on the index


            // *** Implement this method, use the variables "startNodeIndex" and "stopNodeIndex" as the indices for your start and stop points, respectively ***
        }