开发者

Java: JGraphT: Iterate through nodes

I'm trying to iterate through a开发者_StackOverflow社区ll 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)));            
    }


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.


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);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