Skip to content

1.7 Work with the lists Graphity returns

You work with sorted lists of nodes, and with lists of edges with their sources, targets and edge indices.

You have a list from Graphity and want to read it, combine it with another list and look things up in it, without copying it into an array first. Most answers of Graphity are lists. They come from:

  • the graph itself, such as the out-nodes of a node;
  • searches, such as the nodes that a node reaches;
  • analyses that return edges, such as the back edges of a search or the bridges of a graph.

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. The array names holds the names of the methods. The two edges from a method to itself are labeled with their edge indices. How many methods does run call, and is save among them? Which methods do save or show call? And which calls are recursive, as a list of edges with their edge indices? The picture after shows in green the methods that save or show call, and the recursive calls:

The calls and the methods save and show
The out-nodes of save or show, and the recursive calls
edge 10edge 110: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
edge 10edge 110: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
Input
  • Method asked about
Output
  • Out-node of save or show
  • Recursive call

The out-nodes of a node, like most sets of nodes that Graphity returns, are a SortedIntViewSortedIntViewSortedIntViewSortedIntView. They come in ascending order and without duplicates, so looking up a node is a binary search, and two lists combine in one pass:

SortedIntView calledByRun = calls.outNodes(run); // [3, 4, 5] - load, save and show
int size = calledByRun.size(); // 3
int first = calledByRun.first(); // 3
int last = calledByRun.last(); // 5
int position = calledByRun.indexOf(save); // 1 - found by binary search
boolean hasFormat = calledByRun.contains(format); // false
// Out-nodes come in ascending order and without duplicates, so they combine cheaply:
// the methods that save or show call.
SortedIntView both = calls.outNodes(save).union(calls.outNodes(show));
// both: [7, 9] - format and write
// [3, 4, 5] - a copy that may be changed
int[] copy = calledByRun.toIntArray();
Full example
// A SortedIntView dereferences to a slice, so the methods of slices work on it too.
let called_by_run = calls.out_nodes(run); // [3, 4, 5] - load, save and show
let size = called_by_run.len(); // 3
let first = called_by_run.first(); // Some(3)
let last = called_by_run.last(); // Some(5)
let position = called_by_run.index_of(save); // Some(1) - found by binary search
let has_format = called_by_run.contains(format); // false
// Out-nodes come in ascending order and without duplicates, so they combine cheaply:
// the methods that save or show call.
let (called_by_save, called_by_show) = (calls.out_nodes(save), calls.out_nodes(show));
let both = called_by_save.union(&called_by_show);
// both: [7, 9] - format and write
let copy = called_by_run.to_vec(); // [3, 4, 5] - a copy that may be changed
Full example
const calledByRun = calls.outNodes(run); // [3, 4, 5] - load, save and show
const size = calledByRun.size; // 3
const first = calledByRun.first(); // 3
const last = calledByRun.last(); // 5
const position = calledByRun.indexOf(save); // 1 - found by binary search
const hasFormat = calledByRun.contains(format); // false
// Out-nodes come in ascending order and without duplicates, so they combine cheaply:
// the methods that save or show call.
const both = calls.outNodes(save).union(calls.outNodes(show));
// both: [7, 9] - format and write
// [3, 4, 5] - a copy that may be changed
const copy = calledByRun.toInt32Array();
Full example
// [3, 4, 5] - load, save and show
SortedIntView calledByRun = calls.OutNodes(run);
int count = calledByRun.Count; // 3
int first = calledByRun.First(); // 3
int last = calledByRun.Last(); // 5
int position = calledByRun.IndexOf(save); // 1 - found by binary search
bool hasFormat = calledByRun.Contains(format); // false
// Out-nodes come in ascending order and without duplicates, so they combine
// cheaply: the methods that save or show call.
SortedIntView both = calls.OutNodes(save).Union(calls.OutNodes(show));
// both: [7, 9] - format and write
// [3, 4, 5] - a copy that may be changed
int[] copy = calledByRun.ToArray();
Full example

