Java: JGraphT: Iterate through nodes
Posted
by Rosarch
on Stack Overflow
See other posts from Stack Overflow
or by Rosarch
Published on 2009-11-17T01:34:14Z
Indexed on
2010/03/13
6:45 UTC
Read the original article
Hit count: 207
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)));
}
© Stack Overflow or respective owner