CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    Graph design in Java

    Hi, there!

    I need to implement a general purpose graph and I've stuck with OOP design.

    The graph is very general, just vertices and edges. Also I'll need to iterate through vertices and edges. Both must allow to store some info on them.

    So what is the proper design of a class hierarchy in my case?

    What is a better way to store information on vertices and edges: inheritance or a field?

    Does it make any sense to use inner classes (hope this is a proper name) like this:
    Code:
    class Graph
    {
        public class Vertex { ... }
        public class Edge { ... }
    }
    ?

    Thanks in advance!

  2. #2
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Graph design in Java

    Probably the most adequate solution in this case would be http://jgrapht.sourceforge.net/

    Examples taken from the JGraphT:
    PHP Code:
    private static DirectedGraph<URLDefaultEdgecreateHrefGraph()
        {
            
    DirectedGraph<URLDefaultEdge=
                new 
    DefaultDirectedGraph<URLDefaultEdge>(DefaultEdge.class);

            try {
                
    URL amazon = new URL("http://www.amazon.com");
                
    URL yahoo = new URL("http://www.yahoo.com");
                
    URL **** = new URL("http://www.****.com");

                
    // add the vertices
                
    g.addVertex(amazon);
                
    g.addVertex(yahoo);
                
    g.addVertex(****);

                
    // add edges to create linking structure
                
    g.addEdge(yahooamazon);
                
    g.addEdge(yahoo, ****);
            } catch (
    MalformedURLException e) {
                
    e.printStackTrace();
            }

            return 
    g;
        } 
    PHP Code:
    private static UndirectedGraph<StringDefaultEdgecreateStringGraph()
        {
            
    UndirectedGraph<StringDefaultEdge=
                new 
    SimpleGraph<StringDefaultEdge>(DefaultEdge.class);

            
    String v1 "v1";
            
    String v2 "v2";
            
    String v3 "v3";
            
    String v4 "v4";

            
    // add the vertices
            
    g.addVertex(v1);
            
    g.addVertex(v2);
            
    g.addVertex(v3);
            
    g.addVertex(v4);

            
    // add edges to create a circuit
            
    g.addEdge(v1v2);
            
    g.addEdge(v2v3);
            
    g.addEdge(v3v4);
            
    g.addEdge(v4v1);

            return 
    g;
        } 
    And if you still don't want to implement it just use hashmaps for lists and beans for the actual objects.
    Last edited by Xeel; April 19th, 2010 at 04:04 PM.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured