/*DijkstraSearch searches for the fastest route through the network by arranging the nodes and links from fastest to slowest. * If DijkstaSearch finds the end, it will stop searching and the fastest route is found*/ private static void DijkstraSearch(Node start, Node end, DateTime time, DataModel dataModel) { start.minCostToStart = time; var prioQueue = new List <Node> { start }; while (prioQueue.Any()) { prioQueue = prioQueue.OrderBy(x => x.minCostToStart).ToList(); var node = prioQueue.First(); prioQueue.Remove(node); foreach (var link in node.connections.OrderBy(x => Link.GetDepartTime(DataModel.GetLink(x.start.number, x.end.number, dataModel.links), node.minCostToStart) + x.weight)) { DateTime tempCost = Link.GetDepartTime(DataModel.GetLink(link.start.number, link.end.number, dataModel.links), node.minCostToStart) + link.weight; var childNode = link.end; if (childNode.visited) { continue; } if (tempCost < childNode.minCostToStart) { childNode.minCostToStart = tempCost; childNode.nearestToStart = node; if (!prioQueue.Contains(childNode)) { prioQueue.Add(childNode); } } } node.visited = true; if (node == end) { return; } } }
//Method to ask for the 20 fastest routes public static List <Route> GetRoute(string startName, string endName, DateTime time, DataModel dataModel) { List <Route> listRoute = new List <Route>(); DateTime startTime = time; for (int i = 0; i < 20; i++) { Route fastestRoute = new Route(startName, endName, startTime, dataModel); listRoute.Add(fastestRoute); startTime = fastestRoute.startTime + new TimeSpan(1, 0, 0); foreach (Node node in dataModel.nodes) { node.minCostToStart = DateTime.MaxValue; node.nearestToStart = null; node.visited = false; } } return(listRoute); }