public ClusterNodeCollection ExecuteClustering(ClusterDistance.Strategy strategy, int k) { nodeCollection.BuildSingletonCluster(leafCollection); dissimilarityMatrix = new DissimilarityMatrix(); foreach (ClusterNodePair clusterPair in GetClusterPairCollection()) { double distanceBetweenTwoClusters = ClusterDistance.ComputeLeafDistance(clusterPair.Cluster1, clusterPair.Cluster2); dissimilarityMatrix.AddClusterPairAndDistance(clusterPair, distanceBetweenTwoClusters); } BuildHierarchicalClustering(nodeCollection.Count, strategy, k); return(nodeCollection); }
// this method compute distance between clusters thas has subclusters (cluster2 represents the new cluster) public static double ComputeNodeDistance(ClusterNode node1, ClusterNode node2, DissimilarityMatrix dissimilarityMatrix, Strategy strategy) { ClusterNode node21 = node2.NodeAt(0); ClusterNode node22 = node2.NodeAt(1); double distance1 = dissimilarityMatrix.ReturnClusterPairDistance(new ClusterNodePair(node1, node21)); double distance2 = dissimilarityMatrix.ReturnClusterPairDistance(new ClusterNodePair(node1, node22)); switch (strategy) { case Strategy.MinLinkage: return(distance1 < distance2 ? distance1 : distance2); case Strategy.MaxLinkage: return(distance1 > distance2 ? distance1 : distance2); case Strategy.AverageLinkage: return((distance1 + distance2) / 2); case Strategy.AverageLinkageWeighted: return(distance1 * node21.TotalLeafsCount / node2.TotalLeafsCount + distance2 * node22.TotalLeafsCount / node2.TotalLeafsCount); default: return(0); } }