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.
Problem
Section titled “Problem”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.
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. 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:
- Method asked about
- Out-node of save or show
- Recursive call
Solution
Section titled “Solution”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 showint size = calledByRun.size(); // 3int first = calledByRun.first(); // 3int last = calledByRun.last(); // 5int position = calledByRun.indexOf(save); // 1 - found by binary searchboolean 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 changedint[] copy = calledByRun.toIntArray();// 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 showlet size = called_by_run.len(); // 3let 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 searchlet 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 changedconst calledByRun = calls.outNodes(run); // [3, 4, 5] - load, save and showconst size = calledByRun.size; // 3const first = calledByRun.first(); // 3const last = calledByRun.last(); // 5const position = calledByRun.indexOf(save); // 1 - found by binary searchconst 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 changedconst copy = calledByRun.toInt32Array();// [3, 4, 5] - load, save and showSortedIntView calledByRun = calls.OutNodes(run);int count = calledByRun.Count; // 3int first = calledByRun.First(); // 3int last = calledByRun.Last(); // 5int position = calledByRun.IndexOf(save); // 1 - found by binary searchbool 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 changedint[] copy = calledByRun.ToArray();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)]// 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)"]// 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)]// 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)]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.
Good to know
Section titled “Good to know”- Views, not copies. A
SortedIntViewSortedIntViewSortedIntViewSortedIntViewof out-nodes shares the graph’s own array, and it cannot be changed.SortedIntView.toIntArray<[u32]>::to_vecSortedIntView.toInt32ArraySortedIntView.ToArraymakes a copy that you can change. - Your own sorted arrays.
SortedIntView.viewOfSortedIntView::view_ofSortedIntView.viewOfSortedIntView.ViewOfmakes a view of a range of an array, so that you can combine it with the lists of Graphity. The range must be strictly ascending, which means sorted and without duplicates. Any other range is rejected. The view shares your array, so this range must not change while the view is in use. - Searching.
SortedIntView.indexOfSortedIntView::index_ofSortedIntView.indexOfSortedIntView.IndexOfandSortedIntView.lastIndexOfSortedIntView::last_index_ofSortedIntView.lastIndexOfSortedIntView.LastIndexOfsearch in logarithmic time. Since values occur only once, both return the same position. - Lists in an order of their own. A topological order, a path, a cycle, the body of a loop, a subtree, and the sources and targets of an edge list come as plain lists of node IDs in their own order. They are not sorted and not always without duplicates. They read like any list, but a binary search or a union does not apply to them. Treat all of these lists, and the edge indices of an edge list, as read-only: copy one before you sort or change it.
- The order of an edge list. Edge lists come in edge index order, except the back edges of
BackEdges.ofback_edges::ofBackEdges.ofBackEdges.Of. Those come in the order in which the search finds them.Edges.ofEdges::ofEdges.ofEdges.Ofwith the back type returns the same edges in edge index order. - Into a builder. Both kinds of lists go into a
GraphBuilderGraphBuilderGraphBuilderGraphBuilderwithout being copied into an array first. A node list goes in as the out-nodes of a node withGraphBuilder.addOutEdgesGraphBuilder::add_out_edgesGraphBuilder.addOutEdgesGraphBuilder.AddOutEdgesor as a path withGraphBuilder.addPathGraphBuilder::add_pathGraphBuilder.addPathGraphBuilder.AddPath. An edge list goes in withGraphBuilder.addEdgesGraphBuilder::add_edgesGraphBuilder.addEdgesGraphBuilder.AddEdges.
API used
Section titled “API used”Graph.outNodes(int)Graph::out_nodesGraph.outNodesGraph.OutNodesSortedIntView.size()<[u32]>::lenSortedIntView.sizeSortedIntView.CountSortedIntView.first()SortedIntView::firstSortedIntView.firstSortedIntView.FirstSortedIntView.last()SortedIntView::lastSortedIntView.lastSortedIntView.LastSortedIntView.indexOf(int)SortedIntView::index_ofSortedIntView.indexOfSortedIntView.IndexOfSortedIntView.lastIndexOf(int)SortedIntView::last_index_ofSortedIntView.lastIndexOfSortedIntView.LastIndexOfSortedIntView.union(SortedIntView)SortedIntView::unionSortedIntView.unionSortedIntView.UnionSortedIntView.toIntArray()<[u32]>::to_vecSortedIntView.toInt32ArraySortedIntView.ToArraySortedIntView.viewOf(int[], int, int)SortedIntView::view_ofSortedIntView.viewOfSortedIntView.ViewOfBackEdges.of(Graph)back_edges::ofBackEdges.ofBackEdges.OfEdges.count()Edges::countEdges.countEdges.CountEdges.isEmpty()Edges::is_emptyEdges.isEmptyEdges.IsEmptyEdges.sourceNodes()Edges::source_nodesEdges.sourceNodesEdges.SourceNodesEdges.targetNodes()Edges::target_nodesEdges.targetNodesEdges.TargetNodesEdges.edgeIndices()Edges::edge_indicesEdges.edgeIndicesEdges.EdgeIndicesEdges.of(EdgeTypes, EdgeType)Edges::ofEdges.ofEdges.OfGraphBuilder.addOutEdges(int, IntList)GraphBuilder::add_out_edgesGraphBuilder.addOutEdges(source: number, targets: IntList)GraphBuilder.AddOutEdgesGraphBuilder.addPath(IntList)GraphBuilder::add_pathGraphBuilder.addPath(nodes: IntList)GraphBuilder.AddPathGraphBuilder.addEdges(Edges)GraphBuilder::add_edgesGraphBuilder.addEdgesGraphBuilder.AddEdges