/// <summary> /// Carries out the most cohesive possible merge out of the set of all clusters. Returns true if merged completed (anticohesion is less than the threshold) /// </summary> /// <param name="set"></param> /// <returns></returns> private bool MergeClosestSet(List <CohesiveSet <T> > set, int anticohesionThreshold) { var mergeIndexA = -1; var mergeIndexB = -1; CohesiveSet <T> mergedSet = null; var anticohesion = int.MaxValue; //iterate over every pair to find the one that would be the most cohesive for (var i = 0; i < set.Count; i++) { var setA = set[i]; for (var j = i + 1; j < set.Count; j++) { var setB = set[j]; var mergeSet = setA.Union(setB); var myAnticohesion = mergeSet.Anticohesion; //if I am less anticohesive, ie more cohesive then I am a better candidate if (myAnticohesion < anticohesion) { mergeIndexA = i; mergeIndexB = j; mergedSet = mergeSet; anticohesion = myAnticohesion; } } } if (anticohesion < anticohesionThreshold) { set[mergeIndexA] = mergedSet; set.RemoveAt(mergeIndexB); return(true); } return(false); }
private CohesiveSet(Func <T, T, int> getDistance, CohesiveSet <T> a, CohesiveSet <T> b) { GetDistance = getDistance; Content = new List <T>(); Content.AddRange(a.Content); Content.AddRange(b.Content); FindAndSetCentralElement(); }
/// <summary> /// Returns the union of this set with another set /// </summary> /// <param name="other"></param> /// <returns></returns> public CohesiveSet <T> Union(CohesiveSet <T> other) { return(new CohesiveSet <T>(GetDistance, this, other)); }