public class Edge implements Comparable<Edge>
{
private final int v; // one vertex
private final int w; // the other vertex
private final double weight; // edge weight
public Edge(int v,int w,double weight){
if (v < 0) throw new IllegalArgumentException("vertex index must be a nonnegative integer");
if (w < 0) throw new IllegalArgumentException("vertex index must be a nonnegative integer");
if (Double.isNaN(weight)) throw new IllegalArgumentException("Weight is NaN");
this.v = v;
this.w = w;
this.weight = weight;
}
/*
return the weight of an edge
*/
public double weight()
{
return this.weight;
}
/*
return either of the vertex
*/
public int either(){
return v;
}
/*
return the other vertex
*/
public int other(int vertex){
if (vertex == v) return w;
else if (vertex == w) return v;
else throw new RuntimeException("Inconsistent edge");
}
/*
Compares two edges by weight
*/
public int compareTo(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);
}
}
Edge
最新推荐文章于 2022-07-16 05:12:03 发布