Esempio n. 1
0
        private static void testBasic2()
        {
            // Build the list of points
            List<PointF> points = new List<PointF>();
            points.Add(new PointF(485.0F, 164.0F));
            points.Add(new PointF(501.0F, 184.0F));
            points.Add(new PointF(293.0F, 131.0F));
            points.Add(new PointF(215.0F, 151.0F));
            points.Add(new PointF(263.0F, 185.0F));
            points.Add(new PointF(455.0F, 21.0F));
            points.Add(new PointF(480.0F, 30.0F));
            points.Add(new PointF(477.0F, 213.0F));
            points.Add(new PointF(119.0F, 235.0F));
            points.Add(new PointF(122.0F, 31.0F));
            points.Add(new PointF(400.0F, 211.0F));
            points.Add(new PointF(343.0F, 255.0F));

            // Build the adjacency list
            List<HashSet<int>> adjacencyList = new List<HashSet<int>>();
            for (int i = 0; i < points.Count; i++)
                adjacencyList.Add(new HashSet<int>());

            adjacencyList[0].Add(1);
            adjacencyList[0].Add(7);
            adjacencyList[0].Add(10);
            adjacencyList[1].Add(2);
            adjacencyList[1].Add(6);
            adjacencyList[1].Add(4);
            adjacencyList[2].Add(11);
            adjacencyList[2].Add(4);
            adjacencyList[2].Add(6);
            adjacencyList[3].Add(9);
            adjacencyList[3].Add(8);
            adjacencyList[3].Add(0);
            adjacencyList[4].Add(0);
            adjacencyList[4].Add(10);
            adjacencyList[4].Add(1);
            adjacencyList[5].Add(10);
            adjacencyList[5].Add(9);
            adjacencyList[5].Add(2);
            adjacencyList[6].Add(7);
            adjacencyList[6].Add(5);
            adjacencyList[6].Add(0);
            adjacencyList[7].Add(11);
            adjacencyList[7].Add(5);
            adjacencyList[7].Add(6);
            adjacencyList[8].Add(6);
            adjacencyList[8].Add(4);
            adjacencyList[8].Add(5);
            adjacencyList[9].Add(1);
            adjacencyList[9].Add(2);
            adjacencyList[9].Add(11);
            adjacencyList[10].Add(0);
            adjacencyList[10].Add(8);
            adjacencyList[10].Add(6);
            adjacencyList[11].Add(5);
            adjacencyList[11].Add(10);
            adjacencyList[11].Add(7);

            int startNodeIndex = 3;
            int stopNodeIndex = 7;

            string expected = "3, 0, 7";

            // Array test
            IDijkstraShortestPathQueue arrayQ = new ArrayImpl(points.Count);
            List<int> arrayPath = PathSolver.findShortestPath(arrayQ, points, adjacencyList, startNodeIndex, stopNodeIndex);
            string arrayResult = PathSolver.pathToString(arrayPath);
            Debug.Assert(expected.Equals(arrayResult));

            // Heap test
            IDijkstraShortestPathQueue heapQ = new HeapArrayImpl(points.Count);
            List<int> heapPath = PathSolver.findShortestPath(heapQ, points, adjacencyList, startNodeIndex, stopNodeIndex);
            string heapResult = PathSolver.pathToString(heapPath);
            Debug.Assert(expected.Equals(heapResult));
        }
Esempio n. 2
0
        // Use this to generate paths (from start) to every node; then, just return the path of interest from start node to end node
        private void solveButton_Click(object sender, EventArgs e)
        {
            // This was the old entry point, but now it is just some form interface handling
            bool ready = true;

            if(startNodeIndex == -1)
            {
                sourceNodeBox.Focus();
                sourceNodeBox.BackColor = Color.Red;
                ready = false;
            }
            if(stopNodeIndex == -1)
            {
                if(!sourceNodeBox.Focused)
                    targetNodeBox.Focus();
                targetNodeBox.BackColor = Color.Red;
                ready = false;
            }
            if (points.Count > 0)
            {
                resetImageToPoints(points);
                paintStartStopPoints();
            }
            else
            {
                ready = false;
            }
            if(ready)
            {
                clearSome();

                IDijkstraShortestPathQueue solver = null;
                List<int> arrayPath = null;
                double arraySeconds = -1.0;
                double heapSeconds = -1.0;
                string timerBoxFormat = "F6";

                // Run the array implementation
                if (arrayCheckBox.Checked)
                {
                    solver = new ArrayImpl(points.Count);
                    Stopwatch arrayTimer = new Stopwatch();
                    arrayTimer.Start();
                    arrayPath = PathSolver.findShortestPath(solver, points, adjacencyList, startNodeIndex, stopNodeIndex);
                    arrayTimer.Stop();
                    arraySeconds = arrayTimer.Elapsed.TotalMilliseconds / 1000;
                }

                // Run the heap implementation
                solver = new HeapArrayImpl(points.Count);
                Stopwatch heapTimer = new Stopwatch();
                heapTimer.Start();
                List<int> heapPath = PathSolver.findShortestPath(solver, points, adjacencyList, startNodeIndex, stopNodeIndex);
                heapTimer.Stop();
                heapSeconds = heapTimer.Elapsed.TotalMilliseconds / 1000;

                // If the list is null, then there isn't a path to the specified node
                if(heapPath == null)
                {
                    pathCostBox.Text = "NO PATH";
                }
                else
                {
                    // Verify that both got the same paths
                    if (arrayCheckBox.Checked)
                        verifyDifferentImplementationPaths(arrayPath, heapPath);

                    // Draw the path
                    float pathCost = drawPath(heapPath);
                    pathCostBox.Text = pathCost.ToString(timerBoxFormat);

                    // Set the appropriate times
                    if (arrayCheckBox.Checked)
                        arrayTimeBox.Text = arraySeconds.ToString(timerBoxFormat);
                    heapTimeBox.Text = heapSeconds.ToString(timerBoxFormat);

                    // Speed up comparison
                    if (arrayCheckBox.Checked)
                        differenceBox.Text = (arraySeconds / heapSeconds).ToString(timerBoxFormat);
                }
            }
        }