我对深度优先遍历的信息存在矛盾,希望能得到一些帮助,了解如何构建程序。给定一个特定的图形,我想打印出顶点的序列。用户将输入要从哪个节点开始遍历。我正在查看不同的示例,但我不理解深度优先遍历的顺序如何工作。我有以下伪代码可供使用:
public DFS() {
DFS(v)
{ num(v) = i ++;
for all vertices u adjacent to v
{ if num(u) is 0
attach edge(uv) to edges;
DFS(u);
}
}
depthFirstSearch()
{ for all vertices v
num(v) = 0;
edges = null; //vector of all edges
i=1;
while there is a vertex v such that num(v) is 0
DFS(v);
output edges;
}