public void AddChild(Stop stop, Tree tree) { Node node = new Node(); node.Parent = this; node.value = stop; Child.Add(node); tree.nodeList.Add(node); }
private static void bfsGraph(List<Route> routes, List<Stop> stops) { Stop initial = stops.FirstOrDefault(o => o.name == "BAKIRKÖY"); Stop goal = stops.FirstOrDefault(o => o.name == "KABATAŞ"); // prepare the resulting tree which will be used to create solutioin sequence Tree solutionTree = new Tree(); // find routes passing through a stop List<Route> passRoutes = new List<Route>(); Queue<Stop> frontier = new Queue<Stop>(); frontier.Enqueue(initial); List<Stop> explored = new List<Stop>(); while (true) { if (frontier.Count == 0) { Console.WriteLine("No Solution"); break; } Stop currentStop = frontier.Dequeue(); passRoutes.Clear(); routes.Where(o => o.stops.Contains(currentStop)).ToList().ForEach(o => passRoutes.Add(o)); explored.Add(currentStop); Node node = new Node(); if (currentStop.name == goal.name) { Console.WriteLine("Destination Reached."); node = solutionTree.nodeList.FirstOrDefault(o => o.value.name == currentStop.name); if (node == null) Console.WriteLine("Current Node not found in the solution tree node list: " + currentStop.name); List<Stop> solution = new List<Stop>(); while (node.isRoot == false) { solution.Add(node.value); node = node.Parent; } solution.Add(node.value); solution.Reverse(); Console.WriteLine("\nSolution is: \n"); Console.ReadKey(); /* foreach (Stop s in solution) { Console.Write(s.name + " "); Console.WriteLine(); } */ Stop stop1; Stop stop2; for (int i = 0; i < solution.Count-1; i++) { stop1 = solution[i]; stop2 = solution[i + 1]; Route r = findRoute(stop1, stop2, routes); Console.WriteLine(stop1.name + " - " + stop2.name + " => " + r.name); } break; } // if tree is initially empty, add the root node if (solutionTree.nodeList.Count == 0) { node.value = currentStop; node.isRoot = true; solutionTree.root = node; solutionTree.nodeList.Add(node); } else { node = solutionTree.nodeList.FirstOrDefault(o => o.value.name == currentStop.name); if (node == null) Console.WriteLine("Current Node not found in the solution tree node list: " + currentStop.name); } //Console.WriteLine("Routes passing through: " + currentStop.name); // find adjacent stops of the current stop foreach (Route r in passRoutes) // for each route containing current stop { //Console.Write(r.name + " :\t"); // find the index of the current stop in the routes containing the stop int index = r.stops.IndexOf(currentStop); if (index >= 1) // if its not the first element { // add frontier and tree the previous stop if (frontier.Contains(r.stops[index - 1]) == false && explored.Contains(r.stops[index - 1]) == false) { frontier.Enqueue(r.stops[index - 1]); node.AddChild(r.stops[index - 1], solutionTree); //Console.Write(r.stops[index - 1].name + ", "); } } if (index < r.stops.Count - 1) // and not the last element { if (frontier.Contains(r.stops[index + 1]) == false && explored.Contains(r.stops[index + 1]) == false) { // add frontier the next stop in the list frontier.Enqueue(r.stops[index + 1]); node.AddChild(r.stops[index + 1], solutionTree); //Console.Write(r.stops[index + 1].name + " "); } } //Console.WriteLine(); } //Console.ReadKey(); } }