Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1.    class greedyBFS
  2.     {
  3.         public List<City> cities = new List<City>();
  4.         public List<City> connections = new List<City>();
  5.         public City currentCity = new City("", "0");
  6.         public City minCity = new City("", "0");
  7.         public List<City> closedConnections = new List<City>();
  8.         public void gbfSearch(string intitialCity, string destinationCity)
  9.         {            
  10.             int n = 0;
  11.             if (cities == null || cities.Count <= 1) return;
  12.             while (cities.ElementAt(n).name != intitialCity)
  13.             {
  14.                 n++;              
  15.             }
  16.             //Add first city to current
  17.             currentCity = cities.ElementAt(n);
  18.  
  19.             while(currentCity.name != destinationCity)
  20.             {              
  21.                 //loop aslong as there are neighbours
  22.                 for (int i = 0; i < currentCity.neighbour.Count; i++)
  23.                 {
  24.                     connections.Add(currentCity.neighbour.ElementAt(i));
  25.                     //Find mindist amongst neighbours
  26.                     if (currentCity.distance > currentCity.neighbour.ElementAt(i).distance) minCity = currentCity.neighbour.ElementAt(i);
  27.                     //When done set currentCity to closest neighbour city
  28.                     if (i == currentCity.neighbour.Count && !closedConnections.Exists((City city) => { return city == currentCity; }))
  29.                     {
  30.                         closedConnections.Add(currentCity);
  31.                         currentCity = minCity;
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             Console.WriteLine("currentCity: " + currentCity.name);
  37.             Console.WriteLine("Distance:{0}",  minCity.distance);
  38.             return;
  39.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement