Old value / links. Im getting a ConcurrentModificationException for this Bron Kerbosch adaptation.
public int[] bk(ArrayList<Integer> R, ArrayList<Integer> P, ArrayList<Integer> X) {
int count[] = new int[n];
int u=0, c = 0;
ArrayList<Integer> tempPX = new ArrayList<Integer>();
ArrayList<Integer> newP = P;
ArrayList<Integer> newX = X;
ArrayList<Integer> newR = R;
if (P.isEmpty() && X.isEmpty()) {
count[R.size()]++;
} else {
u = 0; c = 0;
tempPX.addAll(P); tempPX.addAll(X);
for (Integer v : tempPX) {
if (neighbours[v].size() > neighbours[u].size()) {
u = c;
}
c++;
}
P.removeAll(neighbours[u]);
for (Integer v : newP) {
newR.add(v);
newP.retainAll(neighbours[v]);
newX.retainAll(neighbours[v]);
bk(newR, newP, newX);
P.remove(v);
X.add(v);
}
}
return count;
}
An exception occurs in the line for (Integer v: newP) and there is a recursive call. I need P.removeAll (neighbors [u]); then loop around to this resulting list, do something in the comments inside, and PASS COPIES in a recursive call so that it doesn't complain or work, don't pass links and continue to modify the same P / X / R object. So, how and WHEN do I copy them? These are the first lines .. I make copies of the links, not me ... (yes, I know that I โmodifyโ newP, then iterate over the old P, they just point to the same object that seems)
--- new code after reading the answers -
public int[] bk(List<Integer> r, List<Integer> p, List<Integer> x) {
int count[] = new int[n];
int u = 1;
List<Integer> tempPX = new ArrayList<Integer>();
List<Integer> newR, newP, newX;
if (p.isEmpty() && x.isEmpty()) {
count[r.size()]++;
} else {
tempPX.addAll(p);
tempPX.addAll(x);
for (Integer v : tempPX) {
if (neighbours[v].size() > neighbours[u].size()) {
u = v;
}
}
p.removeAll(neighbours[u]);
newP = new ArrayList<Integer>(p);
for (Integer v : newP) {
r.add(v);
newR = new ArrayList<Integer>(r);
p.retainAll(neighbours[v]);
newP = new ArrayList<Integer>(p);
x.retainAll(neighbours[v]);
newX = new ArrayList<Integer>(x);
bk(newR, newP, newX);
p.remove(v);
x.add(v);
}
}
return count;
}