Skip to content

1.6 Find the out-nodes and in-nodes of a node

You read the nodes that a node has edges to, the nodes that have edges to it, and both together.

You have a node and want the nodes directly connected to it. Its out-nodes are the targets of its outgoing edges. Its in-nodes are the sources of its incoming edges. A graph stores the out-nodes of every node; the in-nodes have to be built once. Such questions come up for:

  • the methods a method calls, and the methods that call it;
  • the packages a package uses, and the packages that use it;
  • the tasks that wait for a task, and the tasks it waits for.

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 does run call, and which methods call format? The picture after shows both answers in green: the methods that run calls, and the methods that call format - among them format itself, which calls itself.

The calls and the methods run and format
The out-nodes of run and the in-nodes of format
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
  • Out-node or in-node

The out-nodes come straight from the graph:

// [3, 4, 5] - load, save and show
SortedIntView calledByRun = calls.outNodes(run);
int outDegree = calls.outDegree(run); // 3
boolean runCallsSave = calls.hasOutNode(run, save); // true
boolean recursive = calls.containsSelfLoop(parse); // true - parse calls itself
Full example
let called_by_run = calls.out_nodes(run); // [3, 4, 5] - load, save and show
let out_degree = calls.out_degree(run); // 3
let run_calls_save = calls.has_out_node(run, save); // true
let recursive = calls.contains_self_loop(parse); // true - parse calls itself
Full example
const calledByRun = calls.outNodes(run); // [3, 4, 5] - load, save and show
const outDegree = calls.outDegree(run); // 3
const runCallsSave = calls.hasOutNode(run, save); // true
const recursive = calls.containsSelfLoop(parse); // true - parse calls itself
Full example
// [3, 4, 5] - load, save and show
SortedIntView calledByRun = calls.OutNodes(run);
int outDegree = calls.OutDegree(run); // 3
bool runCallsSave = calls.HasOutNode(run, save); // true
bool recursive = calls.ContainsSelfLoop(parse); // true - parse calls itself
Full example

For the in-nodes, BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of builds a graph that stores the in-nodes of every node as well:

// A Graph stores the out-nodes of every node; BiAdjacentGraph adds the in-nodes.
// Build it once and keep it.
BiAdjacentGraph both = BiAdjacentGraph.of(calls);
// [4, 5, 7] - save, show and format
SortedIntView callers = both.inNodes(format);
int inDegree = both.inDegree(format); // 3
boolean saveCallsFormat = both.hasInNode(format, save); // true
// [2, 7, 9] - run, format, write
SortedIntView neighbors = both.neighbors(save);
// 3 - 1 in-node, 2 out-nodes
int degree = both.degree(save);
boolean mainIsRoot = both.isRootNode(main); // true - nobody calls main
Full example
// A Graph stores the out-nodes of every node; BiAdjacentGraph adds the in-nodes.
// Build it once and keep it.
let both = BiAdjacentGraph::of(&calls);
let callers = both.in_nodes(format); // [4, 5, 7] - save, show and format
let in_degree = both.in_degree(format); // 3
let save_calls_format = both.has_in_node(format, save); // true
let neighbors = both.neighbors(save); // [2, 7, 9] - run, format, write
let degree = both.degree(save); // 3 - 1 in-node, 2 out-nodes
let main_is_root = both.is_root_node(main); // true - nobody calls main
Full example
// A Graph stores the out-nodes of every node; BiAdjacentGraph adds the in-nodes.
// Build it once and keep it.
const both = BiAdjacentGraph.of(calls);
// [4, 5, 7] - save, show and format
const callers = both.inNodes(format);
const inDegree = both.inDegree(format); // 3
const saveCallsFormat = both.hasInNode(format, save); // true
// [2, 7, 9] - run, format, write
const neighbors = both.neighbors(save);
const degree = both.degree(save); // 3 - 1 in-node, 2 out-nodes
const mainIsRoot = both.isRootNode(main); // true - nobody calls main
Full example
// A Graph stores the out-nodes of every node; BiAdjacentGraph adds the in-nodes.
// Build it once and keep it.
BiAdjacentGraph both = BiAdjacentGraph.Of(calls);
// [4, 5, 7] - save, show and format
SortedIntView callers = both.InNodes(format);
int inDegree = both.InDegree(format); // 3
bool saveCallsFormat = both.HasInNode(format, save); // true
// [2, 7, 9] - run, format, write
SortedIntView neighbors = both.Neighbors(save);
// 3 - 1 in-node, 2 out-nodes
int degree = both.Degree(save);
// true - nobody calls main
bool mainIsRoot = both.IsRootNode(main);
Full example

run calls load, save and show; format is called by save, show and itself. The neighbors of a node are its in-nodes and its out-nodes together: save has the in-node run and the out-nodes format and write. Its degree is 3. The degree counts edges, not neighbors, and here the two numbers agree only by chance: format has 3 neighbors but the degree 4, since its self-loop counts once as an incoming edge and once as an outgoing edge.