Exemple #1
0
        public static int CountMaxDistanceRoutes(List <StartingTown> townsFrom, char townFromName, char townToName, int maxDistance)
        {
            routesResult = new Dictionary <string, int>();

            StartingTown townFrom    = townsFrom.Find(n => n.Name == townFromName);
            int          countRoutes = 0;

            if (townFrom != null)
            {
                FindMaxDistanceRoutes(townFrom, null, maxDistance);
                if (routesResult != null && routesResult.Count > 0)
                {
                    foreach (var routeFound in routesResult.Keys)
                    {
                        //Console.WriteLine(string.Format("{0,-10}{1}", routeFound, routesResult[routeFound]));
                        if (routeFound[routeFound.Length - 1] == townToName)
                        {
                            countRoutes++;
                        }
                    }
                }
            }

            return(countRoutes);
        }
Exemple #2
0
        public static int GetShortestRouteDistance(List <StartingTown> townsFrom, char townFromName, char townToName)
        {
            routesResult = new Dictionary <string, int>();

            StartingTown townFrom         = townsFrom.Find(n => n.Name == townFromName);
            int          shortestDistance = 0;

            if (townFrom != null)
            {
                FindShortestRoutes(townFrom, null, townToName, null, null);

                if (routesResult != null && routesResult.Count > 0)
                {
                    foreach (var routeFound in routesResult.Keys)
                    {
                        //Console.WriteLine(routeFound);
                        if (routeFound[routeFound.Length - 1] == townToName)
                        {
                            if (routesResult[routeFound] < shortestDistance || shortestDistance == 0)
                            {
                                shortestDistance = routesResult[routeFound];
                            }
                        }
                    }
                }
            }

            return(shortestDistance);
        }
Exemple #3
0
        private static void FindShortestRoutes(StartingTown nextTownFrom, string fullRoute, char townToName, string previousRoute, string previousPreviousRoute)
        {
            foreach (EndingTown townTo in nextTownFrom.Destinations)
            {
                var nextRoute = $"{nextTownFrom.Name}{townTo.Name}";

                var nextFullRoute = string.Empty;

                if (fullRoute == null)
                {
                    nextFullRoute = nextRoute;
                }
                else
                {
                    nextFullRoute = $"{fullRoute}{townTo.Name}";
                }

                if (!string.IsNullOrEmpty(fullRoute))
                {
                    if (routesResult.ContainsKey(fullRoute))
                    {
                        var previousRouteDistance = routesResult[fullRoute];
                        var nextRouteDistance     = previousRouteDistance + townTo.Edge;

                        if (routesResult.ContainsKey(nextFullRoute) || previousPreviousRoute == nextRoute)
                        {
                            return;
                        }

                        routesResult.Add(nextFullRoute, nextRouteDistance);

                        if (townTo.Name == townToName)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    routesResult.Add(nextRoute, townTo.Edge);
                }

                if (townTo.Intersection == null)
                {
                    return;
                }

                FindShortestRoutes(townTo.Intersection, nextFullRoute, townToName, nextRoute, previousRoute);
            }
        }
Exemple #4
0
        public List <StartingTown> Build()
        {
            var startingTowns             = new List <StartingTown>();
            List <EndingTown> endingTowns = new List <EndingTown>();
            var graphElements             = routesFile.GetGraphElements();

            foreach (var nodeEdge in graphElements)
            {
                if (nodeEdge[0] == nodeEdge[1])
                {
                    continue;
                }

                var startingTown = startingTowns.Find(n => n.Name == nodeEdge[0]);
                if (startingTown == null)
                {
                    startingTown = new StartingTown(nodeEdge[0]);
                    startingTowns.Add(startingTown);
                }

                if (startingTown.Destinations.Find(n => n.Name == nodeEdge[1]) == null)
                {
                    var endingTown = new EndingTown(nodeEdge[1], int.Parse(nodeEdge.Substring(2)));
                    startingTown.AddDestination(endingTown);

                    endingTowns.Add(endingTown);
                }
            }

            //Add intersections
            foreach (var town in startingTowns)
            {
                var intersections = endingTowns.FindAll(n => n.Name == town.Name);
                foreach (var endingTown in intersections)
                {
                    endingTown.AddIntersection(town);
                }
            }

            return(startingTowns);
        }
Exemple #5
0
        public static int CountMaxStopRoutes(List <StartingTown> townsFrom, char townFromName, char townToName, bool exactMaxStop, int maxStop)
        {
            routesResult = new Dictionary <string, int>();
            var numberOfRoutes = 0;

            StartingTown townFrom = townsFrom.Find(n => n.Name == townFromName);

            if (townFrom != null)
            {
                FindMaxLengthRoutes(townFrom, null, maxStop + 1);

                if (routesResult != null && routesResult.Count > 0)
                {
                    foreach (var route in routesResult.Keys)
                    {
                        if (exactMaxStop)
                        {
                            if (route.Length == maxStop + 1 && route[route.Length - 1] == townToName)
                            {
                                numberOfRoutes++;
                            }
                        }
                        else
                        {
                            if (route.Length <= maxStop + 1 && route[route.Length - 1] == townToName)
                            {
                                numberOfRoutes++;
                            }
                        }

                        //Console.WriteLine(string.Format("{0,-10}{1}", route, routesResult[route]));
                    }
                }
            }

            return(numberOfRoutes);
        }
Exemple #6
0
 private static EndingTown FindTownTo(char townToName, StartingTown townFrom)
 {
     return(townFrom.Destinations.Find(n => n.Name == townToName));
 }