Skip to content

1.5 Keep data on nodes and edges

You keep names, weights and anything else about nodes and edges in arrays indexed by node ID and edge index.

You want to keep data on your nodes and edges, but Graphity stores only the nodes and edges. You need a place for the data in which each value belongs to one node or edge, and that an analysis can read quickly. Almost every graph has such data:

  • names, sizes and owners of the nodes, such as the name and the package of a method;
  • counts and weights of the edges, such as the number of places where one method calls another;
  • results of an analysis, such as the group of each node, kept for the next step.

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. Each method belongs to a package. That is data on the nodes, and the gray frames show the packages. A call can occur at several places in the code, which are called its call sites. Their number is data on the edges, and the pictures write it on each edge. The list callSitescall_sitescallSitescallSites holds the caller and the callee of each call site. Which calls cross a package boundary, and how many call sites does each of them have? The picture after shows these calls in green:

The calls, with their packages and call sites
The calls between packages
1111211111210: main2: run1: parseArgs3: load4: save5: show6: parse7: format8: read9: writestoragemodeluiservicecliapp
1111211111210: main2: run1: parseArgs3: load4: save5: show6: parse7: format8: read9: writestoragemodeluiservicecliapp
Context
  • Package
Output
  • Call between packages

Data on nodes is an array indexed by node ID. Data on edges is an array indexed by edge index, and Graph.edgeIndexOfGraph::edge_index_ofGraph.edgeIndexOfGraph.EdgeIndexOf finds the edge index of each call:

// Data on nodes: one array per kind of data, indexed by node ID.
String[] packageOf = {"app", "cli", "app", "service", "service", "ui", "model",
"model", "storage", "storage"};
// Data on edges: one array per kind of data, indexed by edge index.
int[] siteCount = new int[calls.edgeCount()];
for (CallSite site : callSites) {
siteCount[calls.edgeIndexOf(site.caller(), site.callee())]++;
}
Full example
// Data on nodes: one array per kind of data, indexed by node ID.
let package_of = [
"app", "cli", "app", "service", "service", "ui", "model", "model", "storage",
"storage",
];
// Data on edges: one array per kind of data, indexed by edge index. edge_index_of
// returns None for two nodes without an edge, which no call site is.
let mut site_count = vec![0; calls.edge_count() as usize];
for site in &call_sites {
site_count[calls.edge_index_of(site.caller, site.callee).unwrap() as usize] += 1;
}
Full example
// Data on nodes: one array per kind of data, indexed by node ID.
const packageOf = ["app", "cli", "app", "service", "service", "ui", "model", "model",
"storage", "storage"];
// Data on edges: one array per kind of data, indexed by edge index.
const siteCount = new Int32Array(calls.edgeCount);
for (const site of callSites) {
siteCount[calls.edgeIndexOf(site.caller, site.callee)]++;
}
Full example
// Data on nodes: one array per kind of data, indexed by node ID.
string[] packageOf =
[
"app", "cli", "app", "service", "service",
"ui", "model", "model", "storage", "storage",
];
// Data on edges: one array per kind of data, indexed by edge index.
int[] siteCount = new int[calls.EdgeCount];
foreach (CallSite site in callSites)
{
siteCount[calls.EdgeIndexOf(site.Caller, site.Callee)]++;
}
Full example

Graph.forEachEdgeGraph::for_each_edgeGraph.forEachEdgeGraph.ForEachEdge visits every edge with its source, its target and its edge index. The edge index leads to the data of the edge:

// The calls between packages, each with its number of call sites. The edge index
// finds the data of an edge in its arrays.
List<String> lines = new ArrayList<>();
calls.forEachEdge((caller, callee, edge) -> {
if (!packageOf[caller].equals(packageOf[callee])) {
lines.add(names[caller] + " -> " + names[callee] + ": " + siteCount[edge]);
}
});
// lines: [main -> parseArgs: 1, run -> load: 1, run -> save: 1, run -> show: 2,
// load -> parse: 1, load -> read: 1, save -> format: 1, save -> write: 1,
// show -> format: 1]
Full example
// The calls between packages, each with its number of call sites. The edge index
// finds the data of an edge in its arrays.
let mut lines = Vec::new();
calls.for_each_edge(|caller, callee, edge| {
if package_of[caller as usize] != package_of[callee as usize] {
let (from, to) = (names[caller as usize], names[callee as usize]);
lines.push(format!("{from} -> {to}: {}", site_count[edge as usize]));
}
});
// lines: ["main -> parseArgs: 1", "run -> load: 1", "run -> save: 1",
// "run -> show: 2", "load -> parse: 1", "load -> read: 1",
// "save -> format: 1", "save -> write: 1", "show -> format: 1"]
Full example
// The calls between packages, each with its number of call sites. The edge index
// finds the data of an edge in its arrays.
const lines: string[] = [];
calls.forEachEdge((caller, callee, edge) => {
if (packageOf[caller] !== packageOf[callee]) {
lines.push(`${names[caller]} -> ${names[callee]}: ${siteCount[edge]}`);
}
});
// lines: [main -> parseArgs: 1, run -> load: 1, run -> save: 1, run -> show: 2,
// load -> parse: 1, load -> read: 1, save -> format: 1, save -> write: 1,
// show -> format: 1]
Full example
// The calls between packages, each with its number of call sites. The edge index
// finds the data of an edge in its arrays.
List<string> lines = [];
calls.ForEachEdge((caller, callee, edge) =>
{
if (packageOf[caller] != packageOf[callee])
{
lines.Add($"{names[caller]} -> {names[callee]}: {siteCount[edge]}");
}
});
// lines: [main -> parseArgs: 1, run -> load: 1, run -> save: 1, run -> show: 2,
// load -> parse: 1, load -> read: 1, save -> format: 1, save -> write: 1,
// show -> format: 1]
Full example

The result lists the nine of the twelve calls that cross a package boundary, each with its number of call sites. They are the green edges of the picture after. run -> show has two call sites, since run shows the notes before and after saving them.