Package edu.princeton.cs.algs4
Class Edge
- Object
-
- edu.princeton.cs.algs4.Edge
-
- All Implemented Interfaces:
Comparable<Edge>
public class Edge extends Object implements Comparable<Edge>
TheEdge
class represents a weighted edge in anEdgeWeightedGraph
. Each edge consists of two integers (naming the two vertices) and a real-value weight. The data type provides methods for accessing the two endpoints of the edge and the weight. The natural order for this data type is by ascending order of weight.For additional documentation, see Section 4.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description Edge(int v, int w, double weight)
Initializes an edge between verticesv
andw
of the givenweight
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(Edge that)
Compares two edges by weight.int
either()
Returns either endpoint of this edge.static void
main(String[] args)
Unit tests theEdge
data type.int
other(int vertex)
Returns the endpoint of this edge that is different from the given vertex.String
toString()
Returns a string representation of this edge.double
weight()
Returns the weight of this edge.
-
-
-
Constructor Detail
-
Edge
public Edge(int v, int w, double weight)
Initializes an edge between verticesv
andw
of the givenweight
.- Parameters:
v
- one vertexw
- the other vertexweight
- the weight of this edge- Throws:
IllegalArgumentException
- if eitherv
orw
is a negative integerIllegalArgumentException
- ifweight
isNaN
-
-
Method Detail
-
weight
public double weight()
Returns the weight of this edge.- Returns:
- the weight of this edge
-
either
public int either()
Returns either endpoint of this edge.- Returns:
- either endpoint of this edge
-
other
public int other(int vertex)
Returns the endpoint of this edge that is different from the given vertex.- Parameters:
vertex
- one endpoint of this edge- Returns:
- the other endpoint of this edge
- Throws:
IllegalArgumentException
- if the vertex is not one of the endpoints of this edge
-
compareTo
public int compareTo(Edge that)
Compares two edges by weight. Note thatcompareTo()
is not consistent withequals()
, which uses the reference equality implementation inherited fromObject
.- Specified by:
compareTo
in interfaceComparable<Edge>
- Parameters:
that
- the other edge- Returns:
- a negative integer, zero, or positive integer depending on whether the weight of this is less than, equal to, or greater than the argument edge
-
toString
public String toString()
Returns a string representation of this edge.
-
main
public static void main(String[] args)
Unit tests theEdge
data type.- Parameters:
args
- the command-line arguments
-
-