Class Edge

  • All Implemented Interfaces:

    public class Edge
    extends Object
    implements Comparable<Edge>
    The Edge class represents a weighted edge in an EdgeWeightedGraph. 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.

    Robert Sedgewick, Kevin Wayne
    • Constructor Detail

      • Edge

        public Edge​(int v,
                    int w,
                    double weight)
        Initializes an edge between vertices v and w of the given weight.
        v - one vertex
        w - the other vertex
        weight - the weight of this edge
        IllegalArgumentException - if either v or w is a negative integer
        IllegalArgumentException - if weight is NaN
    • Method Detail

      • weight

        public double weight()
        Returns the weight of this edge.
        the weight of this edge
      • either

        public int either()
        Returns either endpoint of this edge.
        either endpoint of this edge
      • other

        public int other​(int vertex)
        Returns the endpoint of this edge that is different from the given vertex.
        vertex - one endpoint of this edge
        the other endpoint of this edge
        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 that compareTo() is not consistent with equals(), which uses the reference equality implementation inherited from Object.
        Specified by:
        compareTo in interface Comparable<Edge>
        that - the other edge
        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.
        toString in class Object
        a string representation of this edge
      • main

        public static void main​(String[] args)
        Unit tests the Edge data type.
        args - the command-line arguments