Пример #1
0
        static void GraphTheory_FloydWarshallAlgo()
        {
            int[,] graph =
            {
                { 0, 8, 0, 1 },
                { 0, 0, 1, 0 },
                { 4, 0, 0, 0 },
                { 0, 2, 9, 0 }
            };

            ShortedPathAlgo algo      = new ShortedPathAlgo();
            List <int[, ]>  solutions = algo.FloydWarshall_AllpairsShortestPath(graph, 4);

            Console.WriteLine("FloydWarshall's algo");

            int iLoop = 0;

            foreach (int[,] solution in solutions)
            {
                Console.WriteLine("--- Solution - {0} ---", iLoop++);
                Helper.Print2DArray(solution, 4);
                Console.WriteLine("---");
            }

            Console.ReadLine();
        }
Пример #2
0
        static void BellmanFord_ShortestPath()
        {
            int[,] graph =
            {
                {  0, 0, -1, 0, 0, 0,  0 },
                { -2, 0,  0, 0, 8, 0,  0 },
                {  0, 0,  0, 0, 0, 1,  0 },
                {  0, 9,  1, 0, 0, 0,  0 },
                {  0, 0,  0, 0, 0, 0, -3 },
                {  0, 0,  0, 6, 0, 0,  2 },
                {  0, 0,  0, 0, 0, 0,  0 },
            };

            ShortedPathAlgo algo = new ShortedPathAlgo();

            int[] shortestpath = algo.BellmanFord_ShortestPath(graph, 0, 7);

            Console.WriteLine("Dijksta's algo");
            Helper.PrintArray(shortestpath);
            Console.ReadLine();
        }
Пример #3
0
        static void GraphTheory_DijkstraAlgo()
        {
            int[,] graph =
            {
                { 0,  6, 0,  0,  0,  0, 0,  9, 0 },
                { 6,  0, 9,  0,  0,  0, 0, 11, 0 },
                { 0,  9, 0,  5,  0,  6, 0,  0, 2 },
                { 0,  0, 5,  0,  9, 16, 0,  0, 0 },
                { 0,  0, 0,  9,  0, 10, 0,  0, 0 },
                { 0,  0, 6,  0, 10,  0, 2,  0, 0 },
                { 0,  0, 0, 16,  0,  2, 0,  1, 6 },
                { 9, 11, 0,  0,  0,  0, 1,  0, 5 },
                { 0,  0, 2,  0,  0,  0, 6,  5, 0 }
            };

            ShortedPathAlgo algo = new ShortedPathAlgo();

            int[] shortestpath = algo.Dijkstra_ShortestPath(graph, 0, 9);

            Console.WriteLine("Dijksta's algo");
            Helper.PrintArray(shortestpath);
            Console.ReadLine();
        }
Пример #4
0
        private static void GraphTheory_PrimAlgo()
        {
            int[,] graph =
            {
                {  0,  4,  0, 10,  2,  0,  0,  0 },
                {  4,  0, 18,  8,  0,  0,  0,  0 },
                {  0, 18,  0, 11,  0,  0,  0, 19 },
                { 10,  8, 11,  0,  5,  0, 11,  9 },
                {  2,  0,  0,  5,  0, 51,  0,  0 },
                {  0,  0,  0,  0, 51,  0,  1,  2 },
                {  0,  0,  0, 11,  0,  1,  0, 23 },
                {  0,  0, 19,  9,  0,  2, 23,  0 }
            };

            ShortedPathAlgo algo = new ShortedPathAlgo();

            int[,] solution = algo.PrimAlgo_MinimumSpanningTree(graph, 0, 8);

            Console.WriteLine("FloydWarshall's algo");
            Helper.Print2DArray(solution, 8);

            Console.ReadLine();
        }