public static <V extends Vertex<V, E>, E extends Edge<V, E>> boolean hasCycles(
			Graph<V, E> graph, boolean directed) {
		requireNonNull(graph, "The given start vertex is null");
		Set<V> notVisited = new HashSet<>(graph.getVertices());
		while (!notVisited.isEmpty()) {
			if (hasCycle(notVisited.iterator().next(), new Stack<>(),
					new Stack<>(), notVisited, directed)) {
				return true;
			}
		}
		return false;
	}
public static < V extends Vertex < V , E > , E extends Edge < V , E > > boolean hasCycles ( Graph < V , E > graph , boolean directed ) { requireNonNull ( graph , "The given start vertex is null" ) ; Set < V > notVisited = new HashSet < > ( graph . getVertices ( ) ) ; while ( ! notVisited . isEmpty ( ) ) { if ( hasCycle ( notVisited . iterator ( ) . next ( ) , new Stack < > ( ) , new Stack < > ( ) , notVisited , directed ) ) { return true ; } } return false ; }
This method searches a given {@link Graph} for cycles. This method looks
into all vertices. If a disconnected subgraph exists, this graph is
analyzed, too.

@param <V>
is the actual vertex implementation.
@param <E>
is the actual edge implementation.
@param graph
is the {@link Graph} to be searched for cycles.
@param directed
is to be set to <code>true</code> is the graph is to be
handled as an directed graph (The {@link Pair} result in
{@link Edge#getVertices() is interpreted as startVertex and
targetVertex}.). <code>false</code> is to be set otherwise.
@return <code>true</code> is returned if a cycle was found.
<code>false</code> is returned otherwise.