9.6 Condense groups of your own choice
You divide the nodes into groups you choose, and get the condensed graph between the groups.
Problem
Section titled “Problem”You have put the nodes of a graph into groups of your own and want the graph between the groups, for example: which package calls which, or which data center talks to which. Such a division into groups is called a partition. The graph between the groups is called the condensed graph. The groups can be:
- methods grouped by class or package, for the calls between packages;
- servers grouped by data center, for the traffic between data centers;
- tasks grouped by team, for the work that passes from one team to another.
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. The program has eight packages: app, cli, ui,
service, model, storage, json and util. Every method belongs to one of the first six, so
json and util have no methods. The array
packageOfpackage_ofpackageOfpackageOf holds the package of
each method. Which package calls which? The picture before frames the methods by package. The
picture after shows the condensed graph, which holds the calls between the packages:
- Methods of a package
- Package with its group ID
- From a package to a package it calls
Solution
Section titled “Solution”NodePartition byPackage = NodePartition.of(calls, packageOf, 8);// 8 - json and util are emptyint groupCount = byPackage.groupCount();int sizeOfService = byPackage.sizeOf(service); // 2// [3, 4] - load and saveSortedIntView methodsOfService = byPackage.nodesOf(service);
// The calls between packages: an edge wherever a method of one calls a method of// another.Graph packageCalls = byPackage.condense();// packageCalls: app -> [cli, ui, service], ui -> [model], service -> [model, storage]let by_package = NodePartition::of(&calls, &package_of, 8)?;let group_count = by_package.group_count(); // 8 - json and util are emptylet size_of_service = by_package.size_of(service); // 2let methods_of_service = by_package.nodes_of(service); // [3, 4] - load and save
// The calls between packages: an edge wherever a method of one calls a method of// another.let package_calls = by_package.condense();// package_calls: app -> [cli, ui, service], ui -> [model],// service -> [model, storage]const byPackage = NodePartition.of(calls, packageOf, 8);const groupCount = byPackage.groupCount; // 8 - json and util are emptyconst sizeOfService = byPackage.sizeOf(service); // 2const methodsOfService = byPackage.nodesOf(service); // [3, 4] - load and save
// The calls between packages: an edge wherever a method of one calls a method of// another.const packageCalls = byPackage.condense();// packageCalls: app -> [cli, ui, service], ui -> [model], service -> [model, storage]NodePartition byPackage = NodePartition.Of(calls, packageOf, 8);// 8 - json and util are emptyint groupCount = byPackage.GroupCount;int sizeOfService = byPackage.SizeOf(service); // 2// [3, 4] - load and saveSortedIntView methodsOfService = byPackage.NodesOf(service);
// The calls between packages: an edge wherever a method of one calls a method of// another.Graph packageCalls = byPackage.Condense();// packageCalls: app -> [cli, ui, service], ui -> [model],// service -> [model, storage]The condensed graph packageCallspackage_callspackageCallspackageCalls has a node for each group,
with the group IDs as node IDs. That makes one node for each of the eight packages. json and
util have no methods and stay without edges. Calls within a package, such as main -> run, are
left out. The two calls from service to model are load -> parse and save -> format. They
become one edge, as in the picture after.
Good to know
Section titled “Good to know”- Nodes in no group. A group ID of
-1NONE-1-1leaves a node out; its edges are left out of the condensed graph too. - Empty groups stay as nodes without edges, so that the group IDs stay node IDs.
- Groups that Graphity finds. The strongly connected components are a partition that Graphity finds by itself. A partition of your own works the same way. See Condense each group into one node to get a DAG (directed acyclic graph).
- Cycles between groups. Unlike the condensed graph of the strongly connected components, the
condensed graph of groups of your own can have cycles even where the
graph has none. For example, the groups
{a, c}and{b}with the edgesa -> bandb -> cgive an edge each way between the two groups.Graphs.isDaggraphs::is_dagGraphs.isDagGraphs.IsDagtells whether the condensed graph has none. - Sizes and members.
IndexPartition.groupCountIndexPartition::group_countIndexPartition.groupCountIndexPartition.GroupCountis the number of groups.NodePartition.nodeCountNodePartition::node_countNodePartition.nodeCountNodePartition.NodeCountis the number of nodes of the graph, in a group or not.NodePartition.nodesOfNodePartition::nodes_ofNodePartition.nodesOfNodePartition.NodesOflists the nodes of a group.IndexPartition.indicesOfIndexPartition::indices_ofIndexPartition.indicesOfIndexPartition.IndicesOflists the members of a group in any partition of nodes or edges. - The data of the merged nodes and edges. See Reduce the data of the nodes or edges of a group for this data and the edges behind an edge of the condensed graph.
API used
Section titled “API used”NodePartition.of(Graph, int[], int)NodePartition::ofNodePartition.ofNodePartition.OfNodePartition.condense()NodePartition::condenseNodePartition.condenseNodePartition.CondenseIndexPartition.groupCount()IndexPartition::group_countIndexPartition.groupCountIndexPartition.GroupCountIndexPartition.sizeOf(int)IndexPartition::size_ofIndexPartition.sizeOfIndexPartition.SizeOfNodePartition.nodesOf(int)NodePartition::nodes_ofNodePartition.nodesOfNodePartition.NodesOfNodePartition.nodeCount()NodePartition::node_countNodePartition.nodeCountNodePartition.NodeCountIndexPartition.indicesOf(int)IndexPartition::indices_ofIndexPartition.indicesOfIndexPartition.IndicesOfGraphs.isDag(Graph)graphs::is_dagGraphs.isDagGraphs.IsDag