View Javadoc

1   package baseCode.algorithm.learning.unsupervised.cluster;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   
6   import baseCode.common.Distanceable;
7   
8   /***
9    * <hr>
10   * <p>
11   * Copyright (c) 2004 Columbia University
12   * 
13   * @author pavlidis
14   * @version $Id: AverageLinkageDistancer.java,v 1.3 2004/08/02 15:03:43 pavlidis Exp $
15   */
16  public class AverageLinkageDistancer implements Distancer {
17  
18     /*
19      * (non-Javadoc)
20      * 
21      * @see baseCode.algorithm.learning.unsupervised.cluster.Distance#distance(java.lang.Object, java.lang.Object)
22      */
23     public double distance( Distanceable a, Distanceable b ) {
24  
25        double mean = 0.0;
26        int numComparisons = 0;
27        
28        Collection ac = a.toCollection();
29        Collection bc = b.toCollection();
30        
31        for ( Iterator iter = ac.iterator(); iter.hasNext(); ) {
32           Distanceable elementA = ( Distanceable ) iter.next();
33  
34           for ( Iterator iterator = bc.iterator(); iterator
35                 .hasNext(); ) {
36              Distanceable elementB = ( Distanceable ) iterator.next();
37              mean += elementA.distanceTo( elementB );
38              numComparisons++;
39           }
40  
41        }
42  
43        return mean / numComparisons;
44  
45     }
46  }