问题描述
我正在尝试在图(邻接矩阵)中找到最大集团,该图最多可包含50个节点。目前,当图形大小达到n = 40左右时,我将永远接受。谁能找到我可以做的任何优化?
public static void maxClique(Graph graph,List<Integer> clique) {
//Get the newest node added to the clique
int currentValue = clique.get(clique.size() - 1);
if (clique.size() > max.size()) {
max = clique;
}
// Check every node
for (int i = 0; i < graph.size; i++) {
// If the node being checked is connected to the current node,and isn't the current node
if (graph.matrix[currentValue][i] == 1 && !clique.contains(i)) {
//Check if the new clique is bigger than the current max.
//Make a new clique and add all the nodes from the current clique to it
ArrayList<Integer> newClique = new ArrayList<>();
newClique.addAll(clique);
//Add the new node
newClique.add(i);
//Repeat
if (makesNewClique(graph,clique,i)) {
maxClique(graph,newClique);
}
}
}
}
全部课程内容:https://pastebin.com/fNPjvgUm 邻接矩阵:https://pastebin.com/yypN9K4L
解决方法
David Eisenstat的评论找到了答案,这是我的代码,供稍后再来的任何人使用:
public void bronKerbosh(Set<Integer> p,Set<Integer> r,Set<Integer> x) {
if (x.isEmpty() && p.isEmpty() && r.size() > max.size()) {
max = new ArrayList<>();
max.addAll(r);
}
Object[] pArr = p.toArray();
for (Object vTemp : pArr) {
int v = (int)vTemp;
Set<Integer> newR = new HashSet<>();
newR.addAll(r);
newR.add(v);
bronKerbosh(intersect(p,neighbors(v)),newR,intersect(x,neighbors(v)));
p.remove(v);
x.add(v);
}
}