Given a directed graph as an adjacency list, return every distinct simple directed cycle.
For this practice version, a simple directed cycle follows directed edges back to its starting vertex without repeating any other vertex. Represent a cycle as an array of its vertices without repeating the starting vertex at the end. The last vertex must have an edge to the first. A self-loop is a one-vertex cycle.
Input
-
graph
: an array of adjacency lists. Vertices are labeled
0
through
graph.length - 1
;
graph[u]
lists the vertices reachable by one directed edge from
u
.
Output
Return the cycles in canonical form:
-
Rotate each cycle so its smallest vertex label appears first.
-
List each such cycle once. Rotations of a sequence represent the same cycle.
-
Preserve edge direction. Reversing a sequence does not generally represent the same directed cycle.
-
Sort the resulting arrays lexicographically by their vertex sequences, using the shorter sequence first when one is a prefix of another.
Constraints and Edge Cases
-
For this practice version,
1 <= graph.length <= 8
.
-
Adjacency lists may be unsorted but contain no repeated neighbors.
-
The graph may be disconnected and may contain self-loops.
-
If the graph contains no directed cycles, return an empty array.
Example 1
graph = [[1], [2], [0,1], []]
output = [[0,1,2], [1,2]]
The cycles [1,2,0] and [2,0,1] are rotations of [0,1,2], so they are not additional results.
Example 2
graph = [[0,1,2], [0,2], [0,1]]
output = [[0], [0,1], [0,1,2], [0,2], [0,2,1], [1,2]]
Both [0,1,2] and [0,2,1] are valid because their directed edges exist, and they are distinct. The self-loop contributes [0].