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.
Problem
Section titled “Problem”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.
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 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.
- Method asked about
- Out-node or in-node
Solution
Section titled “Solution”The out-nodes come straight from the graph:
// [3, 4, 5] - load, save and showSortedIntView calledByRun = calls.outNodes(run);int outDegree = calls.outDegree(run); // 3boolean runCallsSave = calls.hasOutNode(run, save); // trueboolean recursive = calls.containsSelfLoop(parse); // true - parse calls itselflet called_by_run = calls.out_nodes(run); // [3, 4, 5] - load, save and showlet out_degree = calls.out_degree(run); // 3let run_calls_save = calls.has_out_node(run, save); // truelet recursive = calls.contains_self_loop(parse); // true - parse calls itselfconst calledByRun = calls.outNodes(run); // [3, 4, 5] - load, save and showconst outDegree = calls.outDegree(run); // 3const runCallsSave = calls.hasOutNode(run, save); // trueconst recursive = calls.containsSelfLoop(parse); // true - parse calls itself// [3, 4, 5] - load, save and showSortedIntView calledByRun = calls.OutNodes(run);int outDegree = calls.OutDegree(run); // 3bool runCallsSave = calls.HasOutNode(run, save); // truebool recursive = calls.ContainsSelfLoop(parse); // true - parse calls itselfFor 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 formatSortedIntView callers = both.inNodes(format);int inDegree = both.inDegree(format); // 3boolean saveCallsFormat = both.hasInNode(format, save); // true// [2, 7, 9] - run, format, writeSortedIntView neighbors = both.neighbors(save);// 3 - 1 in-node, 2 out-nodesint degree = both.degree(save);boolean mainIsRoot = both.isRootNode(main); // true - nobody calls main// 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 formatlet in_degree = both.in_degree(format); // 3let save_calls_format = both.has_in_node(format, save); // truelet neighbors = both.neighbors(save); // [2, 7, 9] - run, format, writelet degree = both.degree(save); // 3 - 1 in-node, 2 out-nodeslet main_is_root = both.is_root_node(main); // true - nobody calls main// 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 formatconst callers = both.inNodes(format);const inDegree = both.inDegree(format); // 3const saveCallsFormat = both.hasInNode(format, save); // true// [2, 7, 9] - run, format, writeconst neighbors = both.neighbors(save);const degree = both.degree(save); // 3 - 1 in-node, 2 out-nodesconst mainIsRoot = both.isRootNode(main); // true - nobody calls main// 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 formatSortedIntView callers = both.InNodes(format);int inDegree = both.InDegree(format); // 3bool saveCallsFormat = both.HasInNode(format, save); // true// [2, 7, 9] - run, format, writeSortedIntView neighbors = both.Neighbors(save);// 3 - 1 in-node, 2 out-nodesint degree = both.Degree(save);// true - nobody calls mainbool mainIsRoot = both.IsRootNode(main);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.
Good to know
Section titled “Good to know”- Build the bi-adjacent graph once.
BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Ofcosts time and memory in proportion to the graph; keep it and pass it to everything that needs in-nodes. Several algorithms in this guide take aBiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraphfor that reason. It also works wherever aGraphGraphGraphGraphis expected. - A root node has no in-nodes:
BiAdjacentGraph.isRootNodeBiAdjacentGraph::is_root_nodeBiAdjacentGraph.isRootNodeBiAdjacentGraph.IsRootNodeanswers it for one node in constant time. - Sorted results. The out-nodes, the in-nodes and the neighbors come in ascending order and
without duplicates, so
Graph.hasOutNodeGraph::has_out_nodeGraph.hasOutNodeGraph.HasOutNode,BiAdjacentGraph.hasInNodeBiAdjacentGraph::has_in_nodeBiAdjacentGraph.hasInNodeBiAdjacentGraph.HasInNodeandBiAdjacentGraph.hasNeighborBiAdjacentGraph::has_neighborBiAdjacentGraph.hasNeighborBiAdjacentGraph.HasNeighborsearch them in logarithmic time. See Work with the lists Graphity returns for what else they offer. - Back to one direction. On a
BiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraph,uniAdjacentGraphuni_adjacent_graphuniAdjacentGraphUniAdjacentGraphreturns the same graph without its in-nodes; once only that graph is kept, the memory of the in-nodes can be freed. On a plainGraphGraphGraphGraph,uniAdjacentGraphuni_adjacent_graphuniAdjacentGraphUniAdjacentGraphreturns the same graph, so code that takes either kind of graph can call it.
API used
Section titled “API used”Graph.outNodes(int)Graph::out_nodesGraph.outNodesGraph.OutNodesGraph.outDegree(int)Graph::out_degreeGraph.outDegreeGraph.OutDegreeGraph.hasOutNode(int, int)Graph::has_out_nodeGraph.hasOutNodeGraph.HasOutNodeGraph.containsSelfLoop(int)Graph::contains_self_loopGraph.containsSelfLoopGraph.ContainsSelfLoopBiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfBiAdjacentGraph.inNodes(int)BiAdjacentGraph::in_nodesBiAdjacentGraph.inNodesBiAdjacentGraph.InNodesBiAdjacentGraph.inDegree(int)BiAdjacentGraph::in_degreeBiAdjacentGraph.inDegreeBiAdjacentGraph.InDegreeBiAdjacentGraph.hasInNode(int, int)BiAdjacentGraph::has_in_nodeBiAdjacentGraph.hasInNodeBiAdjacentGraph.HasInNodeBiAdjacentGraph.neighbors(int)BiAdjacentGraph::neighborsBiAdjacentGraph.neighborsBiAdjacentGraph.NeighborsBiAdjacentGraph.hasNeighbor(int, int)BiAdjacentGraph::has_neighborBiAdjacentGraph.hasNeighborBiAdjacentGraph.HasNeighborBiAdjacentGraph.degree(int)BiAdjacentGraph::degreeBiAdjacentGraph.degreeBiAdjacentGraph.DegreeBiAdjacentGraph.isRootNode(int)BiAdjacentGraph::is_root_nodeBiAdjacentGraph.isRootNodeBiAdjacentGraph.IsRootNodeBiAdjacentGraph.uniAdjacentGraph()Graph::uni_adjacent_graphBiAdjacentGraph.uniAdjacentGraphBiAdjacentGraph.UniAdjacentGraphGraph.uniAdjacentGraph()Graph::uni_adjacent_graphGraph.uniAdjacentGraphGraph.UniAdjacentGraph