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.

有帮助吗?

解决方案

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);
}

其他提示

I don't know what tutorial that is, but JUNG has no "SimpleGraph" interface.

You can trivially accomplish this yourself, however, via one of these mechanisms:

  1. as you suggested: identify that the added edge is a self-loop and remove it
  2. once the destination vertex is known, only call addEdge() if source != destination
  3. creating a subclass of your preferred graph type that overrides addEdge() and rejects self-loops.

I don't see why you think this is inefficient; any of these checks are O(1).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top