Last active
June 8, 2020 12:41
-
-
Save exhesham/7069c16ec8cf419ae518105a7ef64e8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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