public int NumberOfRoutes(Node start, Node end) { start.Routes .ForEach(route => _workingRoutes.Add(new YRSRoute(route))); while (_workingRoutes.Any()) { YRSRoute route = _workingRoutes.First(); if (isValid(route)) { var actualStop = route.Paths.Last().End; if (this.ShouldAdd(route, end)) { _validRoutes.Add(route); } foreach (var path in actualStop.Routes) { var newRoute = new YRSRoute(); newRoute.Paths.AddRange(route.Paths); newRoute.Paths.Add(path); _workingRoutes.Add(newRoute); } } _workingRoutes.Remove(route); } return(_validRoutes.Count()); }
private bool isValid(YRSRoute route) { var actualStops = route.Paths.Count; var actualCost = route.Paths.Sum(path => path.Cost); return(actualStops <= this._maxStops && actualCost <= this._maxCost); }
private bool ShouldAdd(YRSRoute route, Node end) { var actualStop = route.Paths.Last().End; var _equalCondition = actualStop.Name == end.Name; var _exactlyCondition = this._exactlyStops == null || route.Paths.Count == this._exactlyStops.Value; return(_equalCondition && _exactlyCondition); }