rsclusters finds overlapping communities in large undirected graphs. It helps power bubble classification at billion-tweet scale for twitter.cat.
It is a Rust port of Twitter's sparse binary factorization, the Metropolis-Hastings community detection step behind SimClusters.
It reads the same .config files and METIS input as the Java jar and writes the same output files. It also reads a binary CSR format, which is much faster to load than METIS text.
cargo build --releaseThe binary ends up in target/release/rsclusters. Building needs a C linker (cc) because some dependencies have build scripts.
rsclusters graph.configThe config format matches the Java version, but there are four additions:
csrPrefix <prefix>loads the binary format described below instead ofmetisFile.checkGraph falseskips the parallel symmetry and sort check on load. It is on by default, like in Java.temperatureRatioexists in the JavaAlgorithmConfigbut the Java config parser ignores it. Here you can set it.proposalStrategy PureRandomis accepted. Java only reaches that strategy from its tests.
All progress goes to stderr, including the per-epoch table, which uses the same columns and number formats as the Java version.
Turns METIS text into the binary format.
rsclusters convert graph.metis graphA port of the Java GraphGenerator. It plants disjoint clusters by default, or overlapping ones with --overlap MAX,AVG, and writes the ground truth as a columns file.
rsclusters generate --clusters 5000 --min-size 100 --max-size 300 --min-p 0.3 --max-p 0.6 --global 0.15 --weighted --seed 1 --metis g.metis --csr g --truth truth.colsPlants disjoint clusters by default, or overlapping ones with --overlap MAX,AVG, and writes the ground truth as a columns file. eval scores any rows file, from either implementation, against a graph.
It prints exact pair precision, edge recall, the summed log-likelihood objective, and, given a truth file, the average best-match F1 against the planted communities.
rsclusters eval --graph g --rows result.rows --truth truth.colsA graph is three files that share a prefix. All are little-endian with no header.
| File | Type | Length | Contents |
|---|---|---|---|
<prefix>.offsets |
u64 | n + 1 | Start of each vertex's list. The first entry is 0 and the last is the total entry count. |
<prefix>.nbrs |
u32 | 2m | Neighbor ids, 0-based, strictly ascending within each vertex. |
<prefix>.wts |
f32 | 2m | Edge weights aligned with nbrs. Leave this file out for an unweighted graph. |
Each undirected edge appears in both endpoints' lists, the same as in METIS. The files are memory-mapped, so loading a graph costs page-cache reads and no parsing.
The algorithm, the likelihood, the proposal strategies, the initializations, and the post-processing follow the Java code. The data structures are different, and a few behaviors changed on purpose:
- The graph is a flat CSR with u32 ids. Each vertex's memberships live in a fixed-capacity inline slot array guarded by a per-row sequence lock, and cluster sizes are atomic counters. That makes the default lock-free mode sound in Rust without per-row heap allocations. Strategies with unbounded row length (the two
FractionOfNeighborsvariants andPureRandom) fall back to a small-vector row behind a per-row reader-writer spin lock. WithnoLocking false, the column locks are acquired in ascending order, which avoids deadlock without the global monitor the Java code uses. - Each worker draws from its own seeded xoshiro256++ stream instead of one synchronized
java.util.Random. Runs are repeatable for a fixed seed and thread count, up to the races that lock-free updates allow. - Likelihoods are computed in f64. Java computes the true-negative term in f32, which loses precision once n is in the millions.
- The union size of a multi-column factor is computed from a snapshot of the columns taken at the start of each epoch, corrected with the live per-column sizes. Java keeps a live hash set per column instead. The only case this misses is a vertex that joined two or more of the factor's columns from outside during the same epoch.
MultipleMembershipLikelihoodsorts the proposals it builds from subsets. The Java code proposes them in greedy order, which breaks the sorted-row invariant that its own intersection code relies on, and its same-as-current check compares against a zero-padded array.- Cluster precision sampling returns early for clusters smaller than 2. The Java version loops forever on a singleton.
- When the rows init file has more columns than
K, K grows to fit. Java has an off-by-one there.
cargo test --releaseApache 2.0. See LICENSE and NOTICE.
The original SBF code is copyright 2020 Twitter, Inc.