/** * Returns true if {@code network} has at least one cycle. A cycle is defined as a non-empty * subset of edges in a graph arranged to form a path (a sequence of adjacent outgoing edges) * starting and ending with the same node. * * <p>This method will detect any non-empty cycle, including self-loops (a cycle of length 1). */ public static boolean hasCycle(Network<?, ?> network) { // In a directed graph, parallel edges cannot introduce a cycle in an acyclic graph. // However, in an undirected graph, any parallel edge induces a cycle in the graph. if (!network.isDirected() && network.allowsParallelEdges() && network.edges().size() > network.asGraph().edges().size()) { return true; } return hasCycle(network.asGraph()); }
@Test public void hasCycle_twoParallelEdges() { for (MutableNetwork<Integer, String> network : networksToTest) { network.addEdge(1, 2, "1-2a"); network.addEdge(1, 2, "1-2b"); } assertThat(hasCycle(directedNetwork)).isFalse(); assertThat(hasCycle(undirectedNetwork)).isTrue(); // cyclic in undirected case }
@Test public void hasCycle_cyclicMultigraph() { for (MutableNetwork<Integer, String> network : networksToTest) { network.addEdge(1, 2, "1-2a"); network.addEdge(1, 2, "1-2b"); network.addEdge(2, 3, "2-3"); network.addEdge(3, 1, "3-1"); } assertThat(hasCycle(directedNetwork)).isTrue(); assertThat(hasCycle(undirectedNetwork)).isTrue(); } }
/** * Returns true if {@code network} has at least one cycle. A cycle is defined as a non-empty * subset of edges in a graph arranged to form a path (a sequence of adjacent outgoing edges) * starting and ending with the same node. * * <p>This method will detect any non-empty cycle, including self-loops (a cycle of length 1). */ public static boolean hasCycle(Network<?, ?> network) { // In a directed graph, parallel edges cannot introduce a cycle in an acyclic graph. // However, in an undirected graph, any parallel edge induces a cycle in the graph. if (!network.isDirected() && network.allowsParallelEdges() && network.edges().size() > network.asGraph().edges().size()) { return true; } return hasCycle(network.asGraph()); }
/** * Returns true if {@code network} has at least one cycle. A cycle is defined as a non-empty * subset of edges in a graph arranged to form a path (a sequence of adjacent outgoing edges) * starting and ending with the same node. * * <p>This method will detect any non-empty cycle, including self-loops (a cycle of length 1). */ public static boolean hasCycle(Network<?, ?> network) { // In a directed graph, parallel edges cannot introduce a cycle in an acyclic graph. // However, in an undirected graph, any parallel edge induces a cycle in the graph. if (!network.isDirected() && network.allowsParallelEdges() && network.edges().size() > network.asGraph().edges().size()) { return true; } return hasCycle(network.asGraph()); }
if (Graphs.hasCycle(graph))