Fhnw.Ecnf.RoutePlanner.RoutePlannerLib.RoutesFloydWarshall.FindShortestRouteBetween C# (CSharp) Метод

FindShortestRouteBetween() публичный Метод

public FindShortestRouteBetween ( string fromCity, string toCity, TransportModes mode, IProgress reportProgress = null ) : List
fromCity string
toCity string
mode TransportModes
reportProgress IProgress
Результат List
        public override List<Link> FindShortestRouteBetween(string fromCity, string toCity,
                                        TransportModes mode, IProgress<string> reportProgress = null)
        {
            List<City> cities = FindCitiesBetween(fromCity, toCity);
            if (cities == null || cities.Count < 1)
            {
                return null;
            }
            List<Link> links = FindAllLinks(cities, mode);
            if (links == null || links.Count < 1)
                return null;

            Setup(cities, links);

            City source = FindCity( fromCity, cities );
            City target = FindCity(toCity, cities);
            if (D[source.Index, target.Index] == Double.MaxValue)
            {
                return new List<Link>(); // no path between source and target
            }
            List<City> path = GetIntermediatePath(source, target);

            // must construct route from path
            List<Link> route = new List<Link>();
            route.Add( new Link(source, path.ElementAt(0), D[source.Index, path.ElementAt(0).Index] ) );
            for (int i = 0; i < path.Count - 1; i++)
            {
                City from = path.ElementAt(i);
                City to = path.ElementAt(i + 1);
                route.Add( new Link(from, to, D[from.Index, to.Index]) );
            }
            route.Add( new Link(path.ElementAt(path.Count - 1), target,
                                D[path.ElementAt(path.Count - 1).Index, target.Index]) );
            return route;
        }

Usage Example

Пример #1
0
        public void TestTask1_FindRoutes()
        {
            Cities cities = new Cities();
            cities.ReadCities(@"citiesTestDataLab4.txt");
            List<Link> expectedLinks = new List<Link>();
            expectedLinks.Add(new Link(new City("Zürich", "Switzerland", 7000, 1, 2),
                                       new City("Aarau", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Aarau", "Switzerland", 7000, 1, 2),
                                       new City("Liestal", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Liestal", "Switzerland", 7000, 1, 2),
                                       new City("Basel", "Switzerland", 7000, 1, 2), 0));

            Routes routes = new RoutesFloydWarshall(cities);
            int count = routes.ReadRoutes(@"linksTestDataLab4.txt");

            Assert.AreEqual(11, cities.Count);

            // test available cities
            routes.ExecuteParallel = true;
            List<Link> links = routes.FindShortestRouteBetween("Zürich", "Basel", TransportModes.Rail);

            Assert.IsNotNull(links);
            Assert.AreEqual(expectedLinks.Count, links.Count);

            for (int i = 0; i < links.Count; i++)
            {
                Assert.AreEqual(expectedLinks[i].FromCity.Name, links[i].FromCity.Name);
                Assert.AreEqual(expectedLinks[i].ToCity.Name, links[i].ToCity.Name);
            }


            links = routes.FindShortestRouteBetween("doesNotExist", "either", TransportModes.Rail);
            Assert.IsNull(links);

        }
All Usage Examples Of Fhnw.Ecnf.RoutePlanner.RoutePlannerLib.RoutesFloydWarshall::FindShortestRouteBetween