Beispiel #1
0
 public void addNode(Node node, int distance)
 {
     if (!Adjacentnodes.Contains(node))
     {
         this.Adjacentnodes.Add(node);       //puts adjacent nodes in list
         this.Distances.Add(node, distance); //calculates costs between nodes
         this.bestpath.Add(node, this);      //puts direct connection as best path for the time
         node.addNode(this, distance);
     }
 }
Beispiel #2
0
        static void Main(string[] args)
        {
            // uses graph examples from Github hogeschool Development 6a Graphs
            Node A = new Node("A");
            Node B = new Node("B");
            Node C = new Node("C");
            Node D = new Node("D");
            Node E = new Node("E");
            Node F = new Node("F");
            Node G = new Node("G");
            Node H = new Node("H");
            Node I = new Node("I");
            Node J = new Node("J");
            Node K = new Node("K");
            Node L = new Node("L");

            A.addNode(B, 2);
            A.addNode(C, 3);
            A.addNode(D, 2);
            B.addNode(E, 4);
            B.addNode(F, 2);
            D.addNode(G, 5);
            D.addNode(H, 3);
            E.addNode(I, 8);
            E.addNode(J, 7);
            G.addNode(K, 5);
            G.addNode(L, 3);

            List <Node> graph = new List <Node>()
            {
                A, B, C, D, E, F, G, H, I, J, K, L
            };

            Console.WriteLine("Bfs order:");
            BFS(graph, A, (n) => Console.WriteLine(n.Letter));
            Console.WriteLine("Dfs order:");
            DFS(graph, A, (n) => Console.WriteLine(n.Letter));


            // Graph example Hogeschool Dev6a/
            Node A2 = new Node("A");
            Node B2 = new Node("B");
            Node C2 = new Node("C");
            Node D2 = new Node("D");
            Node E2 = new Node("E");
            Node F2 = new Node("F");


            A2.addNode(B2, 8);
            A2.addNode(C2, 1);
            B2.addNode(D2, 2);
            C2.addNode(D2, 3);
            D2.addNode(E2, 4);
            D2.addNode(F2, 6);
            E2.addNode(F2, 1);

            List <Node> nodelist = new List <Node> {
                A2, B2, C2, D2, E2, F2
            };

            Console.WriteLine("Order dijkstra through graph from A: ");
            Dijkstra(nodelist, A2);
            Console.WriteLine("Order dijkstra through graph from F: ");
            Dijkstra(nodelist, F2);

            Console.WriteLine("Distance to point A to B: " + A2.getDistance(B2));
            Console.WriteLine("Distance to point F to A: " + F2.getDistance(A2));

            //puts path in reverse, from the destination to start improvements welcome
            Console.WriteLine("best path to A from B");
            A2.Bestpath(B2);
            Console.WriteLine("best path to F from A");
            F2.Bestpath(A2);
        }