tags:

views:

566

answers:

5

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.

+3  A: 

If you need weighted edges and multigraphs, you might want to add another class Edge.

I would also recommend using generics to allow specifying which sub-class of Vertex and Edge are currently used. For example:

public class Graph<V extends Vertex> {
List<V> vertices;
...
}

When it comes to implementing graph algorithms, you could also define interfaces for your graph classes on which the algorithms can operate, so that you can play around with different implementations of the actual graph representation. For example, simple graphs that are well-connected might be better implemented by an adjacency matrix, sparser graphs might be represented by adjacency lists - it all depends...

BTW Building such structures efficiently can be quite challenging, so maybe you could give us some more details on what kind of job you would want to use them for? For more complex tasks I would suggest you have a look at the various Java graph libraries, to get some inspiration.

__roland__
I don't really have any details... my self-teaching goals are pretty open ended. I just want to set up some graphs, find some minimum spanning trees, do some output to graphviz, calculate topological sort, etc.
Rosarch
and can you clarify how you'd use generics?
Rosarch
Ah, thanks for the clarification. If it's just for playing around I think adjacency lists will pretty much do the job... it does not seem to performance-intensive. Hope I clarified the generics part.
__roland__
+2  A: 

Take a look at the http://jung.sourceforge.net/doc/index.html graph library. You can still practice implementing your own algorithms (maybe breadth-first or depth-first search to start), but you don't need to worry about creating the graph structure.

Jeff Storey
+1  A: 

Time ago I had the same problem and did my own implementation. What I suggest you is to implement another class: Edge. Then, a Vertex will have a List of Edge.

public class Edge {
    private Node a, b;
    private directionEnum direction;     // AB, BA or both
    private int weight;
    ...
}

It worked for me. But maybe is so simple. There is this library that maybe can help you if you look into its code: http://jgrapht.sourceforge.net/

Sinuhe
A: 

If your goal is use the graphs in any application there are lots of tools available, you could have a look at - http://www.jfree.org/jfreechart/

Kiru
These aren't the graphs you're looking for, I think.
Carl
yeah no I mean like nodes, weighted edges, stuff like that.
Rosarch
+1  A: 

I'd recommend graphviz highly when you get to the point where you want to render your graphs.

And its companions: take a look at Laszlo Szathmary's GraphViz class, along with notugly.xls.

duffymo
graphviz is legit. Do you recommend anything to make it easier to go from a Java data structure to a graphviz.dot file, and possibly generate the image directly from Java?
Rosarch
Yes, take a look at Laszlo Szathmary's GraphViz class :http://www.vclcomponents.com/Java/Scripts_and_Programs/Miscellaneous/GraphViz_Java_API-info.html, along with notugly.xls: http://www.hokstad.com/making-graphviz-output-pretty-with-xsl.html
duffymo
Laszlo's is good, but it gives me a null pointer exception when I run the demo. Not very encouraging.
Rosarch
I've got a code sample that works fine. You're doing something wrong.
duffymo