Skip to content

5.1 Find the components of a graph read as undirected

You get the weakly connected components of a graph: the groups of nodes that paths connect when the direction of the edges does not matter.

You want to know which nodes of a graph are connected at all, whatever the direction of the edges, or just whether everything is connected. Reading the edges without their direction is called the undirected reading. In this reading, a group of nodes that are connected by paths, as large as it can be, is called a component. No path connects nodes in different components. You need this to find:

  • modules of a codebase that no reference connects, and that could be split off;
  • sub-problems that do not depend on each other, and can be solved separately;
  • networks with no link between them.

The example graph calls models the calls between the methods of a small program. It has an edge from each method to each method it calls. The array names holds the names of the methods. main and run call into every area of the program. The picture before shows the calls they make as dashed edges. Without these calls, which methods are still connected by calls?

The calls kept, and those of main and run left out
The components that remain
0: main1: parseArgs2: run3: load6: parse8: read4: save5: show7: format9: write
0: main1: parseArgs2: run3: load6: parse8: read4: save5: show7: format9: writecomponent 4component 3component 2component 1component 0
Context
  • Call left out
Output
  • Component

The picture after shows the five components that remain, each in a frame.

Build the graph without the calls of main and run, then find its components:

// Leave out the calls of main and run, which call into every area of the program.
var builder = new GraphBuilder();
builder.addNodes(calls.nodeCount());
calls.forEachEdge((caller, callee, edge) -> {
if (caller != main && caller != run) {
builder.addEdge(caller, callee);
}
});
Graph rest = builder.build();
Full example
// Leave out the calls of main and run, which call into every area of the program.
let mut builder = GraphBuilder::new();
builder.add_nodes(calls.node_count());
calls.for_each_edge(|caller, callee, _edge| {
if caller != main && caller != run {
builder.add_edge(caller, callee);
}
});
let rest = builder.build();
Full example
// Leave out the calls of main and run, which call into every area of the program.
const builder = new GraphBuilder();
builder.addNodes(calls.nodeCount);
calls.forEachEdge((caller, callee) => {
if (caller !== main && caller !== run) {
builder.addEdge(caller, callee);
}
});
const rest = builder.build();
Full example
// Leave out the calls of main and run, which call into every area of the program.
var builder = new GraphBuilder();
builder.AddNodes(calls.NodeCount);
calls.ForEachEdge((caller, callee, _) =>
{
if (caller != main && caller != run)
{
builder.AddEdge(caller, callee);
}
});
Graph rest = builder.Build();
Full example
NodePartition components = Components.wccsOf(rest);
int componentCount = components.groupCount(); // 5
List<List<String>> byName = new ArrayList<>();
for (int component = 0; component < componentCount; component++) {
byName.add(
components.nodesOf(component).intStream().mapToObj(m -> names[m]).toList());
}
// byName: [[main], [parseArgs], [run], [load, parse, read],
// [save, show, format, write]]
boolean callsConnected = Graphs.isWeaklyConnected(calls); // true
boolean restConnected = Graphs.isWeaklyConnected(rest); // false
Full example
let components = components::wccs_of(&rest);
let component_count = components.group_count(); // 5
let by_name: Vec<Vec<&str>> = (0..component_count)
.map(|component| {
components
.nodes_of(component)
.iter()
.map(|&m| names[m as usize])
.collect()
})
.collect();
// by_name: [["main"], ["parseArgs"], ["run"], ["load", "parse", "read"],
// ["save", "show", "format", "write"]]
let calls_connected = graphs::is_weakly_connected(&calls); // true
let rest_connected = graphs::is_weakly_connected(&rest); // false
Full example
const components = Components.wccsOf(rest);
const componentCount = components.groupCount; // 5
const byName: string[][] = [];
for (let component = 0; component < componentCount; component++) {
byName.push(Array.from(components.nodesOf(component), (m) => names[m]));
}
// byName: [[main], [parseArgs], [run], [load, parse, read],
// [save, show, format, write]]
const callsConnected = Graphs.isWeaklyConnected(calls); // true
const restConnected = Graphs.isWeaklyConnected(rest); // false
Full example
NodePartition components = Components.WccsOf(rest);
int componentCount = components.GroupCount; // 5
List<List<string>> byName = [];
for (int component = 0; component < componentCount; component++)
{
byName.Add([.. components.NodesOf(component).Select(m => names[m])]);
}
// byName: [[main], [parseArgs], [run], [load, parse, read],
// [save, show, format, write]]
bool callsConnected = Graphs.IsWeaklyConnected(calls); // true
bool restConnected = Graphs.IsWeaklyConnected(rest); // false
Full example

Components.wccsOfcomponents::wccs_ofComponents.wccsOfComponents.WccsOf returns a NodePartitionNodePartitionNodePartitionNodePartition with five groups. One group holds load, parse and read. One group holds save, show, format and write. Each of main, parseArgs and run is a group of its own. All the calls together are one component. The calls without those of main and run are not.