run calls three methods. save is among them, at position 1. SortedIntView.unionSortedIntView::unionSortedIntView.unionSortedIntView.Union returns the methods that save or show call: format and write. They are the green nodes of the picture after.

An algorithm that returns edges returns an EdgesEdgesEdgesEdges, which holds three lists of the same length. Edges.countEdges::countEdges.countEdges.Count is their length, and Edges.isEmptyEdges::is_emptyEdges.isEmptyEdges.IsEmpty tells whether there is no edge. Here, BackEdges.ofback_edges::ofBackEdges.ofBackEdges.Of returns the back edges of a search. These are the edges that close a cycle. See Remove edges to break every cycle. In the calls, the only cycles are the two methods that call themselves, so the back edges are the recursive calls:

// An algorithm that returns edges returns an Edges: here the back edges of a search,
// which are the recursive calls, since the only cycles of calls are methods that call
// themselves.
// Position p is the edge sourceNodes()[p] -> targetNodes()[p], and edgeIndices()[p]
// is its edge index in calls.
Edges recursive = BackEdges.of(calls);
List<String> lines = new ArrayList<>();
for (int p = 0; p < recursive.count(); p++) {
int caller = recursive.sourceNodes().getInt(p);
int callee = recursive.targetNodes().getInt(p);
int edge = recursive.edgeIndices().getInt(p);
lines.add(names[caller] + " -> " + names[callee] + " (edge " + edge + ")");
}
// lines: [parse -> parse (edge 10), format -> format (edge 11)]
Full example
// An algorithm that returns edges returns an Edges: here the back edges of a search,
// which are the recursive calls, since the only cycles of calls are methods that call
// themselves.
// Position p is the edge source_nodes()[p] -> target_nodes()[p], and
// edge_indices()[p] is its edge index in calls.
let recursive = back_edges::of(&calls);
let mut lines = Vec::new();
for p in 0..recursive.count() as usize {
let caller = recursive.source_nodes()[p];
let callee = recursive.target_nodes()[p];
let edge = recursive.edge_indices()[p];
let (from, to) = (names[caller as usize], names[callee as usize]);
lines.push(format!("{from} -> {to} (edge {edge})"));
}
// lines: ["parse -> parse (edge 10)", "format -> format (edge 11)"]
Full example
// An algorithm that returns edges returns an Edges: here the back edges of a search,
// which are the recursive calls, since the only cycles of calls are methods that call
// themselves.
// Position p is the edge sourceNodes[p] -> targetNodes[p], and edgeIndices[p] is its
// edge index in calls.
const recursive = BackEdges.of(calls);
const lines: string[] = [];
for (let p = 0; p < recursive.count; p++) {
const caller = recursive.sourceNodes[p];
const callee = recursive.targetNodes[p];
const edge = recursive.edgeIndices[p];
lines.push(`${names[caller]} -> ${names[callee]} (edge ${edge})`);
}
// lines: [parse -> parse (edge 10), format -> format (edge 11)]
Full example
// An algorithm that returns edges returns an Edges: here the back edges of a
// search, which are the recursive calls, since the only cycles of calls are
// methods that call themselves.
// Position p is the edge SourceNodes[p] -> TargetNodes[p], and EdgeIndices[p] is
// its edge index in calls.
Edges recursive = BackEdges.Of(calls);
List<string> lines = [];
for (int p = 0; p < recursive.Count; p++)
{
int caller = recursive.SourceNodes[p];
int callee = recursive.TargetNodes[p];
int edge = recursive.EdgeIndices[p];
lines.Add($"{names[caller]} -> {names[callee]} (edge {edge})");
}
// lines: [parse -> parse (edge 10), format -> format (edge 11)]
Full example

The result names the two recursive calls with their edge indices 10 and 11. They are the green edges of the picture after. The edge index leads to the data of each edge in your arrays. See Keep data on nodes and edges.