Java Graph (Structure) Deep copy -


well, have class (vertex), contains hashset in it; , @ point, need deep copy element;

i wrote code, doesn't work; i'm working on bug several days , can't fix it... if has enough time read code , find it, i'll grateful. thank in advance.

well, here function:

public vertex getcopy(vertex copyfrom, vertex copyto, hashset<vertex> created){      copyto.setid(copyfrom.getid());     copyto.setcolor(copyfrom.getcolor());     copyto.setcount(copyfrom.getcount());     copyto.setdepth(copyfrom.getdepth());     copyto.setdistance(copyfrom.getdistance());     copyto.setheurist(copyfrom.getheurist());     copyto.setvisited(copyfrom.isvisited());     copyto.setpath(copyfrom.getpath());      created.add(copyto);      hashset<vertex> copytoneighs = new hashset<vertex>();     hashset<vertex> copyfromneighs = new hashset<vertex>();     copyfromneighs.addall(copyfrom.getneighbours());      iterator<vertex> = copyfromneighs.iterator();      while (it.hasnext()){         vertex curr = it.next();          if (!created.contains(curr)){             vertex newone = new vertex();             newone = getcopy(curr, newone, created);             copytoneighs.add(newone);         } else {             iterator<vertex> itr = created.iterator();             while (itr.hasnext()){                 vertex tmp = itr.next();                 if (tmp.equals(curr)){                     copytoneighs.add(tmp);                     break;                 }             }          }     }      copyto.setneighbours(copytoneighs);      return copyto; } 

and want method copy copyfrom copyto. here how call method:

vertex newone = new vertex(); vertex newcurr = new vertex(); hashset<vertex> seen1 = new hashset<vertex>(); hashset<vertex> seen2 = new hashset<vertex>(); newone = newone.getcopy(tmp, newone, seen1); newcurr = newcurr.getcopy(curr, newcurr, seen2); 

other methods (like .getneighbours(), .addneighbours())work properly, i've tested them hundreds of time;

created wrong concept. have set of nodes in "from" graph, , you're creating set of new nodes in "to" graph. created.add(copyto) add new node, "to" graph, set. when go through created.iterator see if node there, you're looking node copyfromneighs, node in "from" graph. looks me never succeed. or else result you'll have nodes in "to" graph pointing nodes in "from" graph.

basically, believe need created hashmap<vertex,vertex>, not set. "key" of hashmap node in "from" graph, , "value" corresponding node in "to" graph. then, when @ neighbors of "from" node, corresponding "to" node (if it's been copied) map, , add neighbors of newly created "to" node.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -