Esempio n. 1
0
        private static void TestDijkstra()
        {
            //Graph for figure 4.15 part A
            Graph  g  = new Graph();
            Vertex u1 = new Vertex();
            Vertex u2 = new Vertex();
            Vertex u3 = new Vertex();
            Vertex u4 = new Vertex();
            Vertex u5 = new Vertex();
            Vertex u6 = new Vertex();

            g.AddEdge(new Edge(u1, u2, 2));
            g.AddEdge(new Edge(u1, u3, 8));
            g.AddEdge(new Edge(u2, u3, 5));
            g.AddEdge(new Edge(u2, u4, 3));
            g.AddEdge(new Edge(u3, u2, 6));
            g.AddEdge(new Edge(u3, u5, 0));
            g.AddEdge(new Edge(u4, u3, 1));
            g.AddEdge(new Edge(u4, u5, 7));
            g.AddEdge(new Edge(u4, u6, 6));
            g.AddEdge(new Edge(u5, u4, 4));
            g.AddEdge(new Edge(u6, u5, 2));

            Console.WriteLine("Dijkstra Output:");
            Graph tree = GraphAlgorithms.Dijkstra(g, 0, true);
        }
        public void DijkstraTest1()
        {
            Graph SomeGraph = new Graph();

            SortedDictionary <int, int> correctDijkstraAnswer = new SortedDictionary <int, int>
            {
                { 1, 0 },
                { 2, 8 },
                { 3, 3 },
                { 4, 9 }
            };

            SomeGraph.AddNode(1);
            SomeGraph.AddNode(2);
            SomeGraph.AddNode(3);
            SomeGraph.AddNode(4);
            SomeGraph.AddEdge(1, 2, 8);
            SomeGraph.AddEdge(1, 3, 3);
            SomeGraph.AddEdge(2, 4, 1);
            SomeGraph.AddEdge(3, 4, 10);
            SomeGraph.AddEdge(1, 4, 100);

            SortedDictionary <int, int> tryDijkstra = GraphAlgorithms.Dijkstra(SomeGraph, 1);

            CollectionAssert.AreEqual(correctDijkstraAnswer, tryDijkstra);
        }
Esempio n. 3
0
        private void onDijkstraClick(object sender, EventArgs e)
        {
            if (CurrentGraphPanel == null)
            {
                return;
            }

            //Make sure one vertex is selected
            List <ISelectable> selection = CurrentGraphPanel.GetSelection();
            GUIVertex          vert;

            if (selection.Count <= 0 || selection.Count > 1)
            {
                return;
            }
            else if (selection[0] as GUIVertex == null)
            {
                return;
            }
            else
            {
                vert = selection[0] as GUIVertex;
            }

            List <GUIVertex> verts = CurrentGraphPanel.Vertices;
            Graph            g     = CurrentGraphPanel.Graph;

            GraphAlgorithms.Dijkstra(g, g.GetVertices().IndexOf(vert.Vertex), g.Directed);
        }
Esempio n. 4
0
 // Dijkstra’s Algorithm.
 private void toolStripRightSaveShortestPath_Click(object sender, EventArgs e)
 {
     if (ListBoxImport.SelectedIndex != -1)
     {
         graphAlObj.Dijkstra(ListBoxImport.SelectedItem.ToString());
         ListBoxResults.Items.Add("Shortest Paths: " + ListBoxImport.SelectedItem);
     }
     else
     {
         MessageBox.Show("You have not selected a value from the list.", "Caution", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Esempio n. 5
0
        private int IntelligentGeneralChoice()
        {
            int i = 0;

            if (this.DestinationCards.Count < 3)
            {
                return((int)PlayerAction.DestinationCard);
            }
            if (firstloop)
            {
                ExtensionMethods.niceLayout("Dijkstra");
                double mini = 0;
                firstloop = false;
                List <List <Railroad> > shortestpaths = GraphAlgorithms.Dijkstra(AllTracks, citylist, this);
                List <City>             destinations  = new List <City>();
                ExtensionMethods.niceLayout("Vertaling naar railRoads");
                foreach (List <Railroad> path in shortestpaths)
                {
                    Program.logboek.WriteLine(this.GetDestinationCards()[i].ToString());
                    int totalamountOfCardsneeded = 0, amountOfStations = 0;
                    foreach (Railroad road in path)
                    {
                        destinations = road.getdestinations();
                        Program.logboek.WriteLine("\t" + destinations[0].getname() + " - " + destinations[1].getname());
                        if (!road.isOccupied())
                        {
                            totalamountOfCardsneeded += road.getAmountOfLocomotives() * 2 + road.getRailLength()
                                                        + ((road.IsTunnel()) ? 2 : 0);
                        }
                        else
                        {
                            amountOfStations++;
                        }
                    }
                    double trackvalue = (double)(GetDestinationCards()[i].getPoints()
                                                 + 4 - amountOfStations) / (double)(totalamountOfCardsneeded);
                    // empyrisch, hoe groter hoe beter
                    if (trackvalue > mini)
                    {
                        goalRoute = path; mini = trackvalue;
                    }
                    i++;
                }
                i = 1;
                foreach (Railroad road in goalRoute)
                {
                    if (road.isOccupied() && road.getPlayer().CompareTo(this) != 0)
                    {
                        if (i / goalRoute.Count > 0.5)
                        {
                            if (road.getdestinations()[0].getStation() == null)
                            {
                                goalStation = road.getdestinations()[0];
                                return((int)PlayerAction.Station);
                            }
                            else if ((road.getdestinations()[1].getStation() == null))
                            {
                                goalStation = road.getdestinations()[1];
                                return((int)PlayerAction.Station);
                            }
                        }
                    }

                    else if (road.isOccupied() == false)
                    {
                        Program.logboek.WriteLine(this.name + " is trying to build a road between: " + road.ToString());
                        if (ActionsMethods.Pickcards(this, road))
                        {
                            //goalRoad = road;
                            Maderoad = road;
                            return((int)PlayerAction.Railroad);
                        }
                    }
                    i++;
                }
                foreach (Railroad road in goalRoute)
                {
                    //goalRoad = road;
                    if (!road.isOccupied())
                    {
                        goalRoad = road;
                    }
                }
            }
            else
            {
                return((int)PlayerAction.Traincards);
            }
            return((int)PlayerAction.Traincards);
        }