//DFS Implementation static List <City> TopSort(RailwayNetwork railway, City startCity) { var sortedCities = new Queue <City>(); return(sortedCities.ToList()); }
static List <City> DFSTraversal(RailwayNetwork railway, City startCity, LinkedList <City> visited) { var currentCity = startCity; visited.AddLast(startCity); if (visited.Count == railway.Cities.Count) { return(visited.ToList()); } foreach (var city in currentCity.Neighbours) { if (!visited.Contains(city.Value)) { DFSTraversal(railway, city.Value, visited); } } return(visited.ToList()); }
static List <City> BFS(RailwayNetwork railway, City startCity) { var visited = new LinkedList <City>(); var nextCities = new Queue <City>(); var currentCity = startCity; visited.AddLast(startCity); while (visited.Count < railway.Cities.Count) { foreach (var city in currentCity.Neighbours) { if (!visited.Contains(city.Value)) { visited.AddLast(city.Value); } nextCities.Enqueue(city.Value); } currentCity = nextCities.Dequeue(); } return(visited.ToList()); }
static void Main(string[] args) { RailwayNetwork railway = new RailwayNetwork(); var sofia = new City("Sofia"); var pleven = new City("Pleven"); var plovdiv = new City("Plovdiv"); var blagoevgrad = new City("Blagoevgrad"); var varna = new City("Varna"); var staraZagora = new City("Stara Zagora"); var burgas = new City("Burgas"); sofia.Neighbours.AddFirst(new LinkedListNode <City>(pleven)); sofia.Neighbours.AddLast(new LinkedListNode <City>(plovdiv)); sofia.Neighbours.AddLast(new LinkedListNode <City>(blagoevgrad)); pleven.Neighbours.AddLast(new LinkedListNode <City>(varna)); plovdiv.Neighbours.AddLast(new LinkedListNode <City>(staraZagora)); plovdiv.Neighbours.AddLast(new LinkedListNode <City>(burgas)); varna.Neighbours.AddLast(new LinkedListNode <City>(staraZagora)); varna.Neighbours.AddLast(new LinkedListNode <City>(burgas)); staraZagora.Neighbours.AddLast(new LinkedListNode <City>(burgas)); railway.Cities.Add(sofia); railway.Cities.Add(pleven); railway.Cities.Add(plovdiv); railway.Cities.Add(blagoevgrad); railway.Cities.Add(varna); railway.Cities.Add(staraZagora); railway.Cities.Add(burgas); BFS(railway, sofia).ForEach(x => Console.WriteLine(x.Name)); Console.WriteLine(); DFS(railway, sofia).ForEach(x => Console.WriteLine(x.Name)); }
static List <City> DFS(RailwayNetwork railway, City startCity) { var visited = new LinkedList <City>(); return(DFSTraversal(railway, startCity, visited)); }