Class Edge

  • All Implemented Interfaces:
    Comparable<Edge>

    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.

    Author:
    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.
        Parameters:
        v - one vertex
        w - the other vertex
        weight - the weight of this edge
        Throws:
        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.
        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 that compareTo() is not consistent with equals(), which uses the reference equality implementation inherited from Object.
        Specified by:
        compareTo in interface Comparable<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.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this edge
      • main

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