create multiple graphs during loop

Cheng
2012-11-19
2013-05-29
  • Cheng
    Cheng
    2012-11-19

    Hello,

    This may be a naive question, but I don't how to deal with my problem. I want to create several networks at one time. Say there are 10 files representing 10 networks, and now I would like to create these 10 networks at a time, I thought a loop may solve this, where in each round of the loop it creates one network and performs the related analysis. However, it seems I can not define multiple graphs with the "UndirectedSparseGraph<MyNode, MyLink>" structure, is there any way that I can do to solve it? Another way I think may work is that I only define one graph, then I can remove all the edges and nodes of one graph at the end of each loop, and add nodes/edges for another graph at the begining of the next loop, but is there any efficient way to clear a graph? Thanks!

     
  • I don't know what problem that you're having with creating multiple instances of UndirectedSparseGraph; it's just a container class like anything in the Collections framework.  Please post more information about the problem you're having.

    If you only need one graph at a time-your question makes this unclear-then there's no reason to create more than one; just create the reference inside the loop where you're reading the graph, and let Java deal with the garbage collection.

     
  • Cheng
    Cheng
    2012-11-20

    hi,
    sorry if I did not made it clear, my purpose is that I want to apply a same analyzing method in 10 different networks, and want a quick way to do that. I thought when I only use one network, e.g. if I create a network g for file1, add all edges and nodes from file1.  After finishing  the analyzing in one loop, then next for file2, should I remove all the edges and nodes in g first, and read file2 to add edges and nodes in file2 for g?   My question is like, how to destroy a network or remove all the elements quickly in one graph if I dont need it anymore? Thanks!

     
  • Just do this:

    for (File file: files) {
      Graph g = createGraphFromFile(file);

      // do stuff with g;
    }

    That's it.  Let Java do the garbage collection after the loop is complete.  You don't need to do anything else.

    FYI, this is not really a JUNG-specific question; this is a question about how to handle resources in Java.  You may want to look around the web for more general Java tutorials; the Java site's tutorial at least used to be pretty good.

    Joshua

     
  • Cheng
    Cheng
    2012-11-20

    Thanks! Joshua, I just thought that the the elements of file1 will still remain in g if I dont do any thing before next loop, I am a new hand on java, sorry for the naive question, but I really appreciate your patient answer!