public MStation buildStation(Station s) { MStation station = new MStation() { Id = s.Id, name = s.name, address = s.address, country = s.country, state = (State)Enum.Parse(typeof(State), s.state), }; return station; }
public void addGetDeleteStation() { int id1 = dbStation.addNewRecord("BoholmStation", "Boholm", "Denmark", "Open"); int id2 = dbStation.addNewRecord("AalborgStation", "Aalborg", "Denmark", "Open"); int id3 = dbBT.addNewRecord("SmallBattery", "TODB", 8, 30); dbConnection.addNewRecord(id1, id2, 300, 7); int id4 = dbBS.addNewRecord(id3, id1, 1); try { MStation station = dbStation.getRecord(id1, true); Dictionary<MStation, decimal> adj = station.naboStations; ICollection<MStation> naborStations = (ICollection<MStation>)(adj.Keys); IEnumerator ei = naborStations.GetEnumerator(); MStation naborStation = new MStation(); if (ei.MoveNext()) { naborStation = (MStation)ei.Current; } Assert.AreEqual("BoholmStation", station.name); Assert.AreEqual("Boholm", station.address); Assert.AreEqual("Denmark", station.country); Assert.AreEqual("Open", station.state.ToString()); Assert.AreEqual("AalborgStation", naborStation.name); Assert.AreEqual("Aalborg", naborStation.address); Assert.AreEqual("Denmark", naborStation.country); Assert.AreEqual("Open", naborStation.state.ToString()); Assert.AreEqual(id3, station.storages[0].type.id); Assert.AreEqual("SmallBattery", station.storages[0].type.name); Assert.AreEqual("TODB", station.storages[0].type.producer); Assert.AreEqual(8, Convert.ToInt32(station.storages[0].type.capacity)); Assert.AreEqual(30, Convert.ToInt32(station.storages[0].type.exchangeCost)); } catch { } finally { dbBS.deleteRecord(id4); dbBT.deleteRecord(id3); dbConnection.deleteRecord(id1, id2); dbStation.deleteRecord(id2); dbStation.deleteRecord(id1); } }
public static int breathFirstSearch(Dictionary<MStation, LinkedList<MStation>> adjList, MStation start, MStation destination) { int distance = 0; HashSet<MStation> reachedStations = new HashSet<MStation>(); Queue<MStation> queue = new Queue<MStation>(); Dictionary<MStation, int> station_distance = new Dictionary<MStation, int>(); Dictionary<MStation, MStation> station_parent = new Dictionary<MStation, MStation>(); if (start == null) { throw new SystemException("The start station does not exist."); } reachedStations.Add(start); station_distance.Add(start, 0); station_parent.Add(start, null); queue.Enqueue(start); while (queue.Count != 0) { MStation u = queue.Dequeue(); foreach (MStation station in adjList[u]) { if (!reachedStations.Contains(station)) { reachedStations.Add(station); station_distance.Add(station, station_distance[u] + 1); station_parent.Add(station, u); queue.Enqueue(station); } } } if (station_distance.ContainsKey(destination)) { distance = station_distance[destination]; } return distance; //does not include start station }
public LinkedList<MStation> getNaborStationsWithoutDriveHour(int id) { LinkedList<MStation> nStations = new LinkedList<MStation>(); nStations.AddFirst(getRecord(id, false)); using (ElectricCarEntities context = new ElectricCarEntities()) { var connections = from c in context.Connections where c.sId1 == id || c.sId2 == id select c; foreach (var c in connections) { MStation sToAdd = new MStation(); if (c.sId1 == id) { sToAdd = getRecord(c.sId2, false); } else { sToAdd = getRecord(c.sId1, false); } nStations.AddLast(sToAdd); } } return nStations; }
public Dictionary<MStation, decimal> getNaborStationsWithDriveHour(int id) { Dictionary<MStation, decimal> nStations = new Dictionary<MStation, decimal>(); nStations.Add(getRecord(id, false), 0); using (ElectricCarEntities context = new ElectricCarEntities()) { var connections = from c in context.Connections where c.sId1 == id || c.sId2 == id select c; foreach (var c in connections) { MStation sToAdd = new MStation(); decimal driveHour = c.driveHour.Value; if (c.sId1 == id) { sToAdd = getRecord(c.sId2, false); } else { sToAdd = getRecord(c.sId1, false); } nStations.Add(sToAdd, driveHour); } } return nStations; }
public MBookingLine() { BatteryType = new MBatteryType(); Station = new MStation(); }
public void getNaborStations() { int id1 = dbStation.addNewRecord("BoholmStation", "Boholm", "Denmark", "Open"); int id2 = dbStation.addNewRecord("nabor1", "Aarhus", "Denmark", "Close"); int id3 = dbStation.addNewRecord("nabor2", "Aalborg", "Denmark", "Open"); dbConnection.addNewRecord(id1, id2, 200, 2); dbConnection.addNewRecord(id1, id3, 300, 3); try { LinkedList<MStation> stations = dbStation.getNaborStationsWithoutDriveHour(id1); Assert.AreEqual(3, stations.Count); MStation startStation = new MStation(); MStation naborStationId2 = new MStation(); MStation naborStationId3 = new MStation(); foreach (MStation c in stations) { if (c.Id == id1) { startStation = c; } else if (c.Id == id2) { naborStationId2 = c; } else if (true) { naborStationId3 = c; } } Assert.AreEqual(id1, startStation.Id); Assert.AreEqual(id2, naborStationId2.Id); Assert.AreEqual("nabor1", naborStationId2.name); Assert.AreEqual("Aarhus", naborStationId2.address); Assert.AreEqual("Denmark", naborStationId2.country); Assert.AreEqual("Close", naborStationId2.state.ToString()); Assert.AreEqual(id3, naborStationId3.Id); Assert.AreEqual("nabor2", naborStationId2.name); Assert.AreEqual("Aalborg", naborStationId2.address); Assert.AreEqual("Denmark", naborStationId2.country); Assert.AreEqual("Open", naborStationId2.state.ToString()); } catch { } finally { dbConnection.deleteRecord(id1, id2); dbConnection.deleteRecord(id1, id3); dbStation.deleteRecord(id1); dbStation.deleteRecord(id2); dbStation.deleteRecord(id3); } }
public static List<MStation> leastStopsPath(Dictionary<MStation, LinkedList<MStation>> adjList, MStation start, MStation destination, out int numOfStops) { //keep track of visited nodes HashSet<MStation> reachedStations = new HashSet<MStation>(); Queue<MStation> queue = new Queue<MStation>(); Dictionary<MStation, int> station_distance = new Dictionary<MStation, int>(); Dictionary<MStation, MStation> station_parent = new Dictionary<MStation, MStation>(); if (start == null) { throw new SystemException("The start station does not exist."); } reachedStations.Add(start); station_distance.Add(start, 0); station_parent.Add(start, null); queue.Enqueue(start); while (queue.Count != 0) { MStation u = queue.Dequeue(); if (u.Id == destination.Id) { break; } else { foreach (MStation station in adjList[u]) { if (!reachedStations.Contains(station)) { reachedStations.Add(station); station_distance.Add(station, station_distance[u] + 1); station_parent.Add(station, u); queue.Enqueue(station); } } } } List<MStation> leastStopsPath = new List<MStation>(); numOfStops = 0; //return the least stops path if (reachedStations.Contains(destination)) { MStation parent = destination; while (parent != null)//could be a bug { leastStopsPath.Add(parent); parent = station_parent[parent]; } leastStopsPath.Reverse(); numOfStops = leastStopsPath.Count; //including start station and end station } return leastStopsPath; }
public void breathFirstSearchLinearDataStructure() { Dictionary<MStation, LinkedList<MStation>> adjList = new Dictionary<MStation, LinkedList<MStation>>(); MStation s = new MStation() { Id = 1 }; MStation r = new MStation() { Id = 2 }; MStation v = new MStation() { Id = 3 }; MStation w = new MStation() { Id = 4 }; MStation t = new MStation() { Id = 5 }; MStation x = new MStation() { Id = 6 }; MStation y = new MStation() { Id = 7 }; MStation u = new MStation() { Id = 8 }; LinkedList<MStation> adjS = new LinkedList<MStation>(); LinkedList<MStation> adjR = new LinkedList<MStation>(); LinkedList<MStation> adjV = new LinkedList<MStation>(); LinkedList<MStation> adjW = new LinkedList<MStation>(); LinkedList<MStation> adjT = new LinkedList<MStation>(); LinkedList<MStation> adjX = new LinkedList<MStation>(); LinkedList<MStation> adjY = new LinkedList<MStation>(); LinkedList<MStation> adjU = new LinkedList<MStation>(); adjList.Add(s, adjS); adjList.Add(r, adjR); adjList.Add(y, adjY); adjList.Add(t, adjT); adjList.Add(x, adjX); adjList.Add(v, adjV); adjList.Add(u, adjU); adjList.Add(w, adjW); adjS.AddFirst(r); adjR.AddFirst(v); adjV.AddFirst(w); adjW.AddFirst(t); adjT.AddFirst(x); adjX.AddFirst(y); adjY.AddFirst(u); Assert.AreEqual(1, FindPath.breathFirstSearch(adjList, s, r)); Assert.AreEqual(2, FindPath.breathFirstSearch(adjList, s, v)); Assert.AreEqual(3, FindPath.breathFirstSearch(adjList, s, w)); Assert.AreEqual(4, FindPath.breathFirstSearch(adjList, s, t)); Assert.AreEqual(5, FindPath.breathFirstSearch(adjList, s, x)); Assert.AreEqual(6, FindPath.breathFirstSearch(adjList, s, y)); Assert.AreEqual(7, FindPath.breathFirstSearch(adjList, s, u)); Assert.AreEqual(3, FindPath.breathFirstSearch(adjList, w, y)); }
public void leastStopsPathTreeDataStructure() { Dictionary<MStation, LinkedList<MStation>> adjList = new Dictionary<MStation, LinkedList<MStation>>(); MStation s = new MStation() { Id = 1 }; MStation r = new MStation() { Id = 2 }; MStation v = new MStation() { Id = 3 }; MStation w = new MStation() { Id = 4 }; MStation t = new MStation() { Id = 5 }; MStation x = new MStation() { Id = 6 }; MStation y = new MStation() { Id = 7 }; MStation u = new MStation() { Id = 8 }; LinkedList<MStation> adjS = new LinkedList<MStation>(); LinkedList<MStation> adjR = new LinkedList<MStation>(); LinkedList<MStation> adjV = new LinkedList<MStation>(); LinkedList<MStation> adjW = new LinkedList<MStation>(); LinkedList<MStation> adjT = new LinkedList<MStation>(); LinkedList<MStation> adjX = new LinkedList<MStation>(); LinkedList<MStation> adjY = new LinkedList<MStation>(); LinkedList<MStation> adjU = new LinkedList<MStation>(); adjList.Add(s, adjS); adjList.Add(r, adjR); adjList.Add(y, adjY); adjList.Add(t, adjT); adjList.Add(x, adjX); adjList.Add(v, adjV); adjList.Add(u, adjU); adjList.Add(w, adjW); adjS.AddFirst(r); adjS.AddLast(v); adjR.AddFirst(w); adjR.AddLast(t); adjV.AddFirst(x); adjV.AddLast(y); adjT.AddFirst(u); adjY.AddFirst(v); adjV.AddLast(s); int stops1 = 0; int stops2 = 0; int stops3 = 0; int stops4 = 0; int stops5 = 0; int stops6 = 0; List<MStation> path1 = FindPath.leastStopsPath(adjList, s, r, out stops1); List<MStation> path2 = FindPath.leastStopsPath(adjList, s, w, out stops2); List<MStation> path3 = FindPath.leastStopsPath(adjList, s, u, out stops3); List<MStation> path4 = FindPath.leastStopsPath(adjList, s, x, out stops4); List<MStation> path5 = FindPath.leastStopsPath(adjList, x, y, out stops5); List<MStation> path6 = FindPath.leastStopsPath(adjList, y, s, out stops6); Assert.AreEqual(stops1 - 1, FindPath.breathFirstSearch(adjList, s, r)); Assert.AreEqual(1, path1[0].Id); Assert.AreEqual(2, path1[1].Id); Assert.AreEqual(stops2 - 1, FindPath.breathFirstSearch(adjList, s, w)); Assert.AreEqual(1, path2[0].Id); Assert.AreEqual(2, path2[1].Id); Assert.AreEqual(4, path2[2].Id); Assert.AreEqual(stops3 - 1, FindPath.breathFirstSearch(adjList, s, u)); Assert.AreEqual(1, path3[0].Id); Assert.AreEqual(2, path3[1].Id); Assert.AreEqual(5, path3[2].Id); Assert.AreEqual(8, path3[3].Id); Assert.AreEqual(stops4 - 1, FindPath.breathFirstSearch(adjList, s, x)); Assert.AreEqual(1, path4[0].Id); Assert.AreEqual(3, path4[1].Id); Assert.AreEqual(6, path4[2].Id); Assert.AreEqual(stops5, FindPath.breathFirstSearch(adjList, x, y)); Assert.AreEqual(0, path5.Count); Assert.AreEqual(stops6 - 1, FindPath.breathFirstSearch(adjList, y, s)); Assert.AreEqual(7, path6[0].Id); Assert.AreEqual(3, path6[1].Id); Assert.AreEqual(1, path6[2].Id); }
public void leastStopsPathLinearDataStructure() { Dictionary<MStation, LinkedList<MStation>> adjList = new Dictionary<MStation, LinkedList<MStation>>(); MStation s = new MStation() { Id = 1 }; MStation r = new MStation() { Id = 2 }; MStation v = new MStation() { Id = 3 }; MStation w = new MStation() { Id = 4 }; MStation t = new MStation() { Id = 5 }; MStation x = new MStation() { Id = 6 }; MStation y = new MStation() { Id = 7 }; MStation u = new MStation() { Id = 8 }; LinkedList<MStation> adjS = new LinkedList<MStation>(); LinkedList<MStation> adjR = new LinkedList<MStation>(); LinkedList<MStation> adjV = new LinkedList<MStation>(); LinkedList<MStation> adjW = new LinkedList<MStation>(); LinkedList<MStation> adjT = new LinkedList<MStation>(); LinkedList<MStation> adjX = new LinkedList<MStation>(); LinkedList<MStation> adjY = new LinkedList<MStation>(); LinkedList<MStation> adjU = new LinkedList<MStation>(); adjList.Add(s, adjS); adjList.Add(r, adjR); adjList.Add(y, adjY); adjList.Add(t, adjT); adjList.Add(x, adjX); adjList.Add(v, adjV); adjList.Add(u, adjU); adjList.Add(w, adjW); adjS.AddFirst(r); adjR.AddFirst(v); adjV.AddFirst(w); adjW.AddFirst(t); adjT.AddFirst(x); adjX.AddFirst(y); adjY.AddFirst(u); List<MStation> path = new List<MStation>(); int stops1 = 0; int stops2 = 0; int stops3 = 0; int stops4 = 0; int stops5 = 0; int stops6 = 0; int stops7 = 0; int stops8 = 0; List<MStation> path1 = FindPath.leastStopsPath(adjList, s, r, out stops1); List<MStation> path2 = FindPath.leastStopsPath(adjList, s, v, out stops2); List<MStation> path3 = FindPath.leastStopsPath(adjList, s, w, out stops3); List<MStation> path4 = FindPath.leastStopsPath(adjList, s, t, out stops4); List<MStation> path5 = FindPath.leastStopsPath(adjList, s, x, out stops5); List<MStation> path6 = FindPath.leastStopsPath(adjList, s, y, out stops6); List<MStation> path7 = FindPath.leastStopsPath(adjList, s, u, out stops7); List<MStation> path8 = FindPath.leastStopsPath(adjList, w, y, out stops8); Assert.AreEqual(FindPath.breathFirstSearch(adjList, s, r), stops1 - 1); Assert.AreEqual(FindPath.breathFirstSearch(adjList, s, v), stops2 - 1); Assert.AreEqual(FindPath.breathFirstSearch(adjList, s, w), stops3 - 1); Assert.AreEqual(FindPath.breathFirstSearch(adjList, s, t), stops4 - 1); Assert.AreEqual(FindPath.breathFirstSearch(adjList, s, x), stops5 - 1); Assert.AreEqual(FindPath.breathFirstSearch(adjList, s, y), stops6 - 1); Assert.AreEqual(FindPath.breathFirstSearch(adjList, s, u), stops7 - 1); Assert.AreEqual(FindPath.breathFirstSearch(adjList, w, y), stops8 - 1); Assert.AreEqual(1, path1[0].Id); Assert.AreEqual(2, path1[1].Id); Assert.AreEqual(1, path2[0].Id); Assert.AreEqual(2, path2[1].Id); Assert.AreEqual(3, path2[2].Id); Assert.AreEqual(1, path3[0].Id); Assert.AreEqual(2, path3[1].Id); Assert.AreEqual(3, path3[2].Id); Assert.AreEqual(4, path3[3].Id); Assert.AreEqual(1, path4[0].Id); Assert.AreEqual(2, path4[1].Id); Assert.AreEqual(3, path4[2].Id); Assert.AreEqual(4, path4[3].Id); Assert.AreEqual(5, path4[4].Id); Assert.AreEqual(1, path5[0].Id); Assert.AreEqual(2, path5[1].Id); Assert.AreEqual(3, path5[2].Id); Assert.AreEqual(4, path5[3].Id); Assert.AreEqual(5, path5[4].Id); Assert.AreEqual(6, path5[5].Id); Assert.AreEqual(1, path6[0].Id); Assert.AreEqual(2, path6[1].Id); Assert.AreEqual(3, path6[2].Id); Assert.AreEqual(4, path6[3].Id); Assert.AreEqual(5, path6[4].Id); Assert.AreEqual(6, path6[5].Id); Assert.AreEqual(7, path6[6].Id); Assert.AreEqual(1, path7[0].Id); Assert.AreEqual(2, path7[1].Id); Assert.AreEqual(3, path7[2].Id); Assert.AreEqual(4, path7[3].Id); Assert.AreEqual(5, path7[4].Id); Assert.AreEqual(6, path7[5].Id); Assert.AreEqual(7, path7[6].Id); Assert.AreEqual(8, path7[7].Id); Assert.AreEqual(4, path8[0].Id); Assert.AreEqual(5, path8[1].Id); Assert.AreEqual(6, path8[2].Id); Assert.AreEqual(7, path8[3].Id); }