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

« back to all changes in this revision

Viewing changes to src/ch/usi/inf/sape/hac/dendrogram/DendrogramBuilder.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.dendrogram;
 
12
 
 
13
import ch.usi.inf.sape.hac.ClusteringBuilder;
 
14
 
 
15
 
 
16
/**
 
17
 * A DendrogramBuilder creates a Dendrogram consisting of ObservationNodes and
 
18
 * MergeNodes.
 
19
 * 
 
20
 * @author Matthias.Hauswirth@usi.ch
 
21
 */
 
22
public final class DendrogramBuilder implements ClusteringBuilder {
 
23
 
 
24
    private final DendrogramNode[] nodes;
 
25
    private MergeNode lastMergeNode;
 
26
 
 
27
 
 
28
    public DendrogramBuilder(final int nObservations) {
 
29
        nodes = new DendrogramNode[nObservations];
 
30
        for (int i = 0; i<nObservations; i++) {
 
31
            nodes[i] = new ObservationNode(i);
 
32
        }
 
33
    }
 
34
 
 
35
    public final void merge(final int i, final int j, final double dissimilarity) {
 
36
        final MergeNode node = new MergeNode(nodes[i], nodes[j], dissimilarity);
 
37
        nodes[i] = node;
 
38
        lastMergeNode = node;
 
39
    }
 
40
 
 
41
    public final Dendrogram getDendrogram() {
 
42
        if (nodes.length==1) {
 
43
            return new Dendrogram(nodes[0]);
 
44
        } else {
 
45
            return new Dendrogram(lastMergeNode);
 
46
        }
 
47
    }
 
48
 
 
49
}