public boolean hasCycleDFS(Graph<T> graph){
        Set<Vertex<T>> visited = new HashSet<Vertex<T>>();
        for(Vertex<T> vertex : graph.getAllVertex()){
            if(visited.contains(vertex)){
                continue;
            }
            boolean flag = hasCycleDFSUtil(vertex, visited, null);
            if(flag){
                return true;
            }
        }
        return false;
    }
Date 10/11/2014
@author Tushar Roy

Given an undirected graph find cycle in this graph.

Solution
This can be solved in many ways.
Below is the code to solve it using disjoint sets and DFS.

Runtime and space complexity for both the techniques is O(v)
where v is total number of vertices in the graph.
src/com/interview/graph/CycleUndirectedGraph.java