In Java, want to create a simple graph (a unweighted, undirected graph containing no graph loops or multiple edges) containing vertexes/edges that are equal.
I have two Java Classes, one for the vertexes:
class Vertex {
private int field;
public Vertex(int field) {
this.field = field;
}
@Override
public boolean equals(Object obj) {
if (obj.getClass().getPackage()
.equals(this.getClass().getPackage())
&& obj.getClass().getName()
.equals(this.getClass().getName())) {
Vertex vertex = (Vertex) obj;
if (this.field == vertex.field)
return true;
}
// Else
return false;
}
@Override
public int hashCode() {
// No need for a perfect hash
return this.field;
}
}
And a class for the edges:
class Edge {
private int field;
public Edge(int field) {
this.field = field;
}
@Override
public boolean equals(Object obj) {
if (obj.getClass().getPackage()
.equals(this.getClass().getPackage())
&& obj.getClass().getName()
.equals(this.getClass().getName())) {
Edge edge = (Edge) obj;
if (this.field == edge.field)
return true;
}
// Else
return false;
}
@Override
public int hashCode() {
// No need for a perfect hash
return this.field;
}
}
I currently use the JGraphT library. But I ran into a IllegalArgumentException: "loops not allowed"
after starting this code:
// Define a EdgeFactory
EdgeFactory<Vertex, Edge> BOND_FACTORY = new EdgeFactory<Vertex, Edge>() {
@Override
public Edge createEdge(Vertex sourceVertex, Vertex targetVertex) {
return new Edge(2);
}
};
// Create the graph
SimpleGraph<Vertex, Edge> graph = new SimpleGraph<Vertex, Edge>(
BOND_FACTORY);
// Create vertexes
Vertex v1 = new Vertex(1);
Vertex v2 = new Vertex(1);
// Add them to the graph
graph.addVertex(v1);
graph.addVertex(v2);
graph.addEdge(v1, v2);
The problem is that i try to add v2
to the graph, but because v1.equals(v2) == true
v2
is never added to the graph. From the JavaDoc of the lib:
Adds the specified vertex to this graph if not already present. More formally, adds the specified vertex, v, to this graph if this graph contains no vertex u such that u.equals(v).
This check is implemented here.
But then, how do I acomplish what I'm trying to do? Is there another implemention in this library that I could use for it, or is changing the equals()
method a good idea?