Looking at equals() and compareTo(), we take only distance and number of edges into consideration. We don’t look at the vertex id.
If we had a graph
a->b, 1
a->c, 1
a->d, 1
only one of b,c or d could be added to the set, because they are considered equal.
Or when we update the distance with a better distance:
nodeSet.remove(v.vertex); v.vertex.pred = u; v.vertex.distanceWithFewestEdges = new DistanceWithFewestEdges(vDistance, vNumEdges); nodeSet.add(v.vertex);
we should encounter the same problem: add() would override some other vertex that has the same distance and hops.
I think we should also add vertex id to compareTo() and equals() in case both distance and hops are equal. Something like this:
@Override
public int compareTo(Vertex other) {
if (this.distance != other.distance) {
return Integer.compare(this.distance, other.distance);
}
if (this.hops != other.hops) {
return Integer.compare(this.hops, other.hops);
}
return this.id.compareTo(other.id);
}
@Override
public boolean equals(Object o) {
Vertex other = (Vertex) o;
return this == other || this.id.equals(other.id) && this.distance == other.distance && this.hops == other.hops;
}