c# - How to replace inner Foreach loops with a Recursive method -


i'm bit stuck on creating method generic can loop through directed graph possible routes based on max number of depth, example:

all routes maximum of 4 hops returns following concatenated routes:

abcdc   abcde   abceb   adcdc   adcde   adceb   adebc   aebcd   aebce 

the routes data following json values:

"routes": [     {       "from": "a",       "to": "b",       "distance": 5     },     {       "from": "a",       "to": "d",       "distance": 5     },     {       "from": "a",       "to": "e",       "distance": 7     },     {       "from": "b",       "to": "c",       "distance": 4     },     {       "from": "c",       "to": "d",       "distance": 8     },     {       "from": "c",       "to": "e",       "distance": 2     },     {       "from": "d",       "to": "c",       "distance": 8     },     {       "from": "d",       "to": "e",       "distance": 6     },     {       "from": "e",       "to": "b",       "distance": 3     } ] 

and inner loops, fixed 4 times following, start "a", end "c" , int stops value should determine amount of recursion, instead of hard coded loops. assistance or guidance in right direction apreciated.

public void getroutes(string start, string end, int stops) {     var temproutes = graph.routes;      foreach(var route in temproutes.where(x => x.from == start))     {         foreach(var innerroute in temproutes.where(x => x.from == route.to))         {             foreach(var innerroute2 in temproutes.where(x => x.from == innerroute.to))             {                 foreach(var innerroute3 in temproutes.where(x => x.from == innerroute2.to))                 {                     totalpath = start + route.to + innerroute.to + innerroute2.to + innerroute3.to;                      pathcounter.add(totalpath, totalpath.length);                 }             }         }     } } 

try following solution:

the following class models link between 2 nodes in graph:

public class nodelink {     public string { get; set; }     public string { get; set; } } 

the following class can search graph recursively routes:

public class routefinder {     public ienumerable<string> findroutes(nodelink[] node_links, string start, string end, int max_length)     {         if (max_length == 0)             yield break;          if (start == end)         {             yield return start;             yield break;         }          if (max_length == 1)         {             yield break;         }          foreach (var route in node_links.where(x => x.from == start))         {             ienumerable<string> sub_routes = findroutes(node_links, route.to, end, max_length - 1);              foreach (string sub_route in sub_routes)             {                 yield return start + sub_route;             }         }     } } 

and can use this:

var node_links = new list<nodelink> {     new nodelink {from = "a", = "b"},     new nodelink {from = "a", = "c"},     new nodelink {from = "c", = "d"},     new nodelink {from = "c", = "e"},     new nodelink {from = "e", = "f"},     new nodelink {from = "b", = "f"} };  routefinder finder = new routefinder();  foreach (string path in finder.findroutes(node_links.toarray(), "a", "f", 4)) {     console.writeline(path); } 

this output get:

abf acef 

Comments