4.5 Answer many reachability questions at once
You build the transitive closure, which is a graph with an edge wherever a path leads.
Problem
Section titled “Problem”You will ask “does this node reach that one?” many times, for many pairs of nodes. A new search for each question repeats work, so you want every answer at hand. You need this for:
- a check of every dependency in a codebase against the rules of its architecture;
- the access rights that follow from a hierarchy of roles;
- the questions asked again and again of a hierarchy of categories or types.
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 method may end up calling which? The picture after
shows the closure. It has an edge from each method to each method it may end up calling, and the new
edges are green.
- Method asked about
- Edge the closure adds
Solution
Section titled “Solution”Graph closure = Graphs.transitiveClosure(calls);// 23 - 12 calls and 11 new edgesint edgeCount = closure.edgeCount();boolean mainReachesWrite = closure.hasOutNode(main, write); // trueSortedIntView fromRun = closure.outNodes(run);// fromRun: [3, 4, 5, 6, 7, 8, 9] - load, save, show, parse, format, read and write
// true - parse calls itselfboolean parseOnCycle = closure.containsSelfLoop(parse);boolean loadOnCycle = closure.containsSelfLoop(load); // falselet closure = graphs::transitive_closure(&calls);let edge_count = closure.edge_count(); // 23 - 12 calls and 11 new edgeslet main_reaches_write = closure.has_out_node(main, write); // truelet from_run = closure.out_nodes(run);// from_run: [3, 4, 5, 6, 7, 8, 9] - load, save, show, parse, format, read and write
let parse_on_cycle = closure.contains_self_loop(parse); // true - parse calls itselflet load_on_cycle = closure.contains_self_loop(load); // falseconst closure = Graphs.transitiveClosure(calls);// 23 - 12 calls and 11 new edgesconst edgeCount = closure.edgeCount;const mainReachesWrite = closure.hasOutNode(main, write); // trueconst fromRun = closure.outNodes(run);// fromRun: [3, 4, 5, 6, 7, 8, 9] - load, save, show, parse, format, read and write
// true - parse calls itselfconst parseOnCycle = closure.containsSelfLoop(parse);const loadOnCycle = closure.containsSelfLoop(load); // falseGraph closure = Graphs.TransitiveClosure(calls);// 23 - 12 calls and 11 new edgesint edgeCount = closure.EdgeCount;bool mainReachesWrite = closure.HasOutNode(main, write); // trueSortedIntView fromRun = closure.OutNodes(run);// fromRun: [3, 4, 5, 6, 7, 8, 9] - load, save, show, parse, format, read and// write
// true - parse calls itselfbool parseOnCycle = closure.ContainsSelfLoop(parse);bool loadOnCycle = closure.ContainsSelfLoop(load); // falseThe closure is the graph of the picture after, with 23 edges instead of 12. It is a graph like any
other. In this graph, the out-nodes of a node are the nodes
it reaches along one or more edges, and Graph.hasOutNodeGraph::has_out_nodeGraph.hasOutNodeGraph.HasOutNode answers whether one
node reaches another. A node lies on a cycle exactly if it has a
self-loop in the closure. parse has one, since it calls itself, and
load does not.
Good to know
Section titled “Good to know”- A node and itself. Unlike a search, the closure has the edge
u -> uonly whenulies on a cycle: a path of one or more edges leads from it back to itself. Every node reaches itself, so ask the closure only about two different nodes. - What reaches a node is its in-nodes in the closure. To read them, call
BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Ofon the closure and thenBiAdjacentGraph.inNodesBiAdjacentGraph::in_nodesBiAdjacentGraph.inNodesBiAdjacentGraph.InNodes. See Find the out-nodes and in-nodes of a node. - Size. The closure can have up to as many edges as the square of the node count. For the
nodes reached from a few nodes,
Graphs.reachableFromgraphs::reachable_from_sourcesGraphs.reachableFromGraphs.ReachableFromcosts far less. See Find everything a node reaches. - The other way. The closure has an edge
u -> vwherever a path leads fromutov.Graphs.transitiveReductiongraphs::transitive_reductionGraphs.transitiveReductionGraphs.TransitiveReductionkeeps as few edges as possible. See Remove edges that other paths make redundant.
API used
Section titled “API used”Graphs.transitiveClosure(Graph)graphs::transitive_closureGraphs.transitiveClosureGraphs.TransitiveClosureGraph.hasOutNode(int, int)Graph::has_out_nodeGraph.hasOutNodeGraph.HasOutNodeGraph.outNodes(int)Graph::out_nodesGraph.outNodesGraph.OutNodesGraph.containsSelfLoop(int)Graph::contains_self_loopGraph.containsSelfLoopGraph.ContainsSelfLoopBiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfBiAdjacentGraph.inNodes(int)BiAdjacentGraph::in_nodesBiAdjacentGraph.inNodesBiAdjacentGraph.InNodes