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
59 changes: 59 additions & 0 deletions JAVA/CycleDetectionPrt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CycleDetectionPrt {

public static void main(String[] args) {

Map<Integer, List<Integer>> graph = new HashMap<>();
graph.put(0, List.of(1, 2));
graph.put(1, List.of(2));
graph.put(2, List.of(0, 3));
graph.put(3, List.of(3));

if (containsCycle(graph)) {
System.out.println("The graph contains a cycle.");
} else {
System.out.println("The graph does not contain a cycle.");
}
}

public static boolean containsCycle(Map<Integer, List<Integer>> graph) {
//track of visited vertices during DFS
boolean[] visited = new boolean[graph.size()];
// track of vertices in the current DFS traversal
boolean[] currentlyInStack = new boolean[graph.size()];

for (int vertex : graph.keySet()) {
if (!visited[vertex] && isCyclic(graph, vertex, visited, currentlyInStack)) {
return true;
}
}

return false;
}

private static boolean isCyclic(Map<Integer, List<Integer>> graph, int vertex,
boolean[] visited, boolean[] currentlyInStack) {
visited[vertex] = true;
currentlyInStack[vertex] = true;

if (graph.containsKey(vertex)) {
for (int neighbor : graph.get(vertex)) {
if (!visited[neighbor]) {
if (isCyclic(graph, neighbor, visited, currentlyInStack)) {
return true;
}
} else if (currentlyInStack[neighbor]) {
return true; //Cycle detected
}
}
}

currentlyInStack[vertex] = false; //Backtrack

return false;
}
}