protected static int computeCut(Matrix matrix,
DoubleVector rho,
double rhoSum,
DoubleVector matrixRowSums) {
// Compute the conductance of the newly sorted matrix.
DoubleVector x = new DenseVector(matrix.columns());
DoubleVector y = matrixRowSums;
VectorMath.subtract(y, x);
// First compute x and y, which are summations of different cuts of the
// matrix, starting with x being the first row and y being the summation
// of all other rows. While doing this, also compute different
// summations of values in the rho vector using the same cut.
VectorMath.add(x, matrix.getRowVector(0));
double rhoX = rho.get(0);
double rhoY = rhoSum - rho.get(0);
// Compute the dot product between the first possible cut.
double u = VectorMath.dotProduct(x, y);
// Find the current conductance for the cut, assume that this is the
// best so far.
double minConductance = u / Math.min(rhoX, rhoY);
int cutIndex = 0;
// Compute the other possible cuts, ignoring the last cut, which would
// leave no data points in a partition. The cut with the smallest
// conductance is maintained.
for (int i = 1; i < rho.length() - 2; ++i) {
// Compute the new value of u, the denominator for computing the
// conductance.
DoubleVector vector = matrix.getRowVector(i);
double xv = VectorMath.dotProduct(x, vector);
double yv = VectorMath.dotProduct(y, vector);
u = u - xv + yv + 1;
// Shift over vectors from y to x.