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