当我使用递归方法解决最大派系问题时,我发现有一个假设,即第一个节点必须位于最大派系中。
如果删除第一个节点与其他原始边缘之间的一个边缘,则会产生此问题。 enter image description here
import java.util.*;
public class LIS {
static int[] x;
static int n;
static int curNoVer;
static int curMaxVer;
static int[] solution;
static boolean[][] matrix;
public static int maxClique(boolean[][] m, int[] v) {
matrix = m;
n = matrix.length;
x = new int[n];
curNoVer = 0;
curMaxVer = 0;
solution = v;
trackback(0);
return curMaxVer;
}
private static void trackback(int i) {
if (i == n) {
// 到达叶结点
for (int j = 0; j < n; j++) {
solution[j] = x[j];
System.out.print(x[j]+" ");
}
System.out.println();
curMaxVer = curNoVer;
return;
}
boolean connected = true;
for (int j = 0; j < i; j++) {
if (x[j] == 1 && !matrix[i][j]) {
connected = false;
break;
}
}
if (connected) {
x[i] = 1;
curNoVer++;
trackback(i + 1);
curNoVer--;
}
if (curNoVer + n - i > curMaxVer) {
x[i] = 0;
trackback(i + 1);
}
}
private static void test() {
boolean[][] matrix = { { false, true, false, true, false },
{ true, false, true, false, true },
{ false, true, false, false, true },
{ true, false, false, false, true },
{ false, true, true, true, false } };
int[] v = new int[matrix.length];
int bestn = maxClique(matrix, v);
System.out.println("bestn is: " + bestn);
System.out.println(Arrays.toString(solution));
}
public static void main(String[] args) {
test();
}
}