-1

I'm trying to traverse a graph, iteratively, in dfs order.
Below is the code I had. When I tested this, the output is NOT the same as the output of a recursive dfs (also included below).

Can someone help debugging this?

  public static void dfs(GraphNode root) {
        if (root != null && !root.visited) {
            for (GraphNode kid : root.children) {
                dfs(kid);
            }
            System.out.println(root);
            root.visited = true;
        }
    }

  public static void dfsIter(GraphNode root) {
        Stack<GraphNode> stack = new Stack<>();
        stack.push(root);

        do {
            GraphNode cur = stack.pop();
            if (cur != null && !cur.visited) {
                // leaf node or all children have been visisted
                if (cur.children.isEmpty() || childrenVisisted(cur)) {
                    System.out.println(cur);
                    cur.visited = true;
                }
                else {
                    // put it back
                    stack.push(cur);
                    for (GraphNode child :cur.children) {
                        if (!child.visited) {
                            stack.push(child);
                        }
                    }
                }

            }
        }
        while (!stack.isEmpty());
    }

    static boolean childrenVisisted(GraphNode node) {
        for (GraphNode child : node.children) {
            if (!child.visited) return false;
        }
        return true;
    }
c0der
  • 18,467
  • 6
  • 33
  • 65
One Two Three
  • 22,327
  • 24
  • 73
  • 114
  • What is different between the two outputs? – Tyler Oct 23 '17 at 20:13
  • I have downvoted this question because there is no evidence of any debugging performed on this code. Please [edit] your question to show us what your debugging has uncovered, as well as a specific question about a specific line of code. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Joe C Oct 23 '17 at 20:14
  • I have debugged it. At first, I suspect the order was diffrent due to the order in which the children were visisted (in iterative version), so I tried to push the children onto the stack in a reverse order. That still didn't make any diff – One Two Three Oct 23 '17 at 20:15
  • Why `root.visited = true;` is situated after the recursive calls? – DAle Oct 23 '17 at 20:18
  • @DAIe: Because the root is not visited until the recursive calls return – One Two Three Oct 23 '17 at 20:24

1 Answers1

2
  1. In a recursive DFS, you should label the vertex first and then iterate through the children. Otherwise, you'll get an infinite loop if the graph has cycles.
  2. If you want the same traversing order in the iterative approach you need to add children in reverse order to the stack. Because you pop the last pushed element first.
DAle
  • 8,990
  • 2
  • 26
  • 45
  • Actually, I found out that (1) was the problem. (THe children was in the right order. but the flag has to be set before descending ...) Thanks – One Two Three Oct 23 '17 at 20:50