Repository navigation
Make RefineClusterImpl usable for clusters larger than 10 - #54
Merged
Merged
Conversation
RefineClusterImpl usable for clusters larger than 10
philipp94831
marked this pull request as ready for review
November 6, 2026 10:08
tobbber
previously approved these changes
Nov 6, 2026
…erImpl.java Co-authored-by: Tobi <26442427+tobbber@users.noreply.github.com>
tobbber
approved these changes
Nov 6, 2026
philipp94831
enabled auto-merge
November 6, 2026 14:42
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
Increasing
maxSmallClusterSizebeyond the default of 10 makesrefine()appear to hang.maxSmallClusterSizecontrolled two things at once:n(n-1)/2classifier calls (quadratic, cheap)ClusteringGeneratorenumerates every partition of the cluster, i.e. Bell(n) partitions, each scored in O(n²)The second part is super-exponential:
A cluster of 15 records with
maxSmallClusterSize(20)takes well over 10¹¹ operations plus billions of short-lived allocations. The javadoc ofmaxSmallClusterSizeonly mentioned the quadratic part.In addition,
GreedyClustering(used for big clusters) had two bugs:PriorityQueuewith a for-each loop, which does not visit elements in priority order.Changes
Separate the limits for classification and exhaustive search
maxExhaustiveClusterSize(default10): clusters up to this size are solved exactly, as before.maxSmallClusterSizeare still classified completely, but solved by a heuristic:localSearchRestarts(new, default5) restarts from random clusterings.maxSmallClusterSizeuse the greedy approach on sampled edges, unchanged apart from the fix below.refine()rejectsmaxExhaustiveClusterSize > 12(MAX_EXHAUSTIVE_CLUSTER_SIZE) with anIllegalArgumentException.Fix
GreedyClusteringgreedyCluster(double[][], Collection<WeightedEdge>), which the new heuristic reuses.Behaviour changes
maxSmallClusterSizeto 11 or 12 now get the heuristic instead of the exact search for those sizes. SetmaxExhaustiveClusterSize(12)to restore the previous behaviour.Heuristic quality
Compared against the exact search on random clusters of 6–10 records with planted groups and noisy, partially unknown classifications:
Tests
shouldMergeHighestWeightsFirstInGreedyClustering: fails with the previousPriorityQueueiterationshouldApproximateExhaustiveClustering: 200 random weight matrices; the heuristic never beats the exact search and matches it in ≥ 90% of casesshouldFindLocalOptimum: no single-record move improves the local search resultshouldRefineMediumClusterQuickly: a 20-record cluster withmaxSmallClusterSize(20)is split correctly within a 10-second timeout (takes ~20 ms)shouldRejectTooLargeExhaustiveClusterSizeAll existing tests pass unchanged.
Possible follow-up (not part of this PR)
The score in
scoreClusteringcan favour merging records that have no positive connection at all. This happens because the penalty for separating clusters is divided byn - 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.