【发布时间】:2020-09-06 04:28:36
【问题描述】:
我正在学习图,并且遇到过这种深度优先搜索算法的实现,它可以在图中找到循环
我们为什么要检查w !== u。 w === u在什么情况下可以?
dfs(v, u) {
this._marked[v] = true;
const adj = this._graph.adj(v);
for (const w of adj) {
if (!this._marked[w]) {
this.dfs(w, v);
} else if (w !== u) {
this.hasCycle = true;
}
}
}
【问题讨论】: