Wednesday, March 4, 2009

Windows Aero Template Blog

JUNG. The problem with the generation of various graph output for the same input data.

JUNG stands for Java Universal Network / Graph Framework. Library (or framework, as will the authors) is used in general to build graphs and calculate the coordinates of their vertices. JUNG provides a number of predefined types of graphs: directed, nieskierowane acyclic, with parallel edges, trees, etc.
framework itself is very well udokumentowny and uses it in a very intuitive, so I will confine myself to a brief example:


 private void buildGraph () {
/ / I create a graph
SparseGraph SparseGraph s = new ();

/ / Create the vertices of a graph
UndirectedSparseVertex UndirectedSparseVertex node1 = new ();
UndirectedSparseVertex UndirectedSparseVertex node2 = new ();

/ / add vertices to the graph
g.addVertex (node1);
g.addVertex (node1);


/ / I create the edge between two vertices
UndirectedSparseEdge link = new UndirectedSparseEdge (node1, node2);
/ / add edge to graph
g.addEdge (link);

/ / distribution elements of the graph at the district level, measuring 100 x 100
CircleLayout layout = new MyCircleLayout (g);
layout.initialize (new Dimension (100, 100));

/ / informational Displaying coordinates calculated by Jung
System.out.println ("--------------");
System . out.println ("node1.getX ():" + layout.getLocation (node1). getX ());
System.out.println ("node1.getY ():" + layout.getLocation (node1). getY ());
System.out.println ("node2.getX ():" + layout.getLocation (node2). getX ());
System.out.println ("node2.getY ():" + layout. getLocation (node2). getY ());}

Works!

But not quite as I wanted ... pretty soon it became clear that Jung did not always face the same coordinates for the same graph. If the above method was called twice, the result would look like this: --------------

 
node1 (). GetX (): 10.00
node1 (). GetY (): 20.00
node2 (). getX (): 30.00
node2 (). getY (): 40.00

--------------
node1 (). getX (): 30.00
node1 () . getY (): 40.00
node2 (). getX (): 10.00
node2 (). getY (): 20.00


The problem turned out to be the representation of nodes and edges in the object SparseGraph. These items were kept in the collections of HashSet, Thus the order of their collection of the harvest does not always have (and as it turned out it was not), set! Fast, simple and effective solution to the problem turned out to be writing a class extending SparseGraph and overloaded initialize () method. Finally, the working example, as follows:

 private static class extends MyGraph SparseGraph 
protected void {initialize () {

super.initialize ();
/ / LinkedHashSet ensure that items are placed in the file and downloaded it in the same order
LinkedHashSet mVertices = new ();
LinkedHashSet mEdges = new ();

}} private void

buildGraph () {
/ / I create a graph
MyGraph MyGraph s = new ();

//...
/ / continue the same way as in the previous example
}

0 comments:

Post a Comment