Search code examples
javajgrapht

Extending SimpleWeightedGraph - JgraphT


I wanted to know about the right way to extend the SimpleWeightedGraph from JgraphT library so that I could use a simplified alias and also include additional functionalities as required.

Instead of creating a new graph each time using

SimpleWeightedGraph<Node, DefaultWeightedEdge> graph 
= new SimpleWeightedGraph<Node, DefaultWeightedEdge>(DefaultWeightedEdge.class);

I created a class

public class CustomGraph extends SimpleWeightedGraph<Node, DefaultWeightedEdge>{    

    public CustomGraph(){
        super(DefaultWeightedEdge.class);
    }
    /*Additional custom methods*/
}

So that I can instantiate using

CustomGraph graph = new CustomGraph();

But this doesn't seem to create the object. Am i missing any other constructor for this?


Solution

  • Ok I figured it out. Here is the class with the edgefactory constructors that had to be included.

    public class CustomGraph extends SimpleWeightedGraph<Node, DefaultWeightedEdge>{
    
        /**
         * Creates a new simple weighted graph with the specified edge factory.
         *
         * @param ef the edge factory of the new graph.
         */
        public CustomGraph(EdgeFactory<Node, DefaultWeightedEdge> ef)
        {
            super(ef);
        }
    
        /**
         * Creates a new simple weighted graph.
         *
         * @param edgeClass class on which to base factory for edges
         */
        public CustomGraph(Class<? extends DefaultWeightedEdge> edgeClass)
        {
            this(new ClassBasedEdgeFactory<Node, DefaultWeightedEdge>(edgeClass));
        }
    
    }