/** * Return the next edge that will maximize the difference between including it and excluding it. */ public Tuple <int, int> getNextEdge() { Tuple <int, int> bestEdgeSoFar = null; double includeBound, excludeBound; double difference, maxDifference = -1D; // Loop through all edges for (int i = 0; i < cm.GetLength(0); i++) { for (int j = 0; j < cm.GetLength(1); j++) { // If this edge isn't eligible (zero cost), skip it. if (cm[i, j] != 0) { continue; } // Ok, this edge is eligible, calculate the inc/exc difference // For inclusion, make all entries in column i and row j infinite cost. // then reduce the matrix, adding to the bound as necessary. // -- If this edge is used, later on in the core algorith we need toremember // to mark edge [j, i] as infinite as well. double[,] tempCm = duplicateCM(cm); tempCm[i, j] = double.PositiveInfinity; tempCm[j, i] = double.PositiveInfinity; for (int t = 0; t < tempCm.GetLength(0); t++) { tempCm[t, j] = double.PositiveInfinity; } for (int t = 0; t < tempCm.GetLength(1); t++) { tempCm[i, t] = double.PositiveInfinity; } includeBound = bound + ProblemAndSolver.reduceCM(ref tempCm); // For exclusion, make the cost of [i, j] infinite, then // b(Se) = b(Sparent) + min(rowi) + min(colj) tempCm = duplicateCM(cm); tempCm[i, j] = double.PositiveInfinity; excludeBound = bound + ProblemAndSolver.reduceCM(ref tempCm); // Calculate the differnce, check to see if this is lower than the lowest so far difference = Math.Abs(excludeBound - includeBound); if (difference > maxDifference) { maxDifference = difference; bestEdgeSoFar = new Tuple <int, int>(i, j); } } } return(bestEdgeSoFar); }