Chapter 7
Clustering and partitioning
Grouping the vertices of a hypergraph, its hyperedges, or both at once is the most common analysis after centrality: communities of co-authors, cells of a manufacturing system, narratives of a news collection, blocks of a circuit to be placed on separate chips. This chapter explains why a hypergraph objective counts a split group differently from a graph objective, defines the strict and all-or-nothing hypergraph modularities and a Louvain-type heuristic that maximizes them, and presents the alternatives available in hatvis: Louvain on projections, label propagation through the incidences, spectral clustering with the hypergraph Laplacian, and spectral co-clustering of vertices and hyperedges. The second half treats balanced partitioning with the cut-net and connectivity objectives and the Fiduccia–Mattheyses refinement. The examples recover planted communities, form manufacturing cells, and separate the narratives of the news data.
7.1 Problem and idea
A community is a set of vertices that interact more among themselves than with the rest. For a graph, “interact” means “share an edge”; for a hypergraph, it means “share a hyperedge”, and the question is how a hyperedge that is split between two communities should be counted. Consider a meeting of four people, two from each of two departments (fig. 7.1). In the clique expansion the meeting becomes six pairs, four of which cross the department boundary, so the split costs four. As a hyperedge it is one meeting that involves both departments, so the split costs one. Larger groups make the difference larger: a hyperedge of size split evenly contributes crossing pairs but one crossing hyperedge. A graph objective therefore penalizes splitting a large group much more than splitting a small one, while a hypergraph objective treats every group as a unit, whatever its size.
Neither convention is right in general. If the cost of a split is a communication cost per pair (every pair of people who must coordinate across a department boundary), the clique expansion is the correct model; if the cost is per group (a meeting that needs a room in both buildings, a net that must be routed between two chips, a task that requires resources of two cells), the hypergraph objective is. The idea of this chapter is to make the choice explicit: every method below states which representation and which counting it uses, and hatvis provides methods of both kinds so that the reader can choose by the cost model rather than by the software at hand [95].
7.2 Theory
7.2.1 Modularity
The modularity of a partition of a graph compares the weight of the edges inside the communities with the weight expected in a random graph with the same degrees [96]:
|
| (7.1) |
with degrees , total weight , and resolution . Applied to the clique expansion (2.2), it is the standard way of clustering a hypergraph with graph tools, and it inherits the quadratic weighting of large hyperedges described above.
Kamiński et al. [97] defined modularity directly on the hypergraph. In their strict version a hyperedge counts as internal only if all its vertices are in the same community, and the expected number of internal hyperedges of size is computed from a random hypergraph that preserves the degrees, in which the probability that a hyperedge of size falls entirely into community is , with :
|
| (7.2) |
where are the hyperedges of size , their total weight, , and indicates that lies inside one community. For a graph ( only) the expression reduces to the usual modularity up to the treatment of self-loops. Chodrow, Veldt, and Benson [98] derived an all-or-nothing modularity of the same structure as the maximum-likelihood objective of a degree-corrected hypergraph stochastic block model in which a hyperedge is more likely when all its members are in one community; in their objective each size has its own weight, estimated from the data, which lets the model learn whether large groups tend to be homogeneous. Both objectives are all-or-nothing: a hyperedge with one member outside the community earns nothing. This is the right model for interactions that require the whole group, and it is the reason why these objectives prefer coarse partitions that keep large hyperedges intact.
7.2.2 Maximizing hypergraph modularity
The Louvain method [99] maximizes graph modularity by moving single vertices to the neighboring community with the largest gain and then aggregating communities into super-vertices. Under the strict objective (7.2) single-vertex moves from a singleton start rarely pay off, because a hyperedge becomes internal only when its last member joins, so a naive transfer of the method stalls near the singleton partition. The heuristic of hatvis (algorithm 7.1) therefore starts from the Louvain partition of the clique expansion, which already groups vertices that share many hyperedges, then alternates exact local moves and greedy merges of communities, both evaluated with the hypergraph objective, until neither improves it. The merge step follows the agglomerative scheme considered for hypergraph modularity by Kamiński et al. [97]; Kumar et al. [100] proposed a different route, iteratively reweighting the clique expansion so that graph Louvain approximates the hypergraph objective.
7.2.3 Other community methods
Louvain on projections. Louvain on the clique expansion groups vertices; Louvain on the weighted -line graph groups hyperedges, which is how Nagy et al. [4] found activity-based modules of a manufacturing space in addition to resource-based ones. Both are fast and robust; they optimize graph modularity of the chosen projection, not (7.2).
Label propagation. The label propagation algorithm of Raghavan et al. [101] lets every vertex adopt the most frequent label among its neighbors. On a hypergraph hatvis propagates through the incidences: every hyperedge adopts the weighted majority label of its members, then every vertex adopts the weighted majority label of its hyperedges, with weights and random tie-breaking, until the labels are stable. It needs no parameter and runs in linear time per sweep, but it is sensitive to noise and tends to merge communities when many hyperedges cross them.
Spectral clustering. Zhou et al. [35] generalized the normalized cut to hypergraphs. For a vertex set with complement ,
|
| (7.3) |
where a hyperedge contributes in proportion to the number of member pairs it splits, normalized by its size. The relaxed problem is solved by the eigenvectors of the normalized Laplacian (2.4); for communities the rows of the leading eigenvectors of are normalized to unit length and clustered with -means [102]. Spectral clustering needs the number of communities and fails on disconnected hypergraphs (each component produces a zero eigenvalue), but it is deterministic up to the -means start and uses global information.
Co-clustering of vertices and hyperedges. Spectral co-clustering [51] (section 3.2.5) assigns vertices and hyperedges jointly to groups by bipartite spectral partitioning of the incidence matrix (3.5). It answers a different question from the vertex methods: not only which vertices belong together, but also which hyperedges define each group. For a document collection the co-clusters are topics with their documents, for a manufacturing space resource cells with their activities.
Statistical models. Generative models infer communities with uncertainty and allow overlapping memberships. Beyond the stochastic block model behind the all-or-nothing modularity [98], Contisciani, Battiston, and De Bacco [103] and Ruggeri et al. [104] fit mixed-membership models that scale to very large hypergraphs; Hypergraphx [23] implements several of them, and the converters of section 2.3 make them available for hatvis hypergraphs.
7.2.4 Balanced partitioning
Partitioning asks for blocks of nearly equal size that cut as little as possible. It is the classical hypergraph problem of VLSI design, where vertices are cells, hyperedges are nets, and blocks are chips or regions, and it is used in parallel computing to distribute data with little communication [105, 106]. For a partition into blocks , let be the number of blocks that hyperedge touches. Three cost functions are standard:
|
| (7.4) |
The cut-net counts every split hyperedge once; the connectivity metric counts how many additional blocks each hyperedge reaches, which is the communication volume when every block holding a member must receive the data of the net; the sum of external degrees (SOED) is the sum of the two. For all three coincide up to a factor. The balance constraint requires . Finding an optimal balanced partition is NP-hard [107].
The Fiduccia–Mattheyses (FM) heuristic [108] improves a bisection by moving single vertices between the two blocks. In each pass every vertex is moved exactly once, always the vertex whose move gives the largest decrease of the cut among those allowed by the balance constraint, even if the gain is negative; the gains are updated incrementally from the number of members each hyperedge has in each block. At the end of the pass the sequence of moves is rolled back to the prefix with the smallest cut. Accepting negative gains inside a pass lets FM escape local minima, and the gain buckets make a pass run in time linear in the number of incidences. Multilevel partitioners such as hMETIS [105] and KaHyPar [106] coarsen the hypergraph, partition the coarsest level, and refine with FM on the way back; hatvis implements the flat multi-start FM (algorithm 7.2), sufficient for hypergraphs up to tens of thousands of incidences, and obtains -way partitions by recursive bisection.
The normalized cut (7.3) offers a different balance: it does not fix the block sizes but divides the cut by the volumes, so that cutting off a small piece is expensive. The sign of the Fiedler vector of the normalized Laplacian is its spectral relaxation for [35].
7.2.5 Comparing partitions
When a ground truth is known, as in the synthetic examples, partitions are compared with the adjusted Rand index (ARI) of Hubert and Arabie [109]: the fraction of vertex pairs on which two partitions agree (together in both or apart in both), corrected for the agreement expected by chance. It is 1 for identical partitions, close to 0 for independent ones, and does not depend on the labels or the number of communities.
In a nutshell: Clustering and partitioning
- A split hyperedge of size costs one unit in a hypergraph objective and up to pairs in the clique expansion; choose by the cost model.
- Strict hypergraph modularity (7.2) (Kamiński et al.) and all-or-nothing modularity (Chodrow et al.): a hyperedge is internal only if all members are in one community; hypergraph Louvain: clique-Louvain start, exact local moves, greedy merges.
- Alternatives: Louvain on the clique expansion (vertices) or line graph (hyperedges), label propagation through incidences, spectral clustering with the normalized hypergraph cut (7.3), spectral co-clustering of vertices and hyperedges.
- Partitioning: cut-net, connectivity, SOED (7.4) under a balance constraint; multi-start Fiduccia–Mattheyses, recursive bisection; remove isolated vertices before spectral bisection.
- Evaluation: modularity, cut metrics, adjusted Rand index.
7.3 Using it in hatvis
hypergraph_louvain(H, resolution=1.0, init="clique", random_state=None, max_rounds=20)
- Returns
-
a partition (Series of community labels ).
- Related
-
modularity(H, partition, resolution) (strict hypergraph modularity), graph_modularity(H, partition), louvain(H, target="nodes"|"edges", weighting, resolution, s), label_propagation(H), spectral_clustering(H, n_clusters), spectral_coclustering(H, n_clusters), communities(partition), partition_sizes(partition).
partition.fm_bisection(H, imbalance=0.03, n_starts=10, max_passes=20, random_state=None)
- Returns
-
a two-block partition.
- Related
-
partition.recursive_bisection(H, n_blocks), partition.cut_metrics(H, partition) (cut-net, connectivity, SOED, number of blocks, imbalance), partition.normalized_cut(H, subset), spectral.fiedler_vector(H).
7.4 Examples
Planted communities. A hypergraph stochastic block model (section 9.2.1) with 120 vertices in four equal communities and 300 hyperedges of sizes 2 to 5 is generated for decreasing shares of intra-community hyperedges; the remaining hyperedges draw their members from the whole vertex set. Four methods are compared by the ARI against the planted partition.
methods = { "hypergraph Louvain": hv.hypergraph_louvain(H, random_state=0), "clique Louvain": hv.louvain(H, random_state=0), "spectral (Zhou)": hv.spectral_clustering(H, 4, random_state=0), "label propagation": hv.label_propagation(H, random_state=0), }
| hypergraph Louvain | clique Louvain | spectral (Zhou) | label propagation | |
| 0.90 | 1.000 | 1.000 | 1.000 | 1.000 |
| 0.75 | 1.000 | 1.000 | 1.000 | 0.978 |
| 0.60 | 1.000 | 1.000 | 0.978 | 0.548 |
| 0.45 | 0.956 | 0.872 | 0.869 | 0.380 |
| 0.30 | 0.144 | 0.206 | 0.190 | 0.000 |
All methods recover the communities when most hyperedges are internal (table 7.1, fig. 7.2). Label propagation degrades first: at it has merged communities, and at 0.3 it returns a single one. The interesting regime is , where more than half of the hyperedges are random: hypergraph Louvain still reaches an ARI of 0.956, while clique Louvain and spectral clustering fall to about 0.87. In this model the planted structure is defined by whole hyperedges being internal, which is exactly what the strict modularity counts, whereas the clique expansion dilutes it with the many crossing pairs of the large random hyperedges. At no method finds the structure; the three modularity- and spectrum-based methods are equally weak there, and the strict objective, which needs internal hyperedges to reward a community, is slightly worse than the clique expansion.
Open in ColabExample plot_21_planted_communities.py in the gallery
Forming manufacturing cells. Following Nagy et al. [4], the activities of the wire-harness space are grouped by Louvain on their line graph and the resources by hypergraph Louvain.
act = hv.louvain(H, target="edges", random_state=0) res = hv.hypergraph_louvain(H, random_state=0) res_clique = hv.louvain(H, random_state=0)
The activities fall into three communities of 24, 24, and 22. The first contains all transport and handling between stations (buffer handling, hand-overs, and most AGV loading); the second the twenty crimping activities together with four assembly activities; the third the remaining sixteen assembly activities with six AGV loading steps. Each community contains activities of both lines in equal numbers, so the modules are functional, not line-based: the two lines are copies of each other, and what distinguishes activities is the kind of work, not where it happens. The resources form three communities: the transport pair (the operator and the competence of AGV loading); a community of fifteen resources with all four crimping machines, the crimping competence , the robots and , the operators and , the three screwdrivers with the screw-fastening competence , and the sensors and ; and a community of fourteen resources with the other operators and robots, the AGV, the tools and , the competences and , and the sensor . The strict modularity of the hypergraph Louvain partition is 0.419, higher than the 0.394 of the clique Louvain partition evaluated with the same objective. Figure 7.3 shows the incidence matrix ordered by the two partitions, in which the cells appear as blocks, and the clique expansion colored by the resource communities. The AGV itself does not stay with the transport pair: it takes part in more activities than any operator, and the objective places it with the resources with which it shares the largest volume of activities. The partition is a proposal, not a design; it shows which groupings keep the most activities inside one cell and which resources (here the sensors and the screwdrivers) would have to be shared between cells in any division.
Open in ColabExample plot_22_cell_formation.py in the gallery
Balanced partitioning. A planted two-block hypergraph with 80 vertices and 200 hyperedges of sizes 2 to 4 () is bisected by multi-start FM, by the sign of the Fiedler vector, and at random.
fm = partition.fm_bisection(H, imbalance=0.05, n_starts=10, random_state=0) Hc = H.remove_isolated() # an isolated vertex would get its own block fiedler = (hv.spectral.fiedler_vector(Hc) > 0).astype(int).reindex(H.nodes, fill_value=0) rnd = pd.Series(np.random.default_rng(0).permutation([0, 1] * 40), index=H.nodes) table = pd.DataFrame({name: partition.cut_metrics(H, p) for name, p in {"FM": fm, "Fiedler sign": fiedler, "random": rnd, "planted": truth}.items()}).T table["normalized cut"] = [partition.normalized_cut(H, p.index[p == 0]) for p in (fm, fiedler, rnd, truth)]
FM and the Fiedler bisection both find the planted partition exactly: cut-net and connectivity of 25, normalized cut 0.121, perfect balance; the random bisection cuts 146 hyperedges. The hypergraph has one isolated vertex, and the script removes it before computing the Fiedler vector. Without that step the normalized Laplacian has a second zero eigenvalue, its eigenvector is concentrated on the isolated vertex, and the sign split puts one vertex in one block and the other 79 in the other: a cut of zero with an imbalance of 0.975, a degenerate solution that the normalized cut avoids only when the hypergraph is connected. FM, which enforces the balance explicitly, is immune to this. Recursive bisection into four blocks cuts 108 hyperedges with connectivity of 115 at 5% imbalance; since only two blocks are planted, the second level has to cut through communities, and the cost rises accordingly.
Co-clustering the news data. Spectral co-clustering of the incidence matrix of 180 synthetic articles (60 per period) into six co-clusters groups themes with the articles that use them.
rl, cl = hv.spectral_coclustering(T, n_clusters=6, random_state=0) rows = list(rl.sort_values(kind="stable").index) cols = list(cl.sort_values(kind="stable").index)
The cross-tabulation of the theme co-clusters against the planted narratives is a near-permutation: climate science, policy, society, and energy each form a co-cluster of exactly their four themes, and the finance and impacts themes share one co-cluster. The sixth co-cluster has no themes of its own; its 24 articles are mostly mixtures of the climate narrative with another one, which the method sets apart from the single-narrative articles. Ordering the incidence matrix by the co-clusters (fig. 7.5) shows the narratives as dense blocks, with the two-narrative articles as the off-diagonal entries.
7.5 Further reading
Kamiński et al. [97] define hypergraph modularity and discuss its strict and weaker variants; Chodrow, Veldt, and Benson [98] derive the all-or-nothing modularity from a stochastic block model and give a Louvain algorithm for it; Kumar et al. [100] approximate it by reweighting the clique expansion. Veldt, Benson, and Kleinberg [95] unify hypergraph cut objectives through splitting functions and show when they can be reduced to graph cuts. Contisciani et al. [103] and Ruggeri et al. [104] develop scalable mixed-membership models. For partitioning, Karypis et al. [105] describe the multilevel paradigm of hMETIS and Schlag et al. [106] the state-of-the-art KaHyPar framework.