Skip to content

9.7 Reduce the data of the nodes or edges of a group

You reduce the values of the nodes or edges of a group to one value, such as a sum, a maximum or a list, and get the edges behind an edge of a condensed graph.

You have divided nodes or edges into groups and want one value per group, made from the data of its members. Such a division is called a partition. If you group nodes, the graph with one node per group is called the condensed graph. Sometimes you also want to know which edges of the original graph are behind one edge of the condensed graph. You need this for:

  • the lines of code of a package, as the sum over its methods;
  • the traffic between two data centers, as the sum over the connections between their servers;
  • the label of a group, as the list of the names of its members.

The example graph calls models the calls between the ten methods of a small program. It has an edge from each method to each method it calls. A place in the code where one method calls another is called a call site. The array linesOflines_oflinesOflinesOf holds the lines of code of each method. The array siteCountsite_countsiteCountsiteCount holds the number of call sites of each call. The partition byPackageby_packagebyPackagebyPackage groups the methods into eight packages, of which json and util have no methods. The graph packageCallspackage_callspackageCallspackageCalls is the condensed graph, which holds the calls between the packages. See Condense groups of your own choice for how to build both. How many lines of code does each package have? Which calls are behind the call from service to model? How many call sites does each call between packages have? The picture after shows the condensed graph with these sums. The lines of code of each package stand in brackets, and the call sites stand on the edges.

The calls, grouped by package
The calls between the packages, with the sums
1111211111210: main (12)2: run (20)1: parseArgs (30)5: show (15)3: load (25)4: save (18)6: parse (60)7: format (40)8: read (10)9: write (10)storagemodelserviceuicliapp
1221220: app (32)1: cli (30)2: ui (15)3: service (43)4: model (100)5: storage (20)6: json (0)7: util (0)
Input
  • Methods of a package
Output
  • Package with its lines of code
  • Call with its call sites
// The lines of code of each method:
int[] linesOf = {12, 30, 20, 25, 18, 15, 60, 40, 10, 10};
int[] linesPerPackage = byPackage.reduce(linesOf, 0, Integer::sum);
// linesPerPackage: [32, 30, 15, 43, 100, 20, 0, 0] - model has 100, json and util
// none
// Edge data: which calls are behind each call between packages, and their sum.
EdgePartition byPackageCall = Graphs.edgePartition(packageCalls, byPackage);
SortedIntView behind =
byPackageCall.edgesOf(packageCalls.edgeIndexOf(service, model));
// behind: [5, 7] - load -> parse and save -> format
int[] sitesPerPackageCall = byPackageCall.reduce(siteCount, 0, Integer::sum);
// sitesPerPackageCall: [1, 2, 2, 1, 2, 2] - app -> cli has 1, service -> model 2
Full example
// The lines of code of each method:
let lines_of = [12, 30, 20, 25, 18, 15, 60, 40, 10, 10];
let lines_per_package = by_package.reduce(&lines_of, 0, |sum, lines| sum + lines);
// lines_per_package: [32, 30, 15, 43, 100, 20, 0, 0] - model has 100, json and util
// none
// Edge data: which calls are behind each call between packages, and their sum.
// edge_index_of returns None if there is no such edge.
let by_package_call = graphs::edge_partition(&package_calls, &by_package);
let behind =
by_package_call.edges_of(package_calls.edge_index_of(service, model).unwrap());
// behind: [5, 7] - load -> parse and save -> format
let sites_per_package_call =
by_package_call.reduce(&site_count, 0, |sum, sites| sum + sites);
// sites_per_package_call: [1, 2, 2, 1, 2, 2] - app -> cli has 1, service -> model 2
Full example
// The lines of code of each method:
const linesOf = Int32Array.of(12, 30, 20, 25, 18, 15, 60, 40, 10, 10);
const linesPerPackage = byPackage.reduce(linesOf, 0, (sum, lines) => sum + lines);
// linesPerPackage: [32, 30, 15, 43, 100, 20, 0, 0] - model has 100, json and util
// none
// Edge data: which calls are behind each call between packages, and their sum.
const byPackageCall = Graphs.edgePartition(packageCalls, byPackage);
const behind = byPackageCall.edgesOf(packageCalls.edgeIndexOf(service, model));
// behind: [5, 7] - load -> parse and save -> format
const sitesPerPackageCall = byPackageCall.reduce(
siteCount,
0,
(sum, sites) => sum + sites,
);
// sitesPerPackageCall: [1, 2, 2, 1, 2, 2] - app -> cli has 1, service -> model 2
Full example
// The lines of code of each method:
int[] linesOf = [12, 30, 20, 25, 18, 15, 60, 40, 10, 10];
int[] linesPerPackage = byPackage.Reduce(linesOf, 0, (a, b) => a + b);
// linesPerPackage: [32, 30, 15, 43, 100, 20, 0, 0] - model has 100, json and util
// none
// Edge data: which calls are behind each call between packages, and their sum.
EdgePartition byPackageCall = Graphs.EdgePartition(packageCalls, byPackage);
SortedIntView behind =
byPackageCall.EdgesOf(packageCalls.EdgeIndexOf(service, model));
// behind: [5, 7] - load -> parse and save -> format
int[] sitesPerPackageCall = byPackageCall.Reduce(siteCount, 0, (a, b) => a + b);
// sitesPerPackageCall: [1, 2, 2, 1, 2, 2] - app -> cli has 1, service -> model 2
Full example

linesPerPackagelines_per_packagelinesPerPackagelinesPerPackage has one sum per package. model has 100 lines from parse and format, and json and util have none. The call service -> model stands for the edges 5 and 7 of the calls: load -> parse and save -> format. Each of them has one call site, so service -> model has two. sitesPerPackageCallsites_per_package_callsitesPerPackageCallsitesPerPackageCall holds these sums by edge index of the condensed graph, as on the edges of the picture after.