public void ShouldFindShortestPath()
        {
            var stationA = new TrainStation('A');
            var stationB = new TrainStation('B');
            var stationC = new TrainStation('C');

            stationA.AddConnection(new TrainConnection(3, stationA, stationB));
            stationB.AddConnection(new TrainConnection(5, stationB, stationC));
            stationA.AddConnection(new TrainConnection(7, stationA, stationC));

            var stations = new List <TrainStation> {
                stationA, stationB, stationC
            };

            var trainMap = new TrainMap(stations);

            int shortestPath = trainMap.FindShortestPathDistance('A', 'C');

            Assert.Equal(shortestPath, 7);
        }
예제 #2
0
        static TrainMap InitializeProgram()
        {
            #region InitializeProgram
            // inicializando o grafo com estações e rotas pré definidas
            var stationA = new TrainStation('A');
            var stationB = new TrainStation('B');
            var stationC = new TrainStation('C');
            var stationD = new TrainStation('D');
            var stationE = new TrainStation('E');

            var edgeAB = new TrainConnection(5, stationA, stationB);
            var edgeBC = new TrainConnection(4, stationB, stationC);
            var edgeCD = new TrainConnection(8, stationC, stationD);
            var edgeDC = new TrainConnection(8, stationD, stationC);
            var edgeDE = new TrainConnection(6, stationD, stationE);
            var edgeAD = new TrainConnection(5, stationA, stationD);
            var edgeCE = new TrainConnection(2, stationC, stationE);
            var edgeEB = new TrainConnection(3, stationE, stationB);
            var edgeAE = new TrainConnection(7, stationA, stationE);

            stationA.AddConnections(new List <TrainConnection> {
                edgeAB, edgeAD, edgeAE
            });
            stationB.AddConnection(edgeBC);
            stationC.AddConnections(new List <TrainConnection> {
                edgeCD, edgeCE
            });
            stationD.AddConnections(new List <TrainConnection> {
                edgeDC, edgeDE
            });
            stationE.AddConnection(edgeEB);

            var trainMap = new TrainMap(new List <TrainStation> {
                stationA, stationB, stationC, stationD, stationE
            });

            trainMap.AddRoute("CDC".ToCharArray());
            trainMap.AddRoute("CEBC".ToCharArray());
            trainMap.AddRoute("ABCDC".ToCharArray());
            trainMap.AddRoute("ADCDC".ToCharArray());
            trainMap.AddRoute("ADEBC".ToCharArray());
            trainMap.AddRoute("CEBCDC".ToCharArray());
            trainMap.AddRoute("CDCEBC".ToCharArray());
            trainMap.AddRoute("CDEBC".ToCharArray());
            trainMap.AddRoute("CEBCEBC".ToCharArray());
            trainMap.AddRoute("CEBCEBCEBC".ToCharArray());

            return(trainMap);

            #endregion
        }
예제 #3
0
        public void ShouldCalculateRoute()
        {
            var   stationA = new TrainStation('A');
            var   stationB = new TrainStation('B');
            Route route    = new Route();

            stationA.AddConnection(new TrainConnection(3, stationA, stationB));

            route.AddNode(stationA);
            route.AddNode(stationB);

            int result = route.CalculateRouteDistance();

            Assert.Equal(result, 3);
        }