Skip to content

4.1 Find everything a node reaches

You get the nodes that a path of edges leads to from one node or from several.

Which nodes can you get to from a node by following edges in their direction? The answer is a set of nodes. You can ask for one start node or for several. Such questions come up for:

  • the code that may run when a method runs: every method it may end up calling;
  • the packages that have to be present to use a package: everything it depends on, directly or not;
  • the states a protocol can still get into from the state it is in.

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. Which methods may run when load runs? The picture after shows in green the methods that load reaches. load itself is one of them.

The calls and the start node
The methods that load reaches
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
  • Reached method
SortedIntView fromLoad = Graphs.reachableFrom(calls, load);
// fromLoad: [3, 6, 8] - load, parse and read
SortedIntView fromLoadOrSave = Graphs.reachableFrom(calls, IntList.of(load, save));
// fromLoadOrSave: [3, 4, 6, 7, 8, 9] - load, save, parse, format, read and write
Full example
let from_load = graphs::reachable_from(&calls, load);
// from_load: [3, 6, 8] - load, parse and read
let from_load_or_save = graphs::reachable_from_sources(&calls, &[load, save]);
// from_load_or_save: [3, 4, 6, 7, 8, 9] - load, save, parse, format, read and write
Full example
const fromLoad = Graphs.reachableFrom(calls, load);
// fromLoad: [3, 6, 8] - load, parse and read
const fromLoadOrSave = Graphs.reachableFrom(calls, [load, save]);
// fromLoadOrSave: [3, 4, 6, 7, 8, 9] - load, save, parse, format, read and write
Full example
SortedIntView fromLoad = Graphs.ReachableFrom(calls, load);
// fromLoad: [3, 6, 8] - load, parse and read
SortedIntView fromLoadOrSave = Graphs.ReachableFrom(calls, [load, save]);
// fromLoadOrSave: [3, 4, 6, 7, 8, 9] - load, save, parse, format, read and write
Full example

load reaches parse, read and itself. From load and save together, the search reaches what either of them reaches: save adds itself, format and write.