伪代码中的回溯深度优先搜索算法
boolean backtrackDFS(v) {
If (SolutionFound(v)) return true;
Mark vertex v as reached.
for (each unreached vertex u adjacenct from v)
if (backtrakDFS(u)) return true;
Unmark vertex v;
return false;
}
这里为什么需要Unmark vertex v
? 为什么添加这样的行是安全的,因此 v 再次无法到达,会导致重新访问吗?
boolean backtrackDFS(v) {
If (SolutionFound(v)) return true;
Mark vertex v as reached.
for (each unreached vertex u adjacenct from v)
if (backtrakDFS(u)) return true;
Unmark vertex v;
return false;
}
Here why Unmark vertex v
is needed?
and why is it safe to add such line as hence v becomes unreached again, would it result in revisit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有必要。通常,撤消所做的事情并让事情保持原样是你发现它们时的好习惯。
例如,包含该行将允许您多次使用相同的函数进行搜索,而无需单独的操作来清除标记。
I don't think it's needed. It's usually just good practice to undo what you do, and leave things as they were when you found them.
For example, including that line would allow you to search using the same function more than once without a separate operation to clear the marks.