pub struct CorrelationClustering { /* private fields */ }Expand description
Correlation clustering via PIVOT with optional local search refinement.
use clump::cluster::correlation::{CorrelationClustering, SignedEdge};
let edges = vec![
SignedEdge { i: 0, j: 1, weight: 1.0 },
SignedEdge { i: 0, j: 2, weight: -1.0 },
SignedEdge { i: 1, j: 2, weight: -1.0 },
];
let result = CorrelationClustering::new()
.with_seed(42)
.fit(3, &edges)
.unwrap();
assert_eq!(result.labels[0], result.labels[1]);
assert_ne!(result.labels[0], result.labels[2]);Implementations§
Source§impl CorrelationClustering
impl CorrelationClustering
Sourcepub fn new() -> CorrelationClustering
pub fn new() -> CorrelationClustering
Create a new correlation clusterer with default parameters.
Defaults: max_iter = 100, no fixed seed.
Sourcepub fn with_seed(self, seed: u64) -> CorrelationClustering
pub fn with_seed(self, seed: u64) -> CorrelationClustering
Set the random seed for reproducibility.
Sourcepub fn with_max_iter(self, max_iter: usize) -> CorrelationClustering
pub fn with_max_iter(self, max_iter: usize) -> CorrelationClustering
Set the maximum number of local search iterations.
Pass 0 to skip local search and return the raw PIVOT solution.
Sourcepub fn fit(
&self,
n_items: usize,
edges: &[SignedEdge],
) -> Result<CorrelationResult, Error>
pub fn fit( &self, n_items: usize, edges: &[SignedEdge], ) -> Result<CorrelationResult, Error>
Cluster items based on signed pairwise edges.
n_items: total number of items. Items with no edges become singletons.edges: signed pairwise relationships.
Pipeline: precluster (merge almost-cliques) -> contract graph -> PIVOT + merge + local search on contracted graph -> iterated flip on contracted graph -> expand back to original vertices.
Preclustering is inspired by Cohen-Addad et al. (STOC 2024, arXiv 2404.05433). Vertex pairs with a positive edge and Jaccard similarity >= 0.5 over their positive neighborhoods are merged before the main algorithm runs, reducing the effective graph size and giving local search a better starting point.
Returns Error::InvalidParameter if any edge references an item index
>= n_items.
Sourcepub fn edges_from_distances<D>(
data: &[Vec<f32>],
metric: &D,
threshold: f32,
) -> Vec<SignedEdge>where
D: DistanceMetric,
pub fn edges_from_distances<D>(
data: &[Vec<f32>],
metric: &D,
threshold: f32,
) -> Vec<SignedEdge>where
D: DistanceMetric,
Create signed edges from a distance matrix and a threshold.
Pairs closer than threshold get positive weight = threshold - distance.
Pairs farther than threshold get negative weight = threshold - distance
(which is negative). Pairs at exactly threshold get weight 0 and are
included but contribute no cost.
Trait Implementations§
Source§impl Clone for CorrelationClustering
impl Clone for CorrelationClustering
Source§fn clone(&self) -> CorrelationClustering
fn clone(&self) -> CorrelationClustering
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CorrelationClustering
impl Debug for CorrelationClustering
Source§impl Default for CorrelationClustering
impl Default for CorrelationClustering
Source§fn default() -> CorrelationClustering
fn default() -> CorrelationClustering
Auto Trait Implementations§
impl Freeze for CorrelationClustering
impl RefUnwindSafe for CorrelationClustering
impl Send for CorrelationClustering
impl Sync for CorrelationClustering
impl Unpin for CorrelationClustering
impl UnsafeUnpin for CorrelationClustering
impl UnwindSafe for CorrelationClustering
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more