Skip to content

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.

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.

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.

The packages and their dependencies
The type of each edge
edge 0edge 1edge 2edge 3edge 4edge 5edge 6edge 7edge 8edge 9edge 10edge 11edge 12edge 130: app1: cli2: ui3: service4: model5: storage6: json7: util
treetreeforwardtreetreeforwardcrosstreeforwardtreebacktreecrosscross0: app1: cli2: ui3: service4: model5: storage6: json7: util
Input
  • Edge asked about
Context
  • Start node
  • Other edge of the cycle
Output
  • Cross edge
EdgeTypes types = EdgeTypes.of(dependencies);
EdgeType storageToModel =
types.typeOf(dependencies.edgeIndexOf(storage, model)); // BACK - closes a cycle
// FORWARD - also through ui
EdgeType 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
Full example
// 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 cycle
let 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 -> util
Full example
const types = EdgeTypes.of(dependencies);
// EdgeType.BACK - closes a cycle
const storageToModel = types.typeOf(dependencies.edgeIndexOf(storage, model));
// EdgeType.FORWARD - also through ui
const 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 -> util
Full example
EdgeTypes types = EdgeTypes.Of(dependencies);
// Back - closes a cycle
EdgeType storageToModel =
types.TypeOf(dependencies.EdgeIndexOf(storage, model));
// Forward - also through ui
EdgeType 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
Full example

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