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.
Asked
Active
Viewed 147 times
2 Answers
1
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);
}

NotJustANumber
- 40
- 5
0
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:
- as you suggested: identify that the added edge is a self-loop and remove it
- once the destination vertex is known, only call addEdge() if source != destination
- 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).

Joshua O'Madadhain
- 2,704
- 1
- 14
- 18