Java: how to represent graphs?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2009-11-15T14:17:27Z Indexed on 2012/12/06 23:04 UTC
Read the original article Hit count: 212

Filed under:
|

I'm implementing some algorithms to teach myself about graphs and how to work with them. What would you recommend is the best way to do that in Java? I was thinking something like this:

public class Vertex {

    private ArrayList<Vertex> outnodes; //Adjacency list. if I wanted to support edge weight, this would be a hash map.

    //methods to manipulate outnodes
}

public class Graph {
    private ArrayList<Vertex> nodes;
    //algorithms on graphs
}

But I basically just made this up. Is there a better way?

Also, I want it to be able to support variations on vanilla graphs like digraphs, weighted edges, multigraphs, etc.

© Stack Overflow or respective owner

Related posts about java

Related posts about graph