@@ -0,0 +1,1556 @@
+package algorithms.graph;%0A%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0Aimport datastructures.graph.AbstractGraph;%0Aimport datastructures.graph.UndirectedGraph;%0Aimport datastructures.graph.Vertex;%0Aimport datastructures.util.GraphUtil;%0A%0Apublic class DetectCycleUndirectedGraph2 %7B%0A  private AbstractGraph%3CString%3E graph = null;%0A%0A  public DetectCycleUndirectedGraph2() %7B%0A    graph = new UndirectedGraph%3CString%3E();%0A  %7D%0A%0A  public void constructGraph(String%5B%5D input) %7B%0A    GraphUtil.constructGraph(graph, input);%0A  %7D%0A%0A  public boolean detectCycle() %7B%0A    Set%3CVertex%3CString%3E%3E visited = new HashSet%3CVertex%3CString%3E%3E();%0A    Set%3CVertex%3CString%3E%3E vertices = graph.verticesSet();%0A%0A    for (Vertex%3CString%3E vertex : vertices) %7B%0A      if (!visited.contains(vertex)) %7B%0A        boolean containsCycle = detectCycle(vertex, visited, vertex);%0A        if (containsCycle) %7B%0A          return true;%0A        %7D%0A      %7D%0A    %7D%0A%0A    return false;%0A  %7D%0A%0A  private boolean detectCycle(Vertex%3CString%3E vertex, Set%3CVertex%3CString%3E%3E visited, Vertex%3CString%3E source) %7B%0A    if (vertex == null) %7B%0A      return false;%0A    %7D%0A%0A    visited.add(vertex);    %0A%0A    List%3CVertex%3CString%3E%3E neighbours = graph.neighboursOf(vertex);%0A    for (Vertex%3CString%3E neighbour : neighbours) %7B%0A      graph.removeEdge(vertex, source);%0A      if (!neighbour.equals(source)) %7B%0A        if (visited.contains(neighbour)) %7B%0A          return true;%0A        %7D else %7B%0A          if(detectCycle(neighbour, visited, vertex)) %7B%0A            return true;%0A          %7D%0A        %7D%0A      %7D%0A    %7D%0A%0A    return false;%0A  %7D%0A%7D%0A

Add DetectCycleUndirectedGraph2 using DFS to detect cycles