示例#1
0
        public void EdmondKarp_AdjacencyMatrixGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix.WeightedDiGraph <char, int>();

            graph.AddVertex('S');
            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('T');

            graph.AddEdge('S', 'A', 10);
            graph.AddEdge('S', 'C', 10);

            graph.AddEdge('A', 'B', 4);
            graph.AddEdge('A', 'C', 2);
            graph.AddEdge('A', 'D', 8);

            graph.AddEdge('B', 'T', 10);

            graph.AddEdge('C', 'D', 9);

            graph.AddEdge('D', 'B', 6);
            graph.AddEdge('D', 'T', 10);

            var algorithm = new EdmondKarpMaxFlow <char, int>(new EdmondKarpOperators());

            var result = algorithm.ComputeMaxFlow(graph, 'S', 'T');

            Assert.AreEqual(result, 19);
        }
        public void TravellingSalesman_AdjacencyMatrixGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix.WeightedDiGraph <int, int>();

            graph.AddVertex(0);
            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);

            graph.AddEdge(0, 1, 1);
            graph.AddEdge(0, 2, 15);
            graph.AddEdge(0, 3, 6);

            graph.AddEdge(1, 0, 2);
            graph.AddEdge(1, 2, 7);
            graph.AddEdge(1, 3, 3);

            graph.AddEdge(2, 0, 9);
            graph.AddEdge(2, 1, 6);
            graph.AddEdge(2, 3, 12);

            graph.AddEdge(3, 0, 10);
            graph.AddEdge(3, 1, 4);
            graph.AddEdge(3, 2, 8);

            var tsp = new TravellingSalesman <int, int>();

            Assert.AreEqual(21, tsp.FindMinWeight(graph, new TSPShortestPathOperators()));
        }
示例#3
0
        public void MinCut_AdjacencyMatrixGraph_Smoke_Test_2()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix.WeightedDiGraph <char, int>();

            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('E');
            graph.AddVertex('F');
            graph.AddVertex('G');
            graph.AddVertex('H');
            graph.AddVertex('I');
            graph.AddVertex('J');


            graph.AddEdge('A', 'B', 1);
            graph.AddEdge('A', 'C', 1);
            graph.AddEdge('A', 'D', 1);

            graph.AddEdge('B', 'E', 1);
            graph.AddEdge('C', 'E', 1);
            graph.AddEdge('D', 'E', 1);

            graph.AddEdge('E', 'F', 1);

            graph.AddEdge('F', 'G', 1);
            graph.AddEdge('F', 'H', 1);
            graph.AddEdge('F', 'I', 1);

            graph.AddEdge('G', 'J', 1);
            graph.AddEdge('H', 'J', 1);
            graph.AddEdge('I', 'J', 1);


            var algorithm = new MinCut <char, int>(new EdmondKarpOperators());

            var result = algorithm.ComputeMinCut(graph, 'A', 'J');

            Assert.AreEqual(result.Count, 1);
        }
        public void Johnsons_AdjacencyMatrixGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix.WeightedDiGraph <char, int>();

            graph.AddVertex('S');
            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('T');

            graph.AddEdge('S', 'A', -5);
            graph.AddEdge('S', 'C', 10);

            graph.AddEdge('A', 'B', 10);
            graph.AddEdge('A', 'C', 1);
            graph.AddEdge('A', 'D', 8);

            graph.AddEdge('B', 'T', 4);

            graph.AddEdge('C', 'D', 1);

            graph.AddEdge('D', 'B', 1);
            graph.AddEdge('D', 'T', 10);

            var algorithm = new JohnsonsShortestPath <char, int>(new JohnsonsShortestPathOperators());

            var result = algorithm.FindAllPairShortestPaths(graph);

            var testCase = result.First(x => x.Source == 'S' && x.Destination == 'T');

            Assert.AreEqual(2, testCase.Distance);

            var expectedPath = new char[] { 'S', 'A', 'C', 'D', 'B', 'T' };

            for (int i = 0; i < expectedPath.Length; i++)
            {
                Assert.AreEqual(expectedPath[i], testCase.Path[i]);
            }
        }
