3.2 Condense each group into one node to get a DAG
You condense each strongly connected component into one node and get a graph without cycles that keeps the edges between the components.
Problem
Section titled “Problem”You want to run an algorithm that works only on a graph without cycles, but your graph has cycles. Such algorithms compute a topological order, levels, a transitive reduction or a drawing in layers. Treat each group of nodes that reach each other as one unit. Such a group is an SCC (strongly connected component). Condense each SCC into a single node. The result is called the condensed graph. It is a DAG (directed acyclic graph). All edges from one group to another become one edge. You need this for:
- an overview of a large dependency graph, with each group of packages that use each other drawn as one box;
- an analysis of a call graph that treats each group of mutually recursive functions as one unit;
- an algorithm for DAGs on a graph with cycles, whose results then hold for each group.
Example
Section titled “Example”The example is the grammar of a small programming language:
program = { statement } ;statement = block | ifStatement | assignment ;block = "{" { statement } "}" ;ifStatement = "if" "(" expression ")" statement ;assignment = name "=" expression ";" ;expression = term { "+" term } ;term = factor { "*" factor } ;factor = number | name | "(" expression ")" ;name and number are rules for single tokens. The example graph rules models how the rules use
each other. It has an edge from each rule to each rule it uses. The array names holds the names of
the rules. A tool documents the grammar with an overview: one box for each group of rules that use
each other, and an arrow from each box to each box whose rules it uses. These groups are the SCCs of
the graph.
See Find the groups of nodes that reach each other. The picture
before shows the two SCCs with a cycle as gray frames, and the edges of their cycles in orange.
Which group uses which? Condensing each SCC into one node gives the graph of the picture after. Its
node IDs, from 0 to 5, are the SCC IDs:
- Rule asked about
- SCC with a cycle
- Edge on a cycle
- SCC as one node
- Edge between SCCs
The three rules of the statements became one node, and so did the three rules of the expressions.
The edges inside the SCCs are gone. Each edge from one SCC to another became an edge between their
nodes, such as 4 -> 2 for ifStatement -> expression. Several edges between the same two SCCs
would become one.
Solution
Section titled “Solution”NodePartition.condenseNodePartition::condenseNodePartition.condenseNodePartition.Condense builds the condensed graph from the SccsSccsSccsSccs of
the rules. The names of its nodes come from the rules in each SCC:
// Condense each group of rules that use each other into one node.Sccs sccs = Components.sccsOf(rules);Graph condensed = sccs.condense();boolean acyclic = Graphs.isDag(condensed); // true// condensed: 0 -> [], 1 -> [], 2 -> [0, 1], 3 -> [0, 2], 4 -> [2, 3], 5 -> [4]
// Name each node of the condensed graph after the rules in it.String[] groupNames = new String[sccs.sccCount()];for (int scc = 0; scc < sccs.sccCount(); scc++) { groupNames[scc] = sccs.nodesOf(scc).intStream() .mapToObj(rule -> names[rule]).collect(Collectors.joining("/"));}// groupNames: [name, number, expression/term/factor, assignment,// statement/block/ifStatement, program]
// Which groups does the group of statement use? The out-nodes of its node.SortedIntView usedByStatements = condensed.outNodes(sccs.sccOf(statement));// usedByStatements: [2, 3] - expression/term/factor and assignment// Condense each group of rules that use each other into one node.let sccs = components::sccs_of(&rules);let condensed = sccs.condense();let acyclic = graphs::is_dag(&condensed); // true// condensed: 0 -> [], 1 -> [], 2 -> [0, 1], 3 -> [0, 2], 4 -> [2, 3], 5 -> [4]
// Name each node of the condensed graph after the rules in it.let group_names: Vec<String> = (0..sccs.scc_count()) .map(|scc| { sccs.nodes_of(scc) .iter() .map(|&rule| names[rule as usize]) .collect::<Vec<_>>() .join("/") }) .collect();// group_names: ["name", "number", "expression/term/factor", "assignment",// "statement/block/ifStatement", "program"]
// Which groups does the group of statement use? The out-nodes of its node.let used_by_statements = condensed.out_nodes(sccs.scc_of(statement));// used_by_statements: [2, 3] - expression/term/factor and assignment// Condense each group of rules that use each other into one node.const sccs = Components.sccsOf(rules);const condensed = sccs.condense();const acyclic = Graphs.isDag(condensed); // true// condensed: 0 -> [], 1 -> [], 2 -> [0, 1], 3 -> [0, 2], 4 -> [2, 3], 5 -> [4]
// Name each node of the condensed graph after the rules in it.const groupNames = new Array<string>(sccs.sccCount);for (let scc = 0; scc < sccs.sccCount; scc++) { groupNames[scc] = Array.from(sccs.nodesOf(scc), (rule) => names[rule]).join("/");}// groupNames: [name, number, expression/term/factor, assignment,// statement/block/ifStatement, program]
// Which groups does the group of statement use? The out-nodes of its node.const usedByStatements = condensed.outNodes(sccs.sccOf(statement));// usedByStatements: [2, 3] - expression/term/factor and assignment// Condense each group of rules that use each other into one node.Sccs sccs = Components.SccsOf(rules);Graph condensed = sccs.Condense();bool acyclic = Graphs.IsDag(condensed); // true// condensed: 0 -> [], 1 -> [], 2 -> [0, 1], 3 -> [0, 2], 4 -> [2, 3], 5 -> [4]
// Name each node of the condensed graph after the rules in it.string[] groupNames = new string[sccs.SccCount];for (int scc = 0; scc < sccs.SccCount; scc++){ groupNames[scc] = string.Join("/", sccs.NodesOf(scc).Select(rule => names[rule]));}// groupNames: [name, number, expression/term/factor, assignment,// statement/block/ifStatement, program]
// Which groups does the group of statement use? The out-nodes of its node.SortedIntView usedByStatements = condensed.OutNodes(sccs.SccOf(statement));// usedByStatements: [2, 3] - expression/term/factor and assignmentThe result is the graph of the picture after. It is green as a whole, since the code returns it. It
has one node per SCC, and an edge wherever a rule of one SCC uses a rule of another. It has no
cycle. Its out-nodes say which groups a group uses: the
statements use the expressions and assignment.
Good to know
Section titled “Good to know”- For the order alone, you do not need the condensed graph. The SCC IDs are already an order in which every SCC comes after the SCCs it uses. See Put the nodes in dependency order although the graph has cycles. Since the node IDs of the condensed graph are the SCC IDs, each of its edges goes from a higher node ID to a lower one.
- Which edges are behind an edge. An edge of the condensed graph stands for edges of the
original graph. For example,
4 -> 2stands forifStatement -> expression. To find out which edges these are, useGraphs.edgePartitiongraphs::edge_partitionGraphs.edgePartitionGraphs.EdgePartition. See Reduce the data of the nodes or edges of a group. - Groups of your own. The same works for any grouping of nodes, such as the methods of a
program by package. Build the groups with
NodePartition.ofNodePartition::ofNodePartition.ofNodePartition.Of. See Condense groups of your own choice. Such a condensed graph can have cycles even where the graph has none: groups{a, c}and{b}with the edgesa -> bandb -> cgive a cycle between the two groups. The condensed graph of the SCCs never has one. - Break instead of condense. To get rid of the cycles by removing edges, see Remove edges to break every cycle.
- Built anew on every call.
NodePartition.condenseNodePartition::condenseNodePartition.condenseNodePartition.Condensedoes not keep its result. Keep it in a variable if you use it more than once.
API used
Section titled “API used”Components.sccsOf(Graph)components::sccs_ofComponents.sccsOfComponents.SccsOfNodePartition.condense()NodePartition::condenseNodePartition.condenseNodePartition.CondenseGraphs.isDag(Graph)graphs::is_dagGraphs.isDagGraphs.IsDagSccs.sccCount()Sccs::scc_countSccs.sccCountSccs.SccCountNodePartition.nodesOf(int)NodePartition::nodes_ofNodePartition.nodesOfNodePartition.NodesOfSccs.sccOf(int)Sccs::scc_ofSccs.sccOfSccs.SccOfGraph.outNodes(int)Graph::out_nodesGraph.outNodesGraph.OutNodesGraphs.edgePartition(Graph, NodePartition)graphs::edge_partitionGraphs.edgePartitionGraphs.EdgePartitionNodePartition.of(Graph, int[], int)NodePartition::ofNodePartition.ofNodePartition.Of