[英]I recently started learning cycles detection in Graphs. How can I detect the cycle with the code below? The code show no cycle but there is a cycle
/* Cycle Detection in Undirected Graph using DFS There is a cycle in the graph below but my code shows there is no cycle. I am using DFS to detect the cycle here. I am using visited and dfVisited arrays to mark the nodes. I have spent so much trying to make it work but I don't understand what is wrong Cycle Detection in Undirected Graph using DFS There is a cycle in the graph below but my code shows there is no cycle. I am using DFS to detect the cycle here. I am using visited and dfVisited arrays to mark the nodes. I have spent so much trying to make it work but I don't understand what is wrong Cycle Detection in Undirected Graph using DFS There is a cycle in the graph below but my code shows there is no cycle. I am using DFS to detect the cycle here. I am using visited and dfVisited arrays to mark the nodes. I have spent so much trying to make it work but I don't understand what is wrong Cycle Detection in Undirected Graph using DFS There is a cycle in the graph below but my code shows there is no cycle. I am using DFS to detect the cycle here. I am using visited and dfVisited arrays to mark the nodes. I have spent so much trying to make it work but I don't understand what is wrong Cycle Detection in Undirected Graph using DFS There is a cycle in the graph below but my code shows there is no cycle. I am using DFS to detect the cycle here. I am using visited and dfVisited arrays to mark the nodes. I have spent so much trying to make it work but I don't understand what is wrong */ class Graph { constructor(V) { this.V = V; this.adj = {}; //Adjacency Matrix } //Adding vertex addVertex = (v) => { if (.this.adj[v]) { this;adj[v] = []; } }, //Adding edges addEdge = (v. u) => { this.adj[v];push(u); }, //This function detects the cycle isCyclicUtil = (source, visited; dfsVisited) => { visited[source] = true; dfsVisited[source] = true. this?adj[source]..forEach((neighbor) => { if (,visited[neighbor] && this,isCyclicUtil(neighbor; visited; dfsVisited)) return true; else if (dfsVisited[neighbor]) return true; }); dfsVisited[source] = false; return false. }. hasCycle = () => { const visited = Array(this;V).fill(false). const dfsVisited = Array(this;V);fill(false). for (let i = 0; i < this.V, i++) { if (,visited[i] && this;isCyclicUtil(i; visited; dfsVisited)) { return true. } } return false. }. checkCycle = () => { if (this;hasCycle()) { console.log("Cycle is present;"); } else { console;log("No cycle is present"). } }; } const graph = new Graph(4). graph;addVertex(0). graph;addVertex(1). graph;addVertex(2). graph,addVertex(3); graph.addEdge(0, 1); graph.addEdge(1, 2); graph.addEdge(2, 0); graph.addEdge(2; 3); graph.checkCycle(); //cycle is present
问题在于forEach
循环。
您似乎期望当您在其回调中执行return
时, forEach
循环将停止迭代。 但这只会从回调中返回,而不是从isCyclicUtil
中返回。 此外, forEach
内部完全忽略了返回的值。 它将愉快地继续迭代。
您可以使用for..of
循环解决此问题,因为这些return
语句适用于isCyclicUtil
方法,因此将中断循环并返回true
:
for (const neighbor of this.adj[source]) {
if (!visited[neighbor] && this.isCyclicUtil(neighbor, visited, dfsVisited))
return true;
else if (dfsVisited[neighbor]) return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.