Search code examples
javafor-loopgraphjgrapht

How can I add edges to my graph in a for loop?


I use jgrapht library in Java to create some graphs. I want to iterate through the existing vertices(that I have created in a previous state of the program) and add the corresponding edges depending on some criteria(if statements). As it seems to me the only way to add edges is:

 DefaultWeightedEdge e1 = exampleGraph.addEdge("1", "2");           
 exampleGraph.setEdgeWeight(e1, 20.0);

From my point of view, this won't work in a for loop because it will always refer and change the same object. Any ideas?


Solution

  • for loop
    {
        exampleGraph.setEdgeWeight(exampleGraph.addEdge(x, y), w);
    }
    

    Did you try something like this?