~ubuntu-branches/ubuntu/utopic/libhac-java/utopic

« back to all changes in this revision

Viewing changes to src/ch/usi/inf/sape/hac/ClusteringMatrixBuilder.java

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2014-04-10 20:59:14 UTC
  • Revision ID: package-import@ubuntu.com-20140410205914-jul0jw261jyyy6nn
Tags: upstream-0.20110510
ImportĀ upstreamĀ versionĀ 0.20110510

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
5
 *
 
6
 * http://www.opensource.org/licenses/bsd-license.php
 
7
 * 
 
8
 * See the COPYRIGHT file distributed with this work for information
 
9
 * regarding copyright ownership.
 
10
 */
 
11
package ch.usi.inf.sape.hac;
 
12
 
 
13
 
 
14
/**
 
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.
 
22
 * 
 
23
 * @author Matthias.Hauswirth@usi.ch
 
24
 */
 
25
public final class ClusteringMatrixBuilder implements ClusteringBuilder {
 
26
 
 
27
    private static final int INVALID = -1;
 
28
    
 
29
    private final int[][] clustering;
 
30
    private int currentStep;
 
31
 
 
32
 
 
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;
 
42
            }
 
43
        }
 
44
        currentStep = 0;
 
45
    }
 
46
 
 
47
    public void merge(final int i, final int j, final double dissimilarity) {
 
48
        final int previousStep = currentStep;
 
49
        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;
 
54
            } else {
 
55
                clustering[currentStep][observation] = previousCluster;
 
56
            }
 
57
        }
 
58
    }
 
59
 
 
60
    public int[][] getClustering() {
 
61
        return clustering;
 
62
    }
 
63
 
 
64
}