Question

I'm constructing a graph with JGraphX. Using the following code, it takes an inordinately long time to build compared to similar graph implementations like JGraphT. Why would this be? The vertices are created quickly, but the nested loops that create the edges take a long time, especially when the array sizes are 1000.

Vertex[] verts = new Vertex[1000]; // My own vertex class
mxCell[] cells = new mxCell[1000]; // From com.mxGraph.model.mxCell

// Create graph
mxGraph mx = new mxGraph(); // from com.mxgraph.view.mxGraph

// Create vertices
for(int i = 0; i < verts.length; i++)
{
    verts[i] = new Vertex(Integer.toString(i));
    cells[i] = new mxCell(verts[i]);
    mx.getModel().beginUpdate();
    mx.insertVertex(null, Integer.toString(i), cells[i], 1, 1, 1, 1);
    mx.getModel().endUpdate();
}
System.out.println("Vertices created.");

// Connect graph
Random r = new Random();
for(int j = 0; j < verts.length; j++)
{
    for(int k = 0; k < verts.length; k++)
    {
        if(k != j)
        {
            if(r.nextInt(5) == 0) // Random connections, fairly dense
            {
                mx.getModel().beginUpdate();
                mx.insertEdge(null, Integer.toString(k) + " " + Integer.toString(j), "", cells[j], cells[k]);
                mx.getModel().endUpdate();
            }
        }
    }
}
System.out.println("Finished graph.");
Was it helpful?

Solution

begin and end updates are meant for combining operations into one. Ending an update causes a complete validation of the graph. Here you're wrapping each atomic operation only, they have no effect.

Remove the begin/ends you have an put a begin after you create the graph and the end at the bottom of this code section and try that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top