private Connection GetConnection(Station station1, Station station2) { foreach (var connection in connections) { Station one = connection.Station1; Station two = connection.Station2; if ((station1.Equals(one) && (station2.Equals(two)))) { return(connection); } } return(null); }
public List <Connection> GetDirections(string startStationName, string endStartionName) { if (!this.HasStation(startStationName) || (!this.HasStation(endStartionName))) { throw new KeyNotFoundException("Stations enterered do not exists on this subway"); } Station start = new Station(startStationName); Station end = new Station(endStartionName); List <Connection> route = new List <Connection>(); List <Station> reachableStations = new List <Station>(); Dictionary <Station, Station> previousStation = new Dictionary <Station, Station>(); List <Station> neighbours = (List <Station>)network[start]; foreach (var station in neighbours) { if (station.Equals(end)) { route.Add(GetConnection(start, end)); return(route); } else { reachableStations.Add(station); previousStation.Add(station, start); } } List <Station> nextStations = new List <Station>(); nextStations.AddRange(neighbours); Station currentStation = start; //searchLoop for (int i = 0; i < stations.Count; i++) { List <Station> tmpNextStations = new List <Station>(); foreach (var station in nextStations) { reachableStations.Add(station); currentStation = station; List <Station> currentNeighbours = (List <Station>)network[currentStation]; foreach (var neighbor in currentNeighbours) { if (neighbor.Equals(end)) { reachableStations.Add(neighbor); previousStation.Add(neighbor, currentStation); goto loopEnd; } else if (!reachableStations.Contains(neighbor)) { reachableStations.Add(neighbor); tmpNextStations.Add(neighbor); previousStation.Add(neighbor, currentStation); } } } nextStations = tmpNextStations; } loopEnd: bool keepLooping = true; Station keyStation = end; while (keepLooping) { Station station = (Station)previousStation[keyStation]; route.Add(GetConnection(station, keyStation)); if (start.Equals(station)) { keepLooping = false; } keyStation = station; } return(route); }
public List<Connection> GetDirections(string startStationName, string endStartionName) { if (!this.HasStation(startStationName) || (!this.HasStation(endStartionName))) { throw new KeyNotFoundException("Stations enterered do not exists on this subway"); } Station start = new Station(startStationName); Station end = new Station(endStartionName); List<Connection> route = new List<Connection>(); List<Station> reachableStations = new List<Station>(); Dictionary<Station, Station> previousStation = new Dictionary<Station, Station>(); List<Station> neighbours = (List<Station>)network[start]; foreach (var station in neighbours) { if (station.Equals(end)) { route.Add(GetConnection(start, end)); return route; } else { reachableStations.Add(station); previousStation.Add(station, start); } } List<Station> nextStations = new List<Station>(); nextStations.AddRange(neighbours); Station currentStation = start; //searchLoop for (int i = 0; i < stations.Count; i++) { List<Station> tmpNextStations = new List<Station>(); foreach (var station in nextStations) { reachableStations.Add(station); currentStation = station; List<Station> currentNeighbours = (List<Station>)network[currentStation]; foreach (var neighbor in currentNeighbours) { if (neighbor.Equals(end)) { reachableStations.Add(neighbor); previousStation.Add(neighbor, currentStation); goto loopEnd; } else if (!reachableStations.Contains(neighbor)) { reachableStations.Add(neighbor); tmpNextStations.Add(neighbor); previousStation.Add(neighbor, currentStation); } } } nextStations = tmpNextStations; } loopEnd: bool keepLooping = true; Station keyStation = end; while (keepLooping) { Station station = (Station)previousStation[keyStation]; route.Add(GetConnection(station, keyStation)); if (start.Equals(station)) keepLooping = false; keyStation = station; } return route; }
private Connection GetConnection(Station station1, Station station2) { foreach (var connection in connections) { Station one = connection.Station1; Station two = connection.Station2; if((station1.Equals(one) && (station2.Equals(two)))) return connection; } return null; }