DFS节点遍历序列

问题描述

我正在尝试计算无向节点的遍历

使用 DFS 的图表。据我了解,我的实现是

正确,但结果不正确。


这是我的实现:

public void calculateDfsSequence(long firstNode) {
    long parent = firstNode;
    List<Long> parents = new ArrayList<>();
    List<Long> marked = new ArrayList<>();
    Stack<Long> stack = new Stack<Long>();
    stack.push(firstNode);
    while (!stack.isEmpty()) {
        Long node = stack.pop();
        if(!res.dfsNodeSequence.contains(node)) {
            res.dfsNodeSequence.add(node);
            long[] neighbors = selectionSort(xgraph.getNeighborsOf(node));
            int pushedCnt = 0;
            for (long c : neighbors) {
                if (!res.dfsNodeSequence .contains(c)) {
                    if (pushedCnt == 0) {
                        parents.add(node);
                        pushedCnt = 1;
                    }
                    System.out.println(" pushing     " + c);
                    stack.push(c);
                }
            }
            if (pushedCnt == 0 && !stack.isEmpty()) {
                for (int i = parents.size(); i > 0; i--) {
                    res.dfsNodeSequence.add(parents.get(i - 1));
                }
            }
        }
    }
}

关于如何计算 dfs 节点遍历的任何建议 真的很有帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)