4.1 Find everything a node reaches
You get the nodes that a path of edges leads to from one node or from several.
Problem
Section titled “Problem”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.
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. Which methods may run when load runs? The picture after
shows in green the methods that load reaches. load itself is one of them.
Input
- Start node
Output
- Reached method
Solution
Section titled “Solution”SortedIntView fromLoad = Graphs.reachableFrom(calls, load);// fromLoad: [3, 6, 8] - load, parse and readSortedIntView fromLoadOrSave = Graphs.reachableFrom(calls, IntList.of(load, save));// fromLoadOrSave: [3, 4, 6, 7, 8, 9] - load, save, parse, format, read and writelet from_load = graphs::reachable_from(&calls, load);// from_load: [3, 6, 8] - load, parse and readlet 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 writeconst fromLoad = Graphs.reachableFrom(calls, load);// fromLoad: [3, 6, 8] - load, parse and readconst fromLoadOrSave = Graphs.reachableFrom(calls, [load, save]);// fromLoadOrSave: [3, 4, 6, 7, 8, 9] - load, save, parse, format, read and writeSortedIntView fromLoad = Graphs.ReachableFrom(calls, load);// fromLoad: [3, 6, 8] - load, parse and readSortedIntView fromLoadOrSave = Graphs.ReachableFrom(calls, [load, save]);// fromLoadOrSave: [3, 4, 6, 7, 8, 9] - load, save, parse, format, read and writeload reaches parse, read and itself. From load and save together, the search reaches what
either of them reaches: save adds itself, format and write.
Good to know
Section titled “Good to know”- Sorted and complete. The result is a sorted list of node IDs, and the start nodes are in it. See Work with the lists Graphity returns for how to combine it with other lists of nodes.
- Start nodes. Graphity calls the start nodes sources. A start node given twice counts once. Without start nodes, the result is empty.
- The other direction. A search can also follow the edges against their direction. See Find everything that reaches a node.
- A subgraph of what is reached. To keep working on just these nodes, take the subgraph of them. See Take the subgraph of some nodes.
- How far, and along which path?
BfsDistances.ofBfsDistances::ofBfsDistances.ofBfsDistances.Ofalso gives the distances. See Find a shortest path between two nodes. - Only within a few steps, or not through some nodes? A search of your own can stop at a level or follow only the nodes you choose. See Write a breadth-first search.