: .
DFS . , START END, - . , , , , , .
, , node dfs. node.
,
private void depthFirst(WeightedDirectedGraph graph, Stack<String> visited) {
...
visited.push(child);
...
depthFirst(graph, visited);
private void depthFirst(WeightedDirectedGraph graph, String node) {
...
...
depthFirst(graph, child);
(), "", /, , .
, ( global/class , ), , , depthFirst() , .
.
private void depthFirst(WeightedDirectedGraph graph, String node, Set<String> visited) {
visited.add(node);
...
...
if (!visited.contains(child)){
depthFirst(graph, child);
}
, . , , DFS . "/ " . CLRS ( ), . DFS , .
Edit: I did not mention how you could get the path after replacing the stack. This can be easily done using the map that stores the parent of each node as it is studied. The path (if found) can be obtained using the printPath (String node) recursive function, which prints the node character passed to it and calls its parent again.
source
share