publicclass Edge implements Comparable<Edge>
{
private final int v; // one vertexprivate final int w; // the other vertexprivate final double weight; // edge weightpublicEdge(int v,int w,double weight){
if (v < 0) thrownew IllegalArgumentException("vertex index must be a nonnegative integer");
if (w < 0) thrownew IllegalArgumentException("vertex index must be a nonnegative integer");
if (Double.isNaN(weight)) thrownew IllegalArgumentException("Weight is NaN");
this.v = v;
this.w = w;
this.weight = weight;
}
/*
return the weight of an edge
*/publicdoubleweight()
{
returnthis.weight;
}
/*
return either of the vertex
*/publicinteither(){
return v;
}
/*
return the other vertex
*/publicintother(int vertex){
if (vertex == v) return w;
elseif (vertex == w) return v;
elsethrownew RuntimeException("Inconsistent edge");
}
/*
Compares two edges by weight
*/publicintcompareTo(Edge that){
return Double.compare(this.weight, that.weight);
}
/*
return a string representation of this edge
*/public String toString(){
return String.format("%d-%d %.5f", v, w, weight);
}
}