Search code examples
javagraphjung

Jung2: how to disable node selfloop


I would like to make a simple graph which is a graph without node's selfloop. In tutorial that is avaiable online it is said that I should use SimpleGraph interface, however it isn't working as it is not found in any jar. Is there something I can do to disable selflooping or should I just for ex. at every mousekey release check if any selfloops are being added and delete such edge which would be highly inefficient.


Solution

  • As said in above point no. 3 your code should look like that:

    public class UndirectedSimpleGraph<V,E> extends UndirectedSparseGraph<V,E> {
    
    public UndirectedSimpleGraph(){
        super();
    }
    
    public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType){
        Pair<V> new_endpoints = getValidatedEndpoints(edge, endpoints);
        if (new_endpoints == null)
            return false;
    
        V v1 = new_endpoints.getFirst();
        V v2 = new_endpoints.getSecond();
    
        if(v1.equals(v2))
            return false;
        else
            return super.addEdge(edge,endpoints,edgeType);
    }