Skip to main content

find_cycle

Function find_cycle 

Source
pub fn find_cycle(graph: &Graph) -> Vec<u32>
Expand description

Returns the nodes of a cycle of graph in the order of its edges, or an empty vector if graph is a DAG.

The last node has an edge to the first, and a self-loop is a cycle of one node. The search goes depth-first, from each unvisited node in ascending order and through the out-nodes in ascending order, and returns the first cycle it closes: always the same one, but not necessarily the shortest. Here, the cycle in the dependencies between the packages of a program:

let cycle = graphs::find_cycle(&dependencies);
// cycle: [4, 5] - model and storage, closed by the edge storage -> model

For a cycle inside a given SCC, search the subgraph of its nodes from remapped; the node mapping of the subgraph translates the result back. The result is a vector the caller owns, with each node once - Java returns an unmodifiable IntList.

Time: O(node_count + edge_count), stopping at the first cycle. Memory: 4 bytes per node of the cycle for the result; while it runs, 2 bits per node and up to 8 x node_count bytes for the stack, which grows with the deepest path.

ยงPanics

With an invalid argument message if a depth-first path is longer than 2^30 - 5 nodes.