28 June 2008

Visualising jgrapht graphs using yEd

If you need to visualise graphs, such as directed acyclic graphs, then yEd is a nice open-source tools to use. Save the graph to gml as show below. Then open up yEd, you can open it directly from yWorks website using webstart. For DAGs, I've found "hierarchical - interactive" view with "hierarchical - topmost" layout the best way to layout the graph.

import org.jgrapht.DirectedGraph;
import org.jgrapht.ext.GmlExporter;
import org.jgrapht.graph.DefaultDirectedGraph;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
...
DirectedGraph graph = new DefaultDirectedGraph(...);
...
GmlExporter gmlExporter = new GmlExporter();
gmlExporter.setPrintLabels(GmlExporter.PRINT_VERTEX_LABELS);
File file = ...
Writer fileWriter = new BufferedWriter(new FileWriter(file));
gmlExporter.export(fileWriter, graph);