user1642888
user1642888

Reputation: 127

How to create a graph image from adjacency list?

I have an adjacency list representation of a graph in a text file. E.g.

0 -> 1, 2
1 -> 
2 -> 1
3 -> 1

I want to create an image for this graph from the input text file, the graph should look like this: enter image description here

Is there any software available in Java or any other language that can help me achieve this?

Upvotes: 0

Views: 1011

Answers (1)

There is a good lib for that, the Universal Network/Graph Framework

here an Example:

enter image description here

  public static void main(String[] args) {
DirectedSparseGraph<String, String> g = new DirectedSparseGraph<>();
g.addVertex("0");
g.addVertex("1");
g.addVertex("2");
g.addVertex("3");

g.addEdge("Edge1", "0", "1");
g.addEdge("Edge2", "0", "2");
g.addEdge("Edge3", "2", "1");
g.addEdge("Edge4", "3", "1");


VisualizationImageServer<String, String> vv = new VisualizationImageServer<>(new CircleLayout<>(g),
    new Dimension(600, 400));

Transformer<String, String> transformer = new Transformer<String, String>() {
    @Override
    public String transform(String arg0) {
    return arg0;
    }
};
vv.getRenderContext().setVertexLabelTransformer(transformer);

JFrame frame = new JFrame("My Graph");
frame.setLocationRelativeTo(null);
frame.getContentPane().add(vv);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

Upvotes: 1

Related Questions