java - My BFS recursive call does not stop -
i have following code find paths between starting , ending node .
graph.java
import java.util.hashmap; import java.util.linkedhashset; import java.util.linkedlist; import java.util.map; import java.util.set; public class graph { private map<string, linkedhashset<string>> map = new hashmap(); public void addedge(string node1, string node2) { linkedhashset<string> adjacent = map.get(node1); if(adjacent==null) { adjacent = new linkedhashset(); map.put(node1, adjacent); } adjacent.add(node2); } public void addtwowayvertex(string node1, string node2) { addedge(node1, node2); addedge(node2, node1); } public boolean isconnected(string node1, string node2) { set adjacent = map.get(node1); if(adjacent==null) { return false; } return adjacent.contains(node2); } public linkedlist<string> adjacentnodes(string last) { linkedhashset<string> adjacent = map.get(last); if(adjacent==null) { return new linkedlist(); } return new linkedlist<string>(adjacent); }
}
and search.java
import java.util.linkedlist; public class search { private static final string start = "d"; private static final string end = "e"; public static void main(string[] args) { // graph directional graph graph = new graph(); graph.addedge("a", "b"); graph.addedge("a", "c"); graph.addedge("b", "a"); graph.addedge("b", "d"); graph.addedge("b", "e"); // one-way connection graph.addedge("b", "f"); graph.addedge("c", "a"); graph.addedge("c", "e"); graph.addedge("c", "f"); graph.addedge("d", "b"); graph.addedge("e", "c"); graph.addedge("e", "f"); graph.addedge("f", "b"); graph.addedge("f", "c"); graph.addedge("f", "e"); linkedlist<string> visited = new linkedlist(); visited.add(start); new search().breadthfirst(graph, visited); } private void breadthfirst(graph graph, linkedlist<string> visited) { linkedlist<string> nodes = graph.adjacentnodes(visited.getlast()); // examine adjacent nodes (string node : nodes) { if (visited.contains(node)) { continue; } if (node.equals(end)) { visited.add(node); printpath(visited); visited.removelast(); return ; //break; } } // in breadth-first, recursion needs come after visiting adjacent nodes (string node : nodes) { if (visited.contains(node) || node.equals(end)) { continue; } visited.addlast(node); breadthfirst(graph, visited); visited.removelast(); } } private void printpath(linkedlist<string> visited) { (string node : visited) { system.out.print(node); system.out.print(" "); } system.out.println(); } }
i want stop recursive call after finding first path between 2 nodes . return statement before break statement works fine if add many undirected edges not stop printing paths between 2 nodes .
in bfs, have "color" nodes know if visited them already.
http://www.personal.kent.edu/~rmuhamma/algorithms/myalgorithms/graphalgor/breadthsearch.htm
to keep track of progress, breadth-first-search colors each vertex. each vertex of graph in 1 of 3 states:
- undiscovered;
- discovered not explored; and
- fully explored.
the state of vertex, u, stored in color variable follows:
- color[u] = white - "undiscovered" state,
- color [u] = gray - "discovered not explored" state, and
- color [u] = black - "fully explored" state.
Comments
Post a Comment