Skip to content

Make RefineClusterImpl usable for clusters larger than 10 - #54

Merged
philipp94831 merged 6 commits into
masterfrom
fix/clustering
Nov 6, 2026
Merged

philipp94831 merged 6 commits into
masterfrom
fix/clustering

Conversation

@philipp94831

@philipp94831 philipp94831 commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Problem

Increasing maxSmallClusterSize beyond the default of 10 makes refine() appear to hang.

maxSmallClusterSize controlled two things at once:

  1. Full pairwise classification: n(n-1)/2 classifier calls (quadratic, cheap)
  2. Exhaustive search over all clusterings: ClusteringGenerator enumerates every partition of the cluster, i.e. Bell(n) partitions, each scored in O(n²)

The second part is super-exponential:

n partitions (Bell number)
10 115,975
12 4,213,597
15 1,382,958,545
20 51,724,158,235,372

A cluster of 15 records with maxSmallClusterSize(20) takes well over 10¹¹ operations plus billions of short-lived allocations. The javadoc of maxSmallClusterSize only mentioned the quadratic part.

In addition, GreedyClustering (used for big clusters) had two bugs:

  • It iterated over a PriorityQueue with a for-each loop, which does not visit elements in priority order.
  • The comparator was ascending, so even in sorted order the most negative edges would be tried first.

Changes

Separate the limits for classification and exhaustive search

  • New maxExhaustiveClusterSize (default 10): clusters up to this size are solved exactly, as before.
  • Clusters larger than that and up to maxSmallClusterSize are still classified completely, but solved by a heuristic:
    • greedy clustering on the full weight matrix,
    • followed by a local search (move single records to the best other cluster or a new singleton until no move improves the score),
    • plus localSearchRestarts (new, default 5) restarts from random clusterings.
    • The restarts use a fixed seed, so results are deterministic for the same input.
  • Clusters larger than maxSmallClusterSize use the greedy approach on sampled edges, unchanged apart from the fix below.
  • refine() rejects maxExhaustiveClusterSize > 12 (MAX_EXHAUSTIVE_CLUSTER_SIZE) with an IllegalArgumentException.
  • The exhaustive search no longer allocates an array clone and a map entry per partition; it only clones when a better clustering is found.

Fix GreedyClustering

  • Edges are now processed sorted by weight, highest first. Ties keep the input order (stable sort).
  • Extracted a static greedyCluster(double[][], Collection<WeightedEdge>), which the new heuristic reuses.

Behaviour changes

  • Defaults are unchanged: up to 10 records → exact search; above → greedy.
  • Users who set maxSmallClusterSize to 11 or 12 now get the heuristic instead of the exact search for those sizes. Set maxExhaustiveClusterSize(12) to restore the previous behaviour.
  • Results of the big-cluster path may differ because of the greedy ordering fix.

Heuristic quality

Compared against the exact search on random clusters of 6–10 records with planted groups and noisy, partially unknown classifications:

Strategy Matches exact optimum
Greedy only ~67%
Greedy + local search ~86%
Greedy + local search + 5 restarts ~97%

Tests

  • shouldMergeHighestWeightsFirstInGreedyClustering: fails with the previous PriorityQueue iteration
  • shouldApproximateExhaustiveClustering: 200 random weight matrices; the heuristic never beats the exact search and matches it in ≥ 90% of cases
  • shouldFindLocalOptimum: no single-record move improves the local search result
  • shouldRefineMediumClusterQuickly: a 20-record cluster with maxSmallClusterSize(20) is split correctly within a 10-second timeout (takes ~20 ms)
  • shouldRejectTooLargeExhaustiveClusterSize

All existing tests pass unchanged.

Possible follow-up (not part of this PR)

The score in scoreClustering can favour merging records that have no positive connection at all. This happens because the penalty for separating clusters is divided by n - size, which rewards large clusters that are separated from others by negative edges. Splitting clusters into positively connected groups before refining would prevent this, but it changes results, so it should be discussed separately.

@philipp94831 philipp94831 self-assigned this Nov 5, 2026
@philipp94831 philipp94831 changed the title Fix refinement of big clusters Make RefineClusterImpl usable for clusters larger than 10 Nov 5, 2026
@philipp94831
philipp94831 marked this pull request as ready for review November 6, 2026 10:08
tobbber
tobbber previously approved these changes Nov 6, 2026
philipp94831 and others added 2 commits November 6, 2026 15:25
…erImpl.java

Co-authored-by: Tobi <26442427+tobbber@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Nov 6, 2026

Copy link
Copy Markdown

@philipp94831
philipp94831 added this pull request to the merge queue Nov 6, 2026
Merged via the queue into master with commit eb095fc Nov 6, 2026
8 checks passed
@philipp94831
philipp94831 deleted the fix/clustering branch November 6, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants