Skip to content

4.2 Find everything that reaches a node

You get the nodes from which a path of edges leads to a node - the callers of callers, the users of users.

You have a node and want to know everything that can lead to it: every node from which a path of edges leads to it. These are not the nodes it reaches, but the nodes that reach it. Such questions come up for:

  • the impact of a change: every method that may end up calling a changed method may behave differently;
  • the removal of a package: every package that depends on it, directly or not, breaks;
  • the causes of a state: every state from which a protocol can get into it.

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. format changes: which methods may end up calling it? The picture after shows them in green. format itself is one of them.

The calls and the start node
The methods that reach format
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
Input
  • Start node
Output
  • Method that reaches format

The transposed graph has the same nodes, with every edge turned around. What reaches a node in the graph is what the node reaches in the transposed graph. For that, a plain graph needs its in-nodes, which BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of adds. BiAdjacentGraph.transposedBiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.Transposed then turns every edge around:

// Turn every call around, to search from format to the methods that may end up
// calling it.
BiAdjacentGraph calledBy = BiAdjacentGraph.of(calls).transposed();
SortedIntView affected = Graphs.reachableFrom(calledBy, format);
// affected: [0, 2, 4, 5, 7] - main, run, save, show and format itself
Full example
// Turn every call around, to search from format to the methods that may end up
// calling it.
let called_by = BiAdjacentGraph::of(&calls).transposed();
let affected = graphs::reachable_from(&called_by, format);
// affected: [0, 2, 4, 5, 7] - main, run, save, show and format itself
Full example
// Turn every call around, to search from format to the methods that may end up
// calling it.
const calledBy = BiAdjacentGraph.of(calls).transposed();
const affected = Graphs.reachableFrom(calledBy, format);
// affected: [0, 2, 4, 5, 7] - main, run, save, show and format itself
Full example
// Turn every call around, to search from format to the methods that may end up
// calling it.
BiAdjacentGraph calledBy = BiAdjacentGraph.Of(calls).Transposed();
SortedIntView affected = Graphs.ReachableFrom(calledBy, format);
// affected: [0, 2, 4, 5, 7] - main, run, save, show and format itself
Full example

main, run, save and show may end up calling format. format itself is in the result too, as a search always includes its start node.