net.scheinerman.graph
Class Edge<E>

java.lang.Object
  extended by net.scheinerman.graph.Edge<E>
All Implemented Interfaces:
java.lang.Comparable<Edge<E>>

public class Edge<E>
extends java.lang.Object
implements java.lang.Comparable<Edge<E>>

This class represents a connection between two vertices. This class keeps track of the two vertices and the weight of this edge which is the distance between the two vertices. This edge can be added to a graph in one of several ways. If two vertices exist already in a Graph a new edge can be made with both vertices passed to the constructor. After this the add() method must be called to add the Edge to both of the vertices. Another way to add an edge is two call the Vertex.addEdgeTo(Vertex) on one vertex to another.

Warning: Calling the Vertex.addEdge(Edge) only adds the edge to the specified Vertex The same method must be called on the other Vertex.

Since:
1.6
Version:
1.0
Author:
Jonah Scheinerman
See Also:
Graph}, {@link Vertex}

Constructor Summary
Edge()
          Creates a new edge with no vertices.
Edge(Vertex<E> end1, Vertex<E> end2)
          Creates a new edge with two instantiated vertices.
 
Method Summary
 void add()
          Adds this edge to both of the ends that it encapsulates.
 int compareTo(Edge<E> e)
          Comparison based on edge weight.
 Vertex<E> getEnd1()
          Returns the first vertex from the edge.
 Vertex<E> getEnd2()
          Returns the second vertex from the edge.
 double getWeight()
          Returns the weight of this edge which is equal to the distance between the two vertices of this edge.
 void setEnd1(Vertex<E> end1)
          Sets a new first vertex for the edge.
 void setEnd2(Vertex<E> end2)
          Sets a new second vertex for the edge.
 java.lang.String toString()
          Returns a string which consists of the numeric values of the two vertices and the weight of the edge.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Edge

public Edge()
Creates a new edge with no vertices.


Edge

public Edge(Vertex<E> end1,
            Vertex<E> end2)
Creates a new edge with two instantiated vertices.

Parameters:
end1 - - the first vertex
end2 - - the second vertex
Method Detail

add

public void add()
Adds this edge to both of the ends that it encapsulates.


getEnd1

public Vertex<E> getEnd1()
Returns the first vertex from the edge.

Returns:
- the first vertex.

getEnd2

public Vertex<E> getEnd2()
Returns the second vertex from the edge.

Returns:
- the second vertex.

setEnd1

public void setEnd1(Vertex<E> end1)
Sets a new first vertex for the edge.

Parameters:
end1 - - the new first vertex

setEnd2

public void setEnd2(Vertex<E> end2)
Sets a new second vertex for the edge.

Parameters:
end1 - - the new second vertex

getWeight

public double getWeight()
Returns the weight of this edge which is equal to the distance between the two vertices of this edge.

Returns:
- the weight of the edge

toString

public java.lang.String toString()
Returns a string which consists of the numeric values of the two vertices and the weight of the edge. The string is in the following form:

(End1Val,End2Val):Weight

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this Edge.

compareTo

public int compareTo(Edge<E> e)
Comparison based on edge weight.

Specified by:
compareTo in interface java.lang.Comparable<Edge<E>>