dfs(node start) {
stack s;
s.push(start);
while (s.empty() == false) {
top = s.top();
s.pop();
mark top as visited;
check for termination condition
add all of top's unvisited neighbors to the stack.
mark top as not visited;
}
}
dfs(node current) {
mark current as visited;
visit all of current's unvisited neighbors by calling dfs(neighbor)
mark current as not visited;
}
为什么作者在访问所有邻居后将顶点标记为未访问?这是这里必需的步骤吗?既然我们只是在搜索一个顶点,那么如果不将顶点标记为未访问过,它就不能工作吗?
此处的另一个链接实际上并没有将顶点设置为已访问后将其标记为未访问。