Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Graphs/cycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Graph {
constructor(vertices) {
this.vertices = vertices;
this.adjList = new Array(vertices);
for (let i = 0; i < vertices; i++) {
this.adjList[i] = [];
}
}

addEdge(v, w) {
this.adjList[v].push(w);
this.adjList[w].push(v);//Creating Adjacency list.
}

isCyclic() {
const visited = new Array(this.vertices).fill(false);

for (let i = 0; i < this.vertices; i++) {
if (!visited[i]) {
if (this.isCyclicUtil(i, -1, visited)) {//Checking if graph contains a cycle.
return true;
}
}
}

return false;
}

isCyclicUtil(v, parent, visited) {
visited[v] = true;

for (const neighbor of this.adjList[v]) {
if (!visited[neighbor]) {
if (this.isCyclicUtil(neighbor, v, visited)) {
return true;
}
} else if (neighbor !== parent) {
return true;
}
}

return false;
}
}

/*const graph = new Graph(4);
graph.addEdge(0, 1);
graph.addEdge(1, 2);
graph.addEdge(2, 3);

if (graph.isCyclic()) {
console.log("The graph contains a cycle.");
} else {
console.log("The graph does not contain a cycle.");
}
**/