Additional recursive depth search space for path storage

I use depth search to identify paths in an oriented weighted graph, revising nodes that belong to the cycle, and setting cutoff conditions based on the total distance traveled or stopping from the source node.

As I understand it, when recursing, an explicit stack structure is not required for the first depth search, so I was wondering if I could simplify my code even further below, somehow without an explicit stack:

public class DFSonWeightedDirectedGraph {

    private static final String START = "A";
    private static final String END = "E";
    private int pathLength = 0;
    private int stops = 0;

    public static void main(String[] args) {
        //this is a directed weighted graph
        WeightedDirectedGraph graph = new WeightedDirectedGraph();
        graph.addEdge("A", "B", 15);
        graph.addEdge("A", "D", 15);
        graph.addEdge("A", "E", 27);
        //(...) more edges added


        Stack<String> visited = new Stack<String>();        
        visited.push(START);

        new DFSonWeightedDirectedGraph().depthFirst(graph, visited);
    }

    private void depthFirst(WeightedDirectedGraph graph, Stack<String> visited) {

        Collection<Map.Entry<String, Integer>> tree_of_children 
            = graph.get_tree_of_children(visited.peek());

        for (Map.Entry<String, Integer> child : tree_of_children) {


            if(pathLength + child.getValue()>= 20){
                continue;
            }       


            visited.push(child.getKey());
            pathLength += child.getValue();
            stops += 1;         

            if (child.getKey().equals(END)) {
                printPath(visited);
            }

            depthFirst(graph, visited); 
            visited.pop();  
            pathLength -= child.getValue();
            stops -= 1; 
        }
    }

    private void printPath(Stack<String> visited) {
        for (String node : visited) {
            System.out.print(node);
            System.out.print(" ");
        }
        System.out.println("[path length: "+pathLength +
                " stops made: " + stops +"]");
    }
}

, , . , , , , ? .

+5
4

, . ; , .

( ), . , node (, , "" ).

+1

node, "" . . ( ).

, - node -. , . node, , , ..

, node , 1 node, 1 node .

, , node , N N- node. , , , (, -, ).

0

. node .

, .. printPath(), , . , .

, . .

0

: .

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) {
   ...
   //visited.push(child); <-- No longer needed
   ...
   depthFirst(graph, child);

(), "", /, , .

, ( global/class , ), , , depthFirst() , .

.

private void depthFirst(WeightedDirectedGraph graph, String node, Set<String> visited) {
   visited.add(node); // mark current node as visited
   ...
   //visited.push(child); <-- No longer needed
   ...
   if (!visited.contains(child)){ // don't visit nodes we have worked on already
      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.

-1
source

All Articles