Skip to main content

sccs_of

Function sccs_of 

Source
pub fn sccs_of(graph: &Graph) -> Sccs
Expand description

Returns the SCCs of graph. Uses Tarjan’s algorithm, without recursion.

Every node is in exactly one SCC; a node that lies on no cycle is an SCC of its own. SCC IDs are in reverse topological order: every edge between two SCCs goes from the higher SCC ID to the lower one, so SCC 0 has no edges to other SCCs. The nodes of each SCC are in ascending order. Here, dependencies are the dependencies between the packages of a program, and names holds the names of the packages. The example groups the packages by SCC, in the order of the SCC IDs. In this order, each package comes after the packages it uses, and the packages that use each other come together:

// The SCCs in reverse topological order, each as the names of its packages.
let sccs = components::sccs_of(&dependencies);
let build_order: Vec<Vec<&str>> = (0..sccs.scc_count())
    .map(|scc| {
        sccs.nodes_of(scc)
            .iter()
            .map(|&node| names[node as usize])
            .collect()
    })
    .collect();
// build_order: [["util"], ["cli"], ["json"], ["model", "storage"], ["service"],
//               ["ui"], ["app"]]

Time: O(node_count + edge_count), plus sorting the nodes of each SCC: up to O(node_count log node_count) when one SCC holds most of the nodes. Memory: up to 12 x node_count bytes for the result; while it runs, 24 x node_count bytes more.

§Panics

With an invalid argument message if graph has more than 2^30 - 5 nodes.