Example #1
0
        public static void Main1(string[] args)
        {
            Graph graph = new Graph(5);

            graph.AddEdge(1, 2, 10);
            graph.AddEdge(1, 5, 5);
            graph.AddEdge(2, 3, 1);
            graph.AddEdge(2, 5, 2);
            graph.AddEdge(3, 4, 4);
            graph.AddEdge(4, 1, 7);
            graph.AddEdge(4, 3, 6);
            graph.AddEdge(5, 2, 3);
            graph.AddEdge(5, 3, 9);
            graph.AddEdge(5, 4, 2);

            graph.PrintGraph();

            graph.Dijkstra(1);
        }
        static void ParseToGraphAndSolveDijkstra(string filename)
        {
            filename = "C:\\Users\\vixterisk\\Desktop\\MPL\\Сравнение языков\\" + filename;
            var edges       = new List <Edge>();
            var input       = File.ReadAllLines(filename);
            var startNode   = input[0];
            var desiredNode = input[1];

            for (int i = 2; i < input.Length; i++)
            {
                var str = input[i].Split();
                edges.Add(new Edge(str[0], str[1], Int32.Parse(str[2])));
            }
            var    graph  = new Graph(edges);
            var    result = graph.Dijkstra(startNode, desiredNode);
            string name   = filename.Substring(0, filename.Length - 4) + "-CSharp-output.txt";

            File.WriteAllLines(name, new string[] { result.ToString() });
        }
        static void Main(string[] args)
        {
            /* Let us create the example
             * graph discussed above */
            //int[][] graph = {
            //    new [] {0, 4, 0, 0, 0, 0, 0, 8, 0},
            //    new [] {4, 0, 8, 0, 0, 0, 0, 11, 0},
            //    new [] {0, 8, 0, 7, 0, 4, 0, 0, 2},
            //    new [] {0, 0, 7, 0, 9, 14, 0, 0, 0},
            //    new [] {0, 0, 0, 9, 0, 10, 0, 0, 0},
            //    new [] {0, 0, 4, 14, 10, 0, 2, 0, 0},
            //    new [] {0, 0, 0, 0, 0, 2, 0, 1, 6},
            //    new [] {8, 11, 0, 0, 0, 0, 1, 0, 7},
            //    new [] {0, 0, 2, 0, 0, 0, 6, 7, 0}};

            int[][] graphDef =
            {
                new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            };

            graphDef[0][1] = 4;
            graphDef[0][7] = 8;

            graphDef[1][0] = 4;
            graphDef[1][2] = 8;
            graphDef[1][7] = 11;

            graphDef[2][1] = 8;
            graphDef[2][3] = 7;
            graphDef[2][5] = 4;
            graphDef[2][8] = 2;

            graphDef[3][2] = 7;
            graphDef[3][4] = 9;
            graphDef[3][5] = 14;

            graphDef[4][3] = 9;
            graphDef[4][5] = 10;

            graphDef[5][2] = 4;
            graphDef[5][3] = 14;
            graphDef[5][4] = 10;

            graphDef[6][7] = 1;
            graphDef[6][8] = 6;

            graphDef[7][8] = 7;

            graphDef[8][2] = 2;
            graphDef[8][6] = 6;
            graphDef[8][7] = 7;

            var cfg = new GFG();

            cfg.Dijkstra(graphDef, 0);


            var graph    = new Graph(graphDef);
            var dijkstra = graph.Dijkstra(0);
        }