Skip to content

4.5 Answer many reachability questions at once

You build the transitive closure, which is a graph with an edge wherever a path leads.

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.

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.

The calls and the methods asked about
The transitive closure
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
Input
  • Method asked about
Output
  • Edge the closure adds
Graph closure = Graphs.transitiveClosure(calls);
// 23 - 12 calls and 11 new edges
int edgeCount = closure.edgeCount();
boolean mainReachesWrite = closure.hasOutNode(main, write); // true
SortedIntView fromRun = closure.outNodes(run);
// fromRun: [3, 4, 5, 6, 7, 8, 9] - load, save, show, parse, format, read and write
// true - parse calls itself
boolean parseOnCycle = closure.containsSelfLoop(parse);
boolean loadOnCycle = closure.containsSelfLoop(load); // false
Full example
let closure = graphs::transitive_closure(&calls);
let edge_count = closure.edge_count(); // 23 - 12 calls and 11 new edges
let main_reaches_write = closure.has_out_node(main, write); // true
let 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 itself
let load_on_cycle = closure.contains_self_loop(load); // false
Full example
const closure = Graphs.transitiveClosure(calls);
// 23 - 12 calls and 11 new edges
const edgeCount = closure.edgeCount;
const mainReachesWrite = closure.hasOutNode(main, write); // true
const fromRun = closure.outNodes(run);
// fromRun: [3, 4, 5, 6, 7, 8, 9] - load, save, show, parse, format, read and write
// true - parse calls itself
const parseOnCycle = closure.containsSelfLoop(parse);
const loadOnCycle = closure.containsSelfLoop(load); // false
Full example
Graph closure = Graphs.TransitiveClosure(calls);
// 23 - 12 calls and 11 new edges
int edgeCount = closure.EdgeCount;
bool mainReachesWrite = closure.HasOutNode(main, write); // true
SortedIntView fromRun = closure.OutNodes(run);
// fromRun: [3, 4, 5, 6, 7, 8, 9] - load, save, show, parse, format, read and
// write
// true - parse calls itself
bool parseOnCycle = closure.ContainsSelfLoop(parse);
bool loadOnCycle = closure.ContainsSelfLoop(load); // false
Full example

The 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.