com.apporiented.algorithm.clustering.HierarchyBuilder 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.Collection;
import java.util.List;
public class HierarchyBuilder {
private DistanceMap distances;
private List clusters;
public DistanceMap getDistances() {
return distances;
}
public List getClusters() {
return clusters;
}
public HierarchyBuilder(List clusters, DistanceMap distances) {
this.clusters = clusters;
this.distances = distances;
}
public void agglomerate(LinkageStrategy linkageStrategy) {
ClusterPair minDistLink = distances.removeFirst();
if (minDistLink != null) {
clusters.remove(minDistLink.getrCluster());
clusters.remove(minDistLink.getlCluster());
Cluster oldClusterL = minDistLink.getlCluster();
Cluster oldClusterR = minDistLink.getrCluster();
Cluster newCluster = minDistLink.agglomerate(null);
for (Cluster iClust : clusters) {
ClusterPair link1 = findByClusters(iClust, oldClusterL);
ClusterPair link2 = findByClusters(iClust, oldClusterR);
ClusterPair newLinkage = new ClusterPair();
newLinkage.setlCluster(iClust);
newLinkage.setrCluster(newCluster);
Collection distanceValues = new ArrayList();
if (link1 != null) {
Double distVal = link1.getLinkageDistance();
Double weightVal = link1.getOtherCluster(iClust).getWeightValue();
distanceValues.add(new Distance(distVal, weightVal));
distances.remove(link1);
}
if (link2 != null) {
Double distVal = link2.getLinkageDistance();
Double weightVal = link2.getOtherCluster(iClust).getWeightValue();
distanceValues.add(new Distance(distVal, weightVal));
distances.remove(link2);
}
Distance newDistance = linkageStrategy.calculateDistance(distanceValues);
newLinkage.setLinkageDistance(newDistance.getDistance());
distances.add(newLinkage);
}
clusters.add(newCluster);
}
}
private ClusterPair findByClusters(Cluster c1, Cluster c2) {
return distances.findByCodePair(c1, c2);
}
public boolean isTreeComplete() {
return clusters.size() == 1;
}
public Cluster getRootCluster() {
if (!isTreeComplete()) {
throw new RuntimeException("No root available");
}
return clusters.get(0);
}
}