示例#5
0
        public void AStar_AdjacencyMatrixGraph_Smoke_Test()
        {
            var testLocations = @"A5 30 573
                            A4 30 483
                            A2 30 178
                            A1 30 48
                            B1 207 48
                            B2 207 161
                            B3 144 339
                            B4 129 443
                            B5 127 479
                            C2 258 162
                            C3 240 288
                            C4 225 443
                            C5 336 573
                            D1 438 48
                            D2 438 174
                            D3 438 335
                            D4 438 473
                            D5 438 573
                            E4 575 475
                            E5 684 493
                            F1 611 48
                            F2 600 173
                            F5 701 573
                            G1 797 48
                            G2 797 150
                            G2b 770 151
                            G4 797 382
                            G4b 770 382
                            G5 797 573";

            var locationMappings = new Dictionary <string, Location>();

            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix.WeightedDiGraph <Location, double>();

            using (var reader = new StringReader(testLocations))
            {
                string line;
                while ((line = reader.ReadLine()?.Trim()) != null)
                {
                    var @params = line.Split(' ');

                    var location = new Location()
                    {
                        Point = new Point(double.Parse(@params[1]), double.Parse(@params[2])),
                        Name  = @params[0]
                    };

                    locationMappings.Add(@params[0], location);
                    graph.AddVertex(location);
                }
            }

            var testConnections = @"A1 2 B1 A2
                                A2 3 A1 B2 A4
                                A4 3 A2 B5 A5
                                A5 2 A4 C5
                                B1 3 A1 D1 B2
                                B2 4 C2 C3 A2 B1
                                B3 1 B4
                                B4 3 B3 B5 C4
                                B5 3 A4 C5 B4
                                C2 3 D2 B2 C3
                                C3 3 C2 B2 C4
                                C4 3 B4 C3 D4
                                C5 3 D5 B5 A5
                                D1 3 B1 F1 D2
                                D2 3 C2 D1 F2
                                D3 1 D4
                                D4 4 D3 C4 E4 D5
                                D5 3 C5 D4 F5
                                E4 3 D4 E5 F2
                                F1 3 F2 D1 G1
                                F2 4 F1 D2 G2b E4
                                F5 3 E5 G5 D5
                                G1 2 G2 F1
                                G2 3 G1 G2b G4
                                G2b 3 G2 F2 G4b
                                G4 3 G4b G2 G5
                                G4b 3 G4 G2b E5
                                G5 2 F5 G4
                                E5 3 F5 E4 G4b";

            using (var reader = new StringReader(testConnections))
            {
                string line;
                while ((line = reader.ReadLine()?.Trim()) != null)
                {
                    var @params = line.Split(' ');

                    for (int i = 2; i < int.Parse(@params[1]) + 2; i++)
                    {
                        graph.AddEdge(locationMappings[@params[0]], locationMappings[@params[i]],
                                      EucledianDistanceCalculator.GetEucledianDistance(locationMappings[@params[0]].Point,
                                                                                       locationMappings[@params[i]].Point));
                    }
                }
            }

            var algorithm = new AStarShortestPath <Location, double>(new AStarShortestPathOperators(), new AStarSearchHeuristic());

            var result = algorithm.FindShortestPath(graph, locationMappings["A1"], locationMappings["G5"]);

            Assert.AreEqual(10, result.Path.Count);
            Assert.AreEqual(1217.3209396395309, result.Length);

            var expectedPath = new string[] { "A1", "B1", "B2", "C3", "C4", "D4", "E4", "E5", "F5", "G5" };

            for (int i = 0; i < expectedPath.Length; i++)
            {
                Assert.AreEqual(expectedPath[i], result.Path[i].Name);
            }
        }