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.
Problem
Section titled “Problem”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.
Example
Section titled “Example”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.
- Start node
- Method that reaches format
Solution
Section titled “Solution”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// 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// 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// 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 itselfmain, run, save and show may end up calling format. format itself is in the result too,
as a search always includes its start node.
Good to know
Section titled “Good to know”- What it costs.
BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Ofbuilds the in-nodes once, in time and memory in proportion to the graph.BiAdjacentGraph.transposedBiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.Transposedthen costs nothing. See Turn every edge around for the reason. Keep theBiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraphif you search in both directions. - Only the direct callers?
BiAdjacentGraph.inNodesBiAdjacentGraph::in_nodesBiAdjacentGraph.inNodesBiAdjacentGraph.InNodesreturns the in-nodes of a node, without a search. - The other direction. On the graph itself,
Graphs.reachableFromgraphs::reachable_fromGraphs.reachableFromGraphs.ReachableFromfollows the edges in their direction, from one start node or from several. See Find everything a node reaches. On the transposed graph, several start nodes work the same way. - Every search works backwards this way. Shortest paths, distances and the other searches of this guide take the transposed graph like any other graph.
API used
Section titled “API used”BiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfBiAdjacentGraph.transposed()BiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.TransposedGraphs.reachableFrom(Graph, int)graphs::reachable_fromGraphs.reachableFromGraphs.ReachableFrom(Graph, int)BiAdjacentGraph.inNodes(int)BiAdjacentGraph::in_nodesBiAdjacentGraph.inNodesBiAdjacentGraph.InNodes