Skip to contents

[Experimental]

This function returns a cycle of the graph, in terms of both its vertices and edges. If the graph is acyclic, it returns empty vertex and edge sequences.

Use is_acyclic() to determine if a graph has cycles, without returning a specific cycle.

Usage

find_cycle(graph, mode = c("out", "in", "all", "total"))

Arguments

graph

The input graph.

mode

Character constant specifying how to handle directed graphs. out follows edge directions, in follows edges in the reverse direction, and all ignores edge directions. Ignored in undirected graphs.

Value

A list of integer vectors, each integer vector is a path from the source vertex to one of the target vertices. A path is given by its vertex ids.

find_cycle().

Examples


g <- make_lattice(c(3, 3))
find_cycle(g)
#> $vertices
#> + 4/9 vertices, from 49710f7:
#> [1] 9 6 5 8
#> 
#> $edges
#> + 4/12 edges from 49710f7:
#> [1] 8--9 6--9 5--6 5--8
#> 

# Empty results are returned for acyclic graphs
find_cycle(sample_tree(5))
#> $vertices
#> + 0/5 vertices, from 687cb2d:
#> 
#> $edges
#> + 0/4 edges from 687cb2d:
#>