![JAR search and dependency download from the Maven repository](/logo.png)
com.apporiented.algorithm.clustering.DefaultClusteringAlgorithm Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hierarchical-clustering Show documentation
Show all versions of hierarchical-clustering Show documentation
Agglomerative hierarchical clustering analysis and visualization implemented in Java
/*******************************************************************************
* Copyright 2013 Lars Behnke
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.apporiented.algorithm.clustering;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
public class DefaultClusteringAlgorithm implements ClusteringAlgorithm {
@Override
public Cluster performClustering(double[][] distances,
String[] clusterNames, LinkageStrategy linkageStrategy) {
checkArguments(distances, clusterNames, linkageStrategy);
/* Setup model */
List clusters = createClusters(clusterNames);
DistanceMap linkages = createLinkages(distances, clusters);
/* Process */
HierarchyBuilder builder = new HierarchyBuilder(clusters, linkages);
while (!builder.isTreeComplete()) {
builder.agglomerate(linkageStrategy);
}
return builder.getRootCluster();
}
private void checkArguments(double[][] distances, String[] clusterNames,
LinkageStrategy linkageStrategy) {
if (distances == null || distances.length == 0
|| distances[0].length != distances.length) {
throw new IllegalArgumentException("Invalid distance matrix");
}
if (distances.length != clusterNames.length) {
throw new IllegalArgumentException("Invalid cluster name array");
}
if (linkageStrategy == null) {
throw new IllegalArgumentException("Undefined linkage strategy");
}
int uniqueCount=new HashSet(Arrays.asList(clusterNames)).size();
if (uniqueCount != clusterNames.length) {
throw new IllegalArgumentException("Duplicate names");
}
}
@Override
public Cluster performWeightedClustering(double[][] distances, String[] clusterNames,
double[] weights, LinkageStrategy linkageStrategy) {
checkArguments(distances, clusterNames, linkageStrategy);
if (weights.length != clusterNames.length) {
throw new IllegalArgumentException("Invalid weights array");
}
/* Setup model */
List clusters = createClusters(clusterNames, weights);
DistanceMap linkages = createLinkages(distances, clusters);
/* Process */
HierarchyBuilder builder = new HierarchyBuilder(clusters, linkages);
while (!builder.isTreeComplete()) {
builder.agglomerate(linkageStrategy);
}
return builder.getRootCluster();
}
private DistanceMap createLinkages(double[][] distances,
List clusters) {
DistanceMap linkages = new DistanceMap();
for (int col = 0; col < clusters.size(); col++) {
for (int row = col + 1; row < clusters.size(); row++) {
ClusterPair link = new ClusterPair();
Cluster lCluster = clusters.get(col);
Cluster rCluster = clusters.get(row);
link.setLinkageDistance(distances[col][row]);
link.setlCluster(lCluster);
link.setrCluster(rCluster);
linkages.add(link);
}
}
return linkages;
}
private List createClusters(String[] clusterNames) {
List clusters = new ArrayList();
for (String clusterName : clusterNames) {
Cluster cluster = new Cluster(clusterName);
clusters.add(cluster);
}
return clusters;
}
private List createClusters(String[] clusterNames, double[] weights) {
List clusters = new ArrayList();
for (int i = 0; i < weights.length; i++) {
Cluster cluster = new Cluster(clusterNames[i]);
cluster.setDistance(new Distance(0.0, weights[i]));
clusters.add(cluster);
}
return clusters;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy