From 9fa981758e26a876abf1533612c1a0dc8c7a5940 Mon Sep 17 00:00:00 2001 From: KasunDevaka <52396159+KasunDevaka@users.noreply.github.com> Date: Thu, 19 Oct 2023 10:20:47 +0530 Subject: [PATCH] Create CycleDetectionPrt.java --- JAVA/CycleDetectionPrt.java | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 JAVA/CycleDetectionPrt.java diff --git a/JAVA/CycleDetectionPrt.java b/JAVA/CycleDetectionPrt.java new file mode 100644 index 0000000..f49f5e7 --- /dev/null +++ b/JAVA/CycleDetectionPrt.java @@ -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> 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> 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> 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; + } +}