Skip to main content
deleted 36 characters in body
Source Link
Leniel Maccaferri
  • 102.7k
  • 48
  • 381
  • 495

OK, I have actually modified Dijkstra class to do BFS as well and it got me all possible routes. I added this method:

 public void breadthFirstBreadthFirst(Edge graph, LinkedList<String> visited)  {   LinkedList<String> nodes = graph.adjacentNodes(visited.Last());      // examineExamine adjacent nodes   foreach (String node in nodes)   {   if (visited.Contains(node)) { {  continue; }  }     if (node.Equals(endNode)) {   {  visited.AddLast(node);     printPath(visited);     visited.RemoveLast();  RouteCounter++;   break;   }   }      // inIn breadth-first, recursion needs to come after visiting adjacent nodes   foreach (String node in nodes)  {      if  if(visited.Contains(node) || node.Equals(endNode)) { {  continue;  continue;  }     visited.AddLast(node);     // Recursion  breadthFirstBreadthFirst(graph, visited);     visited.RemoveLast();   }  }   usage would be like this: 

Usage would be like this:

Dijkstra d = new Dijkstra(_edges, _nodes);    LinkedList<String> visited = new LinkedList<string>(); //collecting all routes  visited.AddFirst(start);   d.breadthFirstBreadthFirst(graph, visited);  

OK, I have actually modified Dijkstra class to do BFS as well and it got me all possible routes. I added this method:

 public void breadthFirst(Edge graph, LinkedList<String> visited)  {   LinkedList<String> nodes = graph.adjacentNodes(visited.Last());   // examine adjacent nodes   foreach (String node in nodes)   {   if (visited.Contains(node)) { continue; }  if (node.Equals(endNode)) { visited.AddLast(node);   printPath(visited);   visited.RemoveLast();  RouteCounter++;   break;   }   } // in breadth-first, recursion needs to come after visiting adjacent nodes   foreach (String node in nodes) {   if (visited.Contains(node) || node.Equals(endNode)) { continue;  } visited.AddLast(node); breadthFirst(graph, visited);   visited.RemoveLast();   }  }   usage would be like this: Dijkstra d = new Dijkstra(_edges, _nodes);  LinkedList<String> visited = new LinkedList<string>(); //collecting all routes  visited.AddFirst(start); d.breadthFirst(graph, visited);  

OK, I have actually modified Dijkstra class to do BFS as well and it got me all possible routes. I added this method:

public void BreadthFirst(Edge graph, LinkedList<String> visited) { LinkedList<String> nodes = graph.adjacentNodes(visited.Last());    // Examine adjacent nodes foreach (String node in nodes) { if (visited.Contains(node)) {  continue; }     if (node.Equals(endNode))   {  visited.AddLast(node);    printPath(visited);    visited.RemoveLast(); break; } }      // In breadth-first, recursion needs to come after visiting adjacent nodes foreach (String node in nodes)  {      if(visited.Contains(node) || node.Equals(endNode)) {  continue;  }     visited.AddLast(node);     // Recursion  BreadthFirst(graph, visited);    visited.RemoveLast(); } } 

Usage would be like this:

Dijkstra d = new Dijkstra(_edges, _nodes);   LinkedList<String> visited = new LinkedList<string>(); //collecting all routes visited.AddFirst(start);   d.BreadthFirst(graph, visited); 
Source Link
Zulu Z
  • 1.2k
  • 5
  • 14
  • 27

OK, I have actually modified Dijkstra class to do BFS as well and it got me all possible routes. I added this method:

 public void breadthFirst(Edge graph, LinkedList<String> visited) { LinkedList<String> nodes = graph.adjacentNodes(visited.Last()); // examine adjacent nodes foreach (String node in nodes) { if (visited.Contains(node)) { continue; } if (node.Equals(endNode)) { visited.AddLast(node); printPath(visited); visited.RemoveLast(); RouteCounter++; break; } } // in breadth-first, recursion needs to come after visiting adjacent nodes foreach (String node in nodes) { if (visited.Contains(node) || node.Equals(endNode)) { continue; } visited.AddLast(node); breadthFirst(graph, visited); visited.RemoveLast(); } } usage would be like this: Dijkstra d = new Dijkstra(_edges, _nodes); LinkedList<String> visited = new LinkedList<string>(); //collecting all routes visited.AddFirst(start); d.breadthFirst(graph, visited);