private static void timeNeighborJoining() {
final NeighborJoiningF njf = NeighborJoiningF.createInstance();
- for( int n = 3; n <= 8; ++n ) {
+ for( int n = 3; n <= 9; ++n ) {
final int x = ( int ) Math.pow( 2, n );
final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x );
mt.randomize( new Date().getTime() );
System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" );
}
final NeighborJoining nj = NeighborJoining.createInstance();
- for( int n = 3; n <= 8; ++n ) {
+ for( int n = 3; n <= 9; ++n ) {
final int x = ( int ) Math.pow( 2, n );
final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x );
mt.randomize( new Date().getTime() );
private double[][] _d_values;
private final DecimalFormat _df;
private PhylogenyNode[] _external_nodes;
- private double[][] _m_values;
private int[] _mappings;
private int _n;
private double[] _r;
private final boolean _verbose;
+ private int _min_i;
+ private int _min_j;
private NeighborJoining() {
_verbose = false;
reset( distance );
final Phylogeny phylogeny = new Phylogeny();
while ( _n > 2 ) {
- updateM();
// Calculates the minimal distance.
// If more than one minimal distances, always the first found is used
- // could randomize this, so that any would be returned in a randomized fashion...
- double minimum = _m_values[ 0 ][ 1 ];
- int otu1 = 0;
- int otu2 = 1;
- for( int j = 1; j < _n; ++j ) {
- for( int i = 0; i < j; ++i ) {
- if ( _m_values[ i ][ j ] < minimum ) {
- minimum = _m_values[ i ][ j ];
- otu1 = i;
- otu2 = j;
- }
- }
- }
+ updateM();
+ final int otu1 = _min_i;
+ final int otu2 = _min_j;
// It is a condition that otu1 < otu2.
final PhylogenyNode node = new PhylogenyNode();
final double d = _d_values[ _mappings[ otu1 ] ][ _mappings[ otu2 ] ];
_r = new double[ _n ];
_mappings = new int[ _n ];
_d_values = _d.getValues();
- _m_values = new double[ _n ][ _n ];
initExternalNodes();
}
private final void updateM() {
calculateNetDivergences();
+ Double min = Double.MAX_VALUE;
+ _min_i = -1;
+ _min_j = -1;
final int n_minus_2 = _n - 2;
for( int j = 1; j < _n; ++j ) {
final double r_j = _r[ j ];
final int m_j = _mappings[ j ];
for( int i = 0; i < j; ++i ) {
- _m_values[ i ][ j ] = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 );
+ final double m = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 );
+ if ( m < min ) {
+ min = m;
+ _min_i = i;
+ _min_j = j;
+ }
}
}
}
private float[][] _d_values;
private final DecimalFormat _df;
private PhylogenyNode[] _external_nodes;
- private float[][] _m_values;
private int[] _mappings;
private int _n;
private float[] _r;
private final boolean _verbose;
+ private int _min_i;
+ private int _min_j;
private NeighborJoiningF() {
_verbose = false;
reset( distance );
final Phylogeny phylogeny = new Phylogeny();
while ( _n > 2 ) {
- updateM();
// Calculates the minimal distance.
// If more than one minimal distances, always the first found is used
- // could randomize this, so that any would be returned in a randomized fashion...
- double minimum = _m_values[ 0 ][ 1 ];
- int otu1 = 0;
- int otu2 = 1;
- for( int j = 1; j < _n; ++j ) {
- for( int i = 0; i < j; ++i ) {
- if ( _m_values[ i ][ j ] < minimum ) {
- minimum = _m_values[ i ][ j ];
- otu1 = i;
- otu2 = j;
- }
- }
- }
+ updateM();
+ final int otu1 = _min_i;
+ final int otu2 = _min_j;
// It is a condition that otu1 < otu2.
final PhylogenyNode node = new PhylogenyNode();
final float d = _d_values[ _mappings[ otu1 ] ][ _mappings[ otu2 ] ];
_d_values[ i ][ j ] = ( float ) distances.getValue( i, j );
}
}
- _m_values = new float[ _n ][ _n ];
initExternalNodes();
}
private final void updateM() {
calculateNetDivergences();
final int n_minus_2 = _n - 2;
+ float min = Float.MAX_VALUE;
+ _min_i = -1;
+ _min_j = -1;
for( int j = 1; j < _n; ++j ) {
final float r_j = _r[ j ];
final int m_j = _mappings[ j ];
for( int i = 0; i < j; ++i ) {
- _m_values[ i ][ j ] = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 );
+ final float m = _d_values[ _mappings[ i ] ][ m_j ] - ( ( _r[ i ] + r_j ) / n_minus_2 );
+ if ( m < min ) {
+ min = m;
+ _min_i = i;
+ _min_j = j;
+ }
}
}
}