Java recursion calling it with objects - How to copy objects?

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; // find vertex with largest degree            
        tempPX.addAll(P); tempPX.addAll(X); // P รขโ€นฦ’ X
        for (Integer v : tempPX) {            
            if (neighbours[v].size() > neighbours[u].size()) {
                u = c; 
            }
            c++;
        } 

        P.removeAll(neighbours[u]); // P \ neighbours[u]
        for (Integer v : newP) {

            newR.add(v); // R รขโ€นฦ’ v

            newP.retainAll(neighbours[v]); // P รขโ€นโ€š neighbours[v]

            newX.retainAll(neighbours[v]); // X รขโ€นโ€š neighbours[v]

            bk(newR, newP, newX); 

            P.remove(v); // removing object
            X.add(v); // X รขโ€นฦ’ 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 {

        // find vertex with largest degree in P U X        
        tempPX.addAll(p); 
        tempPX.addAll(x);
        for (Integer v : tempPX) {            
            if (neighbours[v].size() > neighbours[u].size()) {
                u = v; 
            }
        } 

        p.removeAll(neighbours[u]);  // P \ neighbours[u]
        newP = new ArrayList<Integer>(p); 
        for (Integer v : newP) {

            r.add(v); // R U  v
            newR = new ArrayList<Integer>(r);

            p.retainAll(neighbours[v]);  // P /\ neighbours[v]
            newP = new ArrayList<Integer>(p);

            x.retainAll(neighbours[v]); // X /\ neighbours[v]
            newX = new ArrayList<Integer>(x);

            bk(newR, newP, newX); 

            p.remove(v); // removing object
            x.add(v); // X U v
        }

    }

    return count;
}
+3
3

, , . ArrayList .

.

List<Integer> newList = new ArrayList<Integer>(oldList);

, , .

, List, , , , .

+6

ArrayList newP = P;

ArrayList. arraylist

ArrayList newP = ArrayList (P);

0

&& , X?

, for else , ? , (c) tempPX (v). v , c . .

, .

if (p.isEmpty() && p.isEmpty()) {
  count[r.size()]++;
} else {
  u = 0; c = 0; // find vertex with largest degree
  tempPX.addAll(p); tempPX.addAll(x); // P U X
  for (Integer v : tempPX) {
    if (neighbours[v].size() > neighbours[u].size()) {
      u = c; 
    }
    c++;
  }

- temppx ( c ++, v ) , for :

for (int c = 0; c < neighbours.size(); ++c)

temppx. , .

0
source

Source: https://habr.com/ru/post/1752961/


All Articles