2
* This file is licensed to You under the "Simplified BSD License".
3
* You may not use this software except in compliance with the License.
4
* You may obtain a copy of the License at
6
* http://www.opensource.org/licenses/bsd-license.php
8
* See the COPYRIGHT file distributed with this work for information
9
* regarding copyright ownership.
11
package ch.usi.inf.sape.hac;
15
* A ClusteringMatrixBuilder builds a matrix in which
16
* each row represents a step in the clustering
17
* and each column represents an observation or cluster.
18
* In the first step (row 0), each column represents an observation.
19
* In the last step, each column refers to the same cluster.
20
* Each step represents a copy of the step above,
21
* with two clusters merged into one.
23
* @author Matthias.Hauswirth@usi.ch
25
public final class ClusteringMatrixBuilder implements ClusteringBuilder {
27
private static final int INVALID = -1;
29
private final int[][] clustering;
30
private int currentStep;
33
public ClusteringMatrixBuilder(final int nObservations) {
34
final int nSteps = nObservations;
35
clustering = new int[nSteps][nObservations];
36
for (int observation = 0; observation<nObservations; observation++) {
37
// initialize original step (each observation is its own cluster)
38
clustering[0][observation] = observation;
39
// initialize subsequent steps to "invalid"
40
for (int step = 1; step<nSteps; step++) {
41
clustering[step][observation] = INVALID;
47
public void merge(final int i, final int j, final double dissimilarity) {
48
final int previousStep = currentStep;
50
for (int observation = 0; observation<clustering.length; observation++) {
51
final int previousCluster = clustering[previousStep][observation];
52
if (previousCluster==j) {
53
clustering[currentStep][observation] = i;
55
clustering[currentStep][observation] = previousCluster;
60
public int[][] getClustering() {