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.
Problem
Section titled “Problem”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.
Example
Section titled “Example”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.
- Methods of a package
- Package with its lines of code
- Call with its call sites
Solution
Section titled “Solution”// 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 -> formatint[] sitesPerPackageCall = byPackageCall.reduce(siteCount, 0, Integer::sum);// sitesPerPackageCall: [1, 2, 2, 1, 2, 2] - app -> cli has 1, service -> model 2// 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 -> formatlet 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// 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 -> formatconst sitesPerPackageCall = byPackageCall.reduce( siteCount, 0, (sum, sites) => sum + sites,);// sitesPerPackageCall: [1, 2, 2, 1, 2, 2] - app -> cli has 1, service -> model 2// 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 -> formatint[] sitesPerPackageCall = byPackageCall.Reduce(siteCount, 0, (a, b) => a + b);// sitesPerPackageCall: [1, 2, 2, 1, 2, 2] - app -> cli has 1, service -> model 2linesPerPackagelines_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.
Good to know
Section titled “Good to know”- Numbers.
reducereducereduceReducestarts each group from the identity you pass, such as 0 for a sum, and combines it with the values of the members in ascending order. A group without members keeps the identity, and the values of nodes or edges in no group are left out.long[]Vec<i64>BigInt64Arraylong[]anddouble[]Vec<f64>Float64Arraydouble[]reduce the same way. - Objects.
IndexPartition.reduce(T[], T[], ...)IndexPartition::reduce_intoIndexPartition.reduce<T>(values: ArrayLike<T>, groupValues: T[], combine: (accumulated: T, value: T) => T)IndexPartition.Reduce<T>(ReadOnlySpan<T>, Span<T>, Func<T, T, T>)reduces objects, such as lists of names, without an identity. It starts each group from the value of its first member and writes the results into an array you pass. In that array, a group without members keeps the entry it has. - Edges in no group. The condensed graph has no edge for two kinds of edges, so they are in no
group of the edge partition: edges inside a group, and edges from or to a node in no group.
EdgePartition.edgeCountEdgePartition::edge_countEdgePartition.edgeCountEdgePartition.EdgeCountis the number of edges of the graph, in a group or not. - The condensed graph of the same partition.
Graphs.edgePartitiongraphs::edge_partitionGraphs.edgePartitionGraphs.EdgePartitiontakes the graph thatNodePartition.condenseNodePartition::condenseNodePartition.condenseNodePartition.Condenseof the same partition returned. It checks only the node count, so another graph with as many nodes gives wrong groups without an error.
API used
Section titled “API used”IndexPartition.reduce(int[], int, IntBinaryOperator)IndexPartition::reduceIndexPartition.reduce<A>(values: A, identity: number, combine: (accumulated: number, value: number) => number)IndexPartition.Reduce<T>(ReadOnlySpan<T>, T, Func<T, T, T>)IndexPartition.reduce(long[], long, LongBinaryOperator)IndexPartition::reduceIndexPartition.reduce(values: BigInt64Array, identity: bigint, combine: (accumulated: bigint, value: bigint) => bigint)IndexPartition.Reduce<T>(ReadOnlySpan<T>, T, Func<T, T, T>)IndexPartition.reduce(double[], double, DoubleBinaryOperator)IndexPartition::reduceIndexPartition.reduce<A>(values: A, identity: number, combine: (accumulated: number, value: number) => number)IndexPartition.Reduce<T>(ReadOnlySpan<T>, T, Func<T, T, T>)IndexPartition.reduce(T[], T[], BinaryOperator)IndexPartition::reduce_intoIndexPartition.reduce<T>(values: ArrayLike<T>, groupValues: T[], combine: (accumulated: T, value: T) => T)IndexPartition.Reduce<T>(ReadOnlySpan<T>, Span<T>, Func<T, T, T>)Graphs.edgePartition(Graph, NodePartition)graphs::edge_partitionGraphs.edgePartitionGraphs.EdgePartitionEdgePartition.edgesOf(int)EdgePartition::edges_ofEdgePartition.edgesOfEdgePartition.EdgesOfEdgePartition.edgeCount()EdgePartition::edge_countEdgePartition.edgeCountEdgePartition.EdgeCountGraph.edgeIndexOf(int, int)Graph::edge_index_ofGraph.edgeIndexOfGraph.EdgeIndexOfNodePartition.condense()NodePartition::condenseNodePartition.condenseNodePartition.Condense