Skip to content

Instantly share code, notes, and snippets.

@exhesham
Last active Jun 8, 2020
Embed
What would you like to do?
public boolean isCycleUtil(int vertex, boolean[] visited, boolean[] recursiveArr) {
visited[vertex] = true;
recursiveArr[vertex] = true;
//recursive call to all the adjacent vertices
for (int i = 0; i < adjList[vertex].size(); i++) {
//if not already visited
int adjVertex = adjList[vertex].get(i);
if (!visited[adjVertex] && isCycleUtil(adjVertex, visited, recursiveArr)) {
return true;
} else if (recursiveArr[adjVertex])
return true;
}
//if reached here means cycle has not found in DFS from this vertex
//reset
recursiveArr[vertex] = false;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment