4 Reachability and distances
This chapter answers which nodes a node reaches, which nodes reach it, and how many edges lie in between.
A path of edges leads from a node to the nodes it reaches. A method reaches all the methods it may end up calling, and a package reaches everything it depends on.
Problems in this chapter
Section titled “Problems in this chapter”- 4.1 Find everything a node reachesYou get the nodes that a path of edges leads to from one node or from several.
- 4.2 Find everything that reaches a nodeYou get the nodes from which a path of edges leads to a node - the callers of callers, the users of users.
- 4.3 Find a shortest path between two nodesYou get the distance from a node to every other node, and a path with the fewest edges.
- 4.4 Find the distances from several start nodes, and the largest distanceYou get the distance of each node from the nearest of several start nodes, and the largest distance a search reaches.
- 4.5 Answer many reachability questions at onceYou build the transitive closure, which is a graph with an edge wherever a path leads.
- 4.6 Remove edges that other paths make redundantYou get the transitive reduction of a DAG (directed acyclic graph), in which every node reaches the same nodes as before, with as few edges as possible.
The examples of this chapter use a small program: the calls between its methods, and the steps that build and publish it.
Reach and distance
Section titled “Reach and distance”A node reaches another node if a path of edges leads from the one to the other. Every node reaches
itself. The distance from one node to another is the number of edges on a
shortest such path. Reach and distance both follow the edges in their direction: main reaches
write, but write does not reach main.
Here, main reaches every method, and the largest distance is 3. run reaches all methods but
main and parseArgs. read and write reach nothing but themselves. To ask which methods may
end up calling format, you follow the edges against their direction. For that, search the
transposed graph, in which every edge is turned around.
Searches from start nodes
Section titled “Searches from start nodes”A search starts at one node or at several. These are its start nodes, which Graphity calls sources. From there, the search follows the edges. A breadth-first search visits the nodes in the order of their distance from the nearest start node, so it finds the distances and a shortest path to every node it reaches.
All reachability at once
Section titled “All reachability at once”The transitive closure of a graph has an edge
u -> v wherever a path of one or more edges leads from u to v. In the calls above, it also has
main -> write. It answers at once every question “does u reach v?” for two different nodes.
The transitive reduction goes the other way. In a DAG (directed acyclic graph),
it leaves out every edge u -> v for which a longer path also leads from u to v. It keeps the
fewest edges with which every node still reaches the same nodes.