Skip to content

2.6 Remove edges to break every cycle

You get the back edges of a depth-first search, whose removal leaves a DAG (directed acyclic graph).

Your graph has cycles, and you need it without them. A depth-first search goes from a node along outgoing edges as deep as it can. An edge by which the search comes back to a node that is still on its current path closes a cycle. Such an edge is a back edge. Removing the back edges leaves a DAG, and the rest of the graph stays as it was. You need this for:

  • an algorithm that requires a DAG, for example to compute a topological order or to draw the graph in layers;
  • a call graph to be analyzed from the bottom up, where recursive calls are in the way;
  • a dependency graph in which the few dependencies that close a cycle should be found and cut.

The example graph dependencies models the dependencies between the packages of a small program. It has an edge from each package to each package it uses. model and storage use each other. This is the only cycle, and its two edges are orange in the picture before. Which edges can be left out so that no cycle remains? The picture after shows the packages and the back edge storage -> model, which is the one edge to leave out.

The packages with their cycle
The packages and the back edge to leave out
0: app1: cli2: ui3: service4: model5: storage6: json7: util
0: app1: cli2: ui3: service4: model5: storage6: json7: util
Context
  • Edge on a cycle
Output
  • Back edge, left out
Edges backEdges = BackEdges.of(dependencies); // storage -> model, edge 10
// Build the graph again, without the back edges.
boolean[] isBack = new boolean[dependencies.edgeCount()];
for (int edge : backEdges.edgeIndices()) {
isBack[edge] = true;
}
var builder = new GraphBuilder();
builder.addNodes(dependencies.nodeCount());
dependencies.forEachEdge((source, target, edge) -> {
if (!isBack[edge]) {
builder.addEdge(source, target);
}
});
Graph acyclic = builder.build();
boolean isDag = Graphs.isDag(acyclic); // true
Full example
let back_edges = back_edges::of(&dependencies); // storage -> model, edge 10
// Build the graph again, without the back edges.
let mut is_back = vec![false; dependencies.edge_count() as usize];
for &edge in back_edges.edge_indices() {
is_back[edge as usize] = true;
}
let mut builder = GraphBuilder::new();
builder.add_nodes(dependencies.node_count());
dependencies.for_each_edge(|source, target, edge| {
if !is_back[edge as usize] {
builder.add_edge(source, target);
}
});
let acyclic = builder.build();
let is_dag = graphs::is_dag(&acyclic); // true
Full example
const backEdges = BackEdges.of(dependencies); // storage -> model, edge 10
// Build the graph again, without the back edges.
const isBack = new Array<boolean>(dependencies.edgeCount).fill(false);
for (const edge of backEdges.edgeIndices) {
isBack[edge] = true;
}
const builder = new GraphBuilder();
builder.addNodes(dependencies.nodeCount);
dependencies.forEachEdge((source, target, edge) => {
if (!isBack[edge]) {
builder.addEdge(source, target);
}
});
const acyclic = builder.build();
const isDag = Graphs.isDag(acyclic); // true
Full example
Edges backEdges = BackEdges.Of(dependencies); // storage -> model, edge 10
// Build the graph again, without the back edges.
bool[] isBack = new bool[dependencies.EdgeCount];
foreach (int edge in backEdges.EdgeIndices)
{
isBack[edge] = true;
}
var builder = new GraphBuilder();
builder.AddNodes(dependencies.NodeCount);
dependencies.ForEachEdge((source, target, edge) =>
{
if (!isBack[edge])
{
builder.AddEdge(source, target);
}
});
Graph acyclic = builder.Build();
bool isDag = Graphs.IsDag(acyclic); // true
Full example

BackEdges.ofback_edges::ofBackEdges.ofBackEdges.Of returns the one back edge storage -> model. Its edge index is 10. The code builds the graph again without this edge. The new graph has 13 edges and no cycle, so it is a DAG and has a topological order.