Menu

edge weight not changing layout

2008-10-09
2013-05-29
  • David Birchfield

    Hi all,

    Sorry if this is a newbie question, but I haven't been able to find a similar post in the forum.  I am in the process of learning JUNG and the KKLayout, SpringLayout, and FRLayout tools.  I have created a graph by adding nodes in my code to a graph, and I can display it to the visualizer view.  I have created the graph by modifying sample code.  It looks something like this:

    UndirectedSparseGraph g = new UndirectedSparseGraph();
               
               
                StringLabeller sl = StringLabeller.getLabeller(g);
                EdgeWeightLabeller el = EdgeWeightLabeller.getLabeller(g);
           
                double defaultWeight = 0.5;
                double weight = defaultWeight;
               
                int numberOfVertexes = 4;
               
                for (int i = 1; i <= numberOfVertexes; i++) {
                    for (int j = i + 1; j <= numberOfVertexes; j++) {
                        String i1 = "" + i;
                        String i2 = "" + j;
                       
                        if(i == 1 && j == 2){
                            weight = 1.5;
                        }
                        else{
                            weight = defaultWeight;
                        }
                       
                        createEdge(g, sl, el, i1, i2, weight);
                    }
                }

    After creating two vertexes, my create function has this code to set the weight of the edge:

    Edge edge = new UndirectedSparseEdge(v1, v2);
    g.addEdge(edge);
    NumberEdgeValue nev = EdgeWeightLabeller.getLabeller(g);
    nev.setNumber(edge, weight);
    el.setWeight(edge, (int)weight);
    edge_weight.setNumber(edge, (double)weight);

    When the graph is created, my renderer displays that one of the edges is a different weight from the others, so it appears that it is in fact being set.  However, no matter which of the above layout algorithms I try, it doesn't generate anything other than a graph with all equal distances between the vertexes.  I have tried all different weights of varying orders of magnitude.  I get the same visual result if I update the weight of the one edge or not.

    Is there something I'm misunderstanding about what I should expect to see vs. how I'm setting this up?  i.e., this graph and layout shouldn't generate anything except a graph with equal lengths?  Or am I not properly setting the weights or configuring it to use that information in the graph?

    Thanks in advance!

     
    • Joshua O'Madadhain

      David:

      The layout algorithms need to be told about the edge weight.  Because you can encode weights however you like, they don't have any 'magic' that allows them to figure this out by themselves. 

      In addition, not all layout algorithms know how to make use of weights, and (unfortunately) they don't all handle weights in the same way.

      For SpringLayout: look at LengthFunction and associated methods.

      For KKLayout: specify a Distance (e.g. an instance of DijkstraDistance) in the constructor.

      Hope this helps--

      Joshua

       
    • David Birchfield

      Hi Joshua,

      Thanks so much for your feedback, and my apologies for the newbie factor.  I was not previously specifying a distance, so as you suggested I have modified my code with the following constructor where g is and instance of an UndirectedSparseGraph;

      dDistance = new DijkstraDistance(g);
      KKLayout kkLayoutInt = new KKLayout(g, dDistance);

      This feels like it's getting on the right track, but would you mind providing a bit more detail about the next steps?  I am still not seeing an effect of the weights that I am setting and everything pretty much winds up equidistant in the end.

      Is the weight setting routine described above appropriate?

      For further reference this is the result of the g.toString() kkLayout.getStatus() calls with 500 iterations:
      G14033520[V9, V6, V3, V5, V1, V7, V8, V2, V4, V0]
      kk status = Kamada-Kawai V=10(10) IT: 501 E=720922.5334585684java.awt.Dimension[width=900,height=600]

      Thanks again!
      David

       
      • Joshua O'Madadhain

        David:

        You need to tell the DijkstraDistance instance how to get the weight for each edge.  Take a look at the other constructors available.

        Other than that, nothing else should be necessary.

        Joshua

         

Log in to post a comment.