2.5 Classify the edges of a depth-first search
You sort the edges of a graph into tree, back, forward and cross edges, which tells you which edges close a cycle and where the others lead.
Problem
Section titled “Problem”You want to know, for each edge of a graph, what role it plays in a depth-first search. Such a search goes from a node along outgoing edges as deep as it can before it turns back. It meets every edge once and gives it one of four types, by where the edge leads:
- A tree edge leads to a node the search had not seen yet. Together, the tree edges form a tree from the start node. If the search needs several start nodes to reach every node, the tree edges form a forest.
- A back edge leads back to a node on the current path and closes a cycle.
- A forward edge leads to a node that the search has already reached from the edge’s source along one or more tree edges.
- A cross edge leads to a node that the search has already finished and that is neither an ancestor nor a descendant of the edge’s source in the tree of the search.
You need this for:
- dependencies between packages or modules, to find the edges that close a cycle: a graph has a cycle exactly when it has a back edge;
- the control-flow graph of a function, to find the edges that lead back to a loop header. The search starts at node 0, so that node must be the entry. If every loop is also entered only through its header, these edges are the back edges;
- a search of your own that treats each edge by its type.
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. The picture before shows the edge
index of each edge. Which type is each edge, for a search that starts
at app and takes the out-nodes of each node in ascending
order? The code asks about storage -> model and app -> service, and collects the cross edges.
The picture after shows the type of each edge. The start node app is purple. The orange edge
model -> storage is the other edge of the cycle of model and storage.
- Edge asked about
- Start node
- Other edge of the cycle
- Cross edge
Solution
Section titled “Solution”EdgeTypes types = EdgeTypes.of(dependencies);EdgeType storageToModel = types.typeOf(dependencies.edgeIndexOf(storage, model)); // BACK - closes a cycle// FORWARD - also through uiEdgeType appToService = types.typeOf(dependencies.edgeIndexOf(app, service));
// All edges of one type, as an edge list.Edges crossEdges = Edges.of(types, EdgeType.CROSS);// crossEdges: ui -> util, storage -> util, json -> util// edge_index_of returns None if there is no such edge.let types = EdgeTypes::of(&dependencies);let storage_to_model = types.type_of(dependencies.edge_index_of(storage, model).unwrap());// storage_to_model: Back - closes a cyclelet app_to_service = types.type_of(dependencies.edge_index_of(app, service).unwrap());// app_to_service: Forward - also through ui
// All edges of one type, as an edge list.let cross_edges = Edges::of(&types, EdgeType::Cross);// cross_edges: ui -> util, storage -> util, json -> utilconst types = EdgeTypes.of(dependencies);// EdgeType.BACK - closes a cycleconst storageToModel = types.typeOf(dependencies.edgeIndexOf(storage, model));// EdgeType.FORWARD - also through uiconst appToService = types.typeOf(dependencies.edgeIndexOf(app, service));
// All edges of one type, as an edge list.const crossEdges = Edges.of(types, EdgeType.CROSS);// crossEdges: ui -> util, storage -> util, json -> utilEdgeTypes types = EdgeTypes.Of(dependencies);// Back - closes a cycleEdgeType storageToModel = types.TypeOf(dependencies.EdgeIndexOf(storage, model));// Forward - also through uiEdgeType appToService = types.TypeOf(dependencies.EdgeIndexOf(app, service));
// All edges of one type, as an edge list.Edges crossEdges = Edges.Of(types, EdgeType.Cross);// crossEdges: ui -> util, storage -> util, json -> utilstorage -> model is the only back edge. The search reaches model before storage, so this edge
leads back to model while model is still on the path. The edge closes the cycle.
app -> service is a forward edge, because the search reaches service through ui first. Three
cross edges lead to util, which the search had already finished from cli.
Edges.ofEdges::ofEdges.ofEdges.Of returns these three as an edge list.
Good to know
Section titled “Good to know”- The types depend on the search. Another start node or another order of the out-nodes gives other types. One thing is the same for every search: there is no back edge exactly when the graph is a DAG (directed acyclic graph). Graphity’s search always starts at the nodes in ascending order and takes the out-nodes of each node in ascending order, so a graph always gets the same types.
- Edges that other paths make redundant. For every forward edge, a path of tree edges also
leads from its source to its target. A tree edge or a cross edge can have another path as well,
such as
service -> model(throughstorage) andui -> util(throughserviceandstorage).Graphs.transitiveReductiongraphs::transitive_reductionGraphs.transitiveReductionGraphs.TransitiveReductionremoves all such edges of a DAG. See Remove edges that other paths make redundant. - Only the back edges?
BackEdges.ofback_edges::ofBackEdges.ofBackEdges.Offinds them without classifying the others. See Remove edges to break every cycle. - Edge lists.
Edges.ofEdges::ofEdges.ofEdges.Ofreturns an edge list. See Work with the lists Graphity returns for what it offers. - During a search of your own. See React to every edge of a depth-first search for a search that sees each edge as it runs.