
umcg.genetica.math.matrix.SymmetricByteDistanceMatrix Maven / Gradle / Ivy
/*
* SymmetricByteDistancaMatrix.java
*
* Created on 04 June 2004, 15:53
*/
package umcg.genetica.math.matrix;
/**
*
* Symmetric Byte Distance Matrix: A memory efficient matrix capable of performing calculations on all genes x all genes:
*
* @author Lude Franke
*/
public class SymmetricByteDistanceMatrix {
private int size;
private byte[] matrix;
private final static double eLog10 = java.lang.Math.log(10);
private final static int MAX_ALL_PAIRS_STEPS = 100;
public final static int MAX_VALUE = Byte.MAX_VALUE + 128;
private long[] elementIndex;
public boolean loadSuccess = false;
/** Creates a new instance of SymmetricByteDistancaMatrix
*
* Defines a new Symmetric matrix, with a predefined size
*
* Initially all values will be Byte.MAX_Value (127)
*
* All data is stored in memory efficient one dimensional array, which costs size * (size + 1) / 2 bytes
*
*/
public SymmetricByteDistanceMatrix(int size) {
this.size = size;
long arraySize = ((long) size * (long) (size + 1)) / 2l;
matrix = new byte[(int) arraySize];
elementIndex = new long[size]; for (int x=0; xy) {
return elementIndex[y] + (long) x;
} else {
return elementIndex[x] + (long) y;
}
}
/* Set the value for point (x,y), enter value between 0 and 255:
*/
public void set(int x, int y, int value) {
matrix[(int) getElement(x,y)] = (byte) (value - 128);
}
/* Get the value for point (x,y), returns value between 0 and 255:
*/
public int get(int x, int y) {
return matrix[(int) getElement(x,y)] + 128;
}
/* Get size of matrix:
*/
public int size() {
return size;
}
public int maxValue() {
return Byte.MAX_VALUE + 128;
}
/* Get the shortest path for all the pairs using the Floyd-Warshall algorithm:
*
* Please be aware that this method replaces the values inside the current matrix with the shortest path values!
*/
public void getAllPairsShortestPath() {
//This method uses the Floyd-Warshall all pairs shortest path algorithm:
//As we deal here with a symmetric matrix, the number of required calculations is 50% less.
//Traverse data:
int previousPercentage = 0;
for (int i=0; i0) {
pathPossible = true;
}
}
System.out.println("pathPossible: " + pathPossible);
//Hypothesis: a path does exist between nodeStart and nodeEnd:
boolean pathExists = true;
//Traverse path, do not stop:
boolean traversePath = true;
//Perform Dijkstra algorithm loop, as long as a path is possible, exists and no stop request has been raised:
while (pathPossible && pathExists && traversePath) {
//Set minimal distance T:
int tMin = maxValue();
//Find node v that is nearest to nodeStart:
int v = -1;
//Traverse all nodes:
for (int i=0; i0) {
//Check whether the distance of the edge between the processed node and node v is the lowest:
if (T[0][v] + get(v, i) < T[0][i]) {
//Change the distance and nodeIndex of the i:
T[0][i] = T[0][v] + (int) get(v,i);
T[1][i] = v;
}
}
}
//Check whether destination has been reached:
if (traversed[y]) {
traversePath = false;
} else {
//Check whether the path still between nodeStart and nodeEnd is still likely to exist:
pathExists = false;
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy