Skip to content

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.

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.

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:

The rules and the SCCs with a cycle
The condensed graph
0: program1: statement2: block3: ifStatement4: assignment5: expression6: term7: factor8: name9: numberSCC 2SCC 4
5: program4: statement/block/ifStatement3: assignment2: expression/term/factor0: name1: number
Input
  • Rule asked about
Context
  • SCC with a cycle
  • Edge on a cycle
Output
  • 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.

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
Full example
// 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
Full example
// 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
Full example
// 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 assignment
Full example

The 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.