From da64bcb5cf504918fcd75e23bdd7042b1f2e09af Mon Sep 17 00:00:00 2001 From: mabhinav004 <59362898+mabhinav004@users.noreply.github.com> Date: Sun, 10 Oct 2021 19:00:19 +0530 Subject: [PATCH] union_and_find_approach_graph Detects cycle in O(n) amortized time complexity. --- Java/union_and_find_approach_graph | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Java/union_and_find_approach_graph diff --git a/Java/union_and_find_approach_graph b/Java/union_and_find_approach_graph new file mode 100644 index 0000000..1ad88b1 --- /dev/null +++ b/Java/union_and_find_approach_graph @@ -0,0 +1,57 @@ + +//Detects cycle in O(n) amortized time complexity. + +import java.util.*; +import java.lang.*; +import java.io.*; + +class Solution +{ + //Function to detect cycle in an undirected graph. + public boolean isCycle(int V, ArrayList> adj) + { + + //Union find + + int[] parent=new int[V]; + + Arrays.fill(parent,-1); + + for(int src=0;src