Esempio n. 1
0
        /// <summary>
        /// Recursively goes throuhg all the paths untill it meets the StopTravellingRule passed to this class
        /// </summary>
        private void FindRecursive(Town current, Town destination, TravelCard ticket)
        {
            //Update distance from source if it is less than last distance calculated
            if (ticket.StopsTravelled != 0)
            {
                DistanceFromSource[current] = Math.Min(DistanceFromSource[current], ticket.MyDistanceFromSource);
            }

            if ((ticket.StopsTravelled != 0 && current.Name == destination.Name) || // We reached the destination so no need to go further
                ticket.MyDistanceFromSource > DistanceFromSource[current]           // We already came here with better cost so no need to go further
                )
            {
                if (current.Name == destination.Name && DistanceFromSource[current] == ticket.MyDistanceFromSource)
                {
                    ShortestPathDetails = ticket;// Store ticket for debugging purposes only
                }
                return;
            }

            // Now visit all the routes going out from this town
            foreach (var route in current.Routes)
            {
                FindRecursive(route.Destination, destination, new TravelCard
                {
                    RouteCovered         = $"{ticket.RouteCovered}{route.Destination.Name}",
                    StopsTravelled       = ticket.StopsTravelled + 1,
                    MyDistanceFromSource = ticket.MyDistanceFromSource + route.Distance,
                    Map = Map
                });
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Recursively goes throuhg all the paths untill it meets the StopTravellingRule passed to this class
        /// </summary>
        private void FindRecursive(Town current, Town destination, TravelCard ticket)
        {
            RecursionCount++;
            // If we get tired of travelling, stop travelling more, you might going round in circles
            if (StopTravelingRule.IsMatch(ticket))
            {
                return;
            }

            // Path found, add it to list and carry on traversing as there might be more ways to come back here
            if (ticket.StopsTravelled != 0 && current.Name == destination.Name && (DestinationFoundRule?.IsMatch(ticket) ?? true))
            {
                Routes.Add(ticket);
            }

            // Now visit all the routes going out from this town
            foreach (var route in current.Routes)
            {
                FindRecursive(route.Destination, destination, new TravelCard
                {
                    RouteCovered         = $"{ticket.RouteCovered}{route.Destination.Name}",
                    StopsTravelled       = ticket.StopsTravelled + 1,
                    MyDistanceFromSource = ticket.MyDistanceFromSource + route.Distance,
                    Map = Map
                });
            }
        }
Esempio n. 3
0
        public int FindShortestPathBetween(Town origin, Town destination)
        {
            InitializeDistanceFromSourceList();

            var ticket = new TravelCard
            {
                RouteCovered = origin.Name,
                Map          = Map
            };

            FindRecursive(origin, destination, ticket);

            return(DistanceFromSource[destination]);
        }
        public int FindShortestPathBetween(Town origin, Town destination)
        {
            InitializeDistanceFromSourceList();

            var ticket = new TravelCard
            {
                RouteCovered = origin.Name,
                Map          = Map
            };

            FindIterative(origin, destination);


            ShortestPathDetails = DistanceFromSource[destination];

            return(DistanceFromSource[destination].MyDistanceFromSource);
        }
Esempio n. 5
0
        /// <summary>
        /// Finds all the paths between origin and destinations. Keeps searching untill stopTravellingRule is met
        /// </summary>
        /// <returns>All possible routes between origin and destination</returns>
        public List <TravelCard> FindAllRoutesBetween(Town origin, Town destination, ITravelRule stopTravelingRule, ITravelRule destinationFoundRule = null)
        {
            Routes         = new List <TravelCard>();
            RecursionCount = 0;

            StopTravelingRule    = stopTravelingRule;
            DestinationFoundRule = destinationFoundRule;

            var ticket = new TravelCard
            {
                RouteCovered = origin.Name,
                Map          = Map
            };

            FindRecursive(origin, destination, ticket);

            return(Routes);
        }
Esempio n. 6
0
        /// <summary>
        /// Recursively goes throuhg all the paths untill it meets the StopTravellingRule passed to this class
        /// </summary>
        private void Find(Town origin, Town destination)
        {
            Stack.Push(new TravelCard
            {
                RouteCovered         = $"{origin.Name}",
                StopsTravelled       = 0,
                MyDistanceFromSource = 0,
                LastTownVisited      = origin,
                Map = Map
            });

            while (Stack.Count > 0)
            {
                var current = Stack.Pop();

                foreach (var route in current.LastTownVisited.Routes)
                {
                    RecursionCount++;

                    var ticket = new TravelCard
                    {
                        RouteCovered         = $"{current.RouteCovered}{route.Destination.Name}",
                        StopsTravelled       = current.StopsTravelled + 1,
                        MyDistanceFromSource = current.MyDistanceFromSource + route.Distance,
                        LastTownVisited      = route.Destination,
                        Map = Map
                    };

                    // If we get tired of travelling, stop travelling more, you might going round in circles
                    if (StopTravelingRule.IsMatch(ticket))
                    {
                        continue;
                    }

                    // Path found, add it to list and carry on traversing as there might be more ways to come back here
                    if (ticket.StopsTravelled != 0 && route.Destination.Name == destination.Name && (DestinationFoundRule?.IsMatch(ticket) ?? true))
                    {
                        Routes.Add(ticket);
                    }

                    Stack.Push(ticket);
                }
            }
        }