English 中文(简体)
Java: JGraphT: Iterate through nodes
原标题:

I m trying to iterate through all nodes, so I can print them out for graphviz. What is the best way to do that using the JGraphT library?

public static void main(String[] args) {
    UndirectedGraph<String, DefaultEdge> g = new SimpleWeightedGraph<String, DefaultEdge>(DefaultEdge.class);

    String odp = "ODP";
    String cck = "CCK";
    String mfe = "MFE";

    g.addVertex(odp);
    g.addVertex(cck);
    g.addVertex(mfe);

    g.addEdge(odp, cck);
    g.addEdge(odp, mfe);

}

Also, how do I add edge weights?

Edit: This seems to work pretty well. But is there a better way?

    Set<DefaultEdge> edges = g.edgeSet();

    for (DefaultEdge e : edges) {
        gv.addln(String.format(""%s" -> "%s"", g.getEdgeSource(e), g.getEdgeTarget(e)));            
    }
最佳回答

Try using WeightedGraph instead of UndirectedGraph (in answer to your second question about adding weights):

WeightedGraph<String, DefaultEdge> g = new SimpleWeightedGraph<String, DefaultEdge>(DefaultEdge.class);

String odp = "ODP";
String cck = "CCK";
String mfe = "MFE";

g.addVertex(odp);
g.addVertex(cck);
g.addVertex(mfe);

DefaultEdge e1 = g.addEdge(odp, cck);
DefaultEdge e1 = g.addEdge(odp, mfe);

g.setEdgeWeight(e1, 10);
g.setEdgeWeight(e2, 4);
问题回答

you can print all the information of the graph using the function toString() over your graph, for example if you have a graph h, you can do it:

System.out.println(h.toString());

On this way you will see the graph in a line. On the other hand, you can visualize the graph allocating coordinates to the vertexs, for example with your graph:

positionVertexAt(ODP, 130, 40);
positionVertexAt(CCK, 60, 20);
positionVertexAt(MFE, 240, 140);

Problem, that you have to implement some function, you have a example in this link http://kickjava.com/src/org/jgrapht/demo/JGraphAdapterDemo.java.htm.

I think is a little difficult, but you can create the nice graph visualization.

In addition you can use the web http://www.graphviz.org where you can format the information of your graph like you did already and then the program build the graph, like in this example if you type this code (is the code of the example of the web):

digraph finite_state_machine {
rankdir=LR;
size="8,5"
node [shape = doublecircle]; LR_0 LR_3 LR_4 LR_8;
node [shape = circle];
LR_0 -> LR_2 [ label = "SS(B)" ];
LR_0 -> LR_1 [ label = "SS(S)" ];
LR_1 -> LR_3 [ label = "S($end)" ];
LR_2 -> LR_6 [ label = "SS(b)" ];
LR_2 -> LR_5 [ label = "SS(a)" ];
LR_2 -> LR_4 [ label = "S(A)" ];
LR_5 -> LR_7 [ label = "S(b)" ];
LR_5 -> LR_5 [ label = "S(a)" ];
LR_6 -> LR_6 [ label = "S(b)" ];
LR_6 -> LR_5 [ label = "S(a)" ];
LR_7 -> LR_8 [ label = "S(b)" ];
LR_7 -> LR_5 [ label = "S(a)" ];
LR_8 -> LR_6 [ label = "S(b)" ];
LR_8 -> LR_5 [ label = "S(a)" ];

}

the program will build this graph : http://www.graphviz.org/content/fsm

I write to you the web here : http://www.graphviz.org/. I hope I could help you, if I find more information or something easier I will tell you.

Pd: Sorry for my English I hope you can understand all.

Instead of using DefaultEdge in the code given by Aaron, you should actually use DefaultWeightedEdge





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签