Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class greedyBFS
- {
- public List<City> cities = new List<City>();
- public List<City> connections = new List<City>();
- public City currentCity = new City("", "0");
- public City minCity = new City("", "0");
- public List<City> closedConnections = new List<City>();
- public void gbfSearch(string intitialCity, string destinationCity)
- {
- int n = 0;
- if (cities == null || cities.Count <= 1) return;
- while (cities.ElementAt(n).name != intitialCity)
- {
- n++;
- }
- //Add first city to current
- currentCity = cities.ElementAt(n);
- while(currentCity.name != destinationCity)
- {
- //loop aslong as there are neighbours
- for (int i = 0; i < currentCity.neighbour.Count; i++)
- {
- connections.Add(currentCity.neighbour.ElementAt(i));
- //Find mindist amongst neighbours
- if (currentCity.distance > currentCity.neighbour.ElementAt(i).distance) minCity = currentCity.neighbour.ElementAt(i);
- //When done set currentCity to closest neighbour city
- if (i == currentCity.neighbour.Count && !closedConnections.Exists((City city) => { return city == currentCity; }))
- {
- closedConnections.Add(currentCity);
- currentCity = minCity;
- }
- }
- }
- Console.WriteLine("currentCity: " + currentCity.name);
- Console.WriteLine("Distance:{0}", minCity.distance);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement