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).
Problem
Section titled “Problem”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.
Example
Section titled “Example”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.
- Edge on a cycle
- Back edge, left out
Solution
Section titled “Solution”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); // truelet 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); // trueconst 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); // trueEdges 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); // trueBackEdges.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.
Good to know
Section titled “Good to know”- Why it works. Every cycle contains a back edge of every depth-first search: the edge by which the search comes back to a node of the cycle that is still on its path. So without the back edges, no cycle is left. A self-loop, such as a method that calls itself, is a cycle of one edge and a back edge too.
- Which edge depends on the search. Starting at
app, the search reachesmodelbeforestorage, sostorage -> modelis the back edge. A search that reachedstoragefirst would findmodel -> storageinstead. Graphity’s search is always the same. See Classify the edges of a depth-first search for how it goes and what the other edges are. - In the order of the search.
BackEdges.ofback_edges::ofBackEdges.ofBackEdges.Oflists the back edges in the order in which the search finds them, not in the order of their edge indices. For edge index order, pass the result ofEdgeTypes.ofEdgeTypes::ofEdgeTypes.ofEdgeTypes.Ofand the back type toEdges.ofEdges::ofEdges.ofEdges.Of. You get the same edges, at the cost of classifying every edge. See Classify the edges of a depth-first search for these calls. - A builder cannot remove edges, so the example builds the graph again without the back edges. Their edge indices say which edges to leave out.
- Condense instead of break. To keep the cycles but treat each of them as a unit, see Condense each group into one node to get a DAG.
API used
Section titled “API used”BackEdges.of(Graph)back_edges::ofBackEdges.ofBackEdges.OfEdges.edgeIndices()Edges::edge_indicesEdges.edgeIndicesEdges.EdgeIndicesGraph.forEachEdge(EdgeConsumer)Graph::for_each_edgeGraph.forEachEdgeGraph.ForEachEdgeGraphBuilder()GraphBuilder::newnew GraphBuildernew GraphBuilderGraphBuilder.addNodes(int)GraphBuilder::add_nodesGraphBuilder.addNodesGraphBuilder.AddNodesGraphBuilder.addEdge(int, int)GraphBuilder::add_edgeGraphBuilder.addEdgeGraphBuilder.AddEdgeGraphBuilder.build()GraphBuilder::buildGraphBuilder.buildGraphBuilder.BuildGraphs.isDag(Graph)graphs::is_dagGraphs.isDagGraphs.IsDagEdgeTypes.of(Graph)EdgeTypes::ofEdgeTypes.ofEdgeTypes.OfEdges.of(EdgeTypes, EdgeType)Edges::ofEdges.ofEdges.Of