public class Edge extends java.lang.Object implements Serializable
Graph.getEdgeByID(int)
to get the edge of a specific id.
getPredecessor()
to access the vertex where the edge is an outgoing one and use getSuccessor()
to access the vertex where the edge is an incoming one.getPredecessor(Vertex)
/getSuccessor(Vertex)
to be free of how the edges are
created.getWeight()
you can get the weight of this edge if the graph is a weighted one.
Edge
keep in mind that you should override serialize(Serializer)
and deserialize(Serializer)
so that the additional data of the new type can be stored in a graph file.Graph
,
Serializable
Modifier and Type | Field and Description |
---|---|
protected Vertex |
predecessor
the predecessor vertex
|
protected Vertex |
successor
the successor vertex
|
Constructor and Description |
---|
Edge(Vertex predecessor,
Vertex successor)
Creates a new (undirected) edge with a weight of
0.0f . |
Edge(Vertex predecessor,
Vertex successor,
boolean directed)
Creates a new edge with a weight of
0.0f . |
Edge(Vertex predecessor,
Vertex successor,
boolean directed,
float weight)
Creates a new edge.
|
Edge(Vertex predecessor,
Vertex successor,
float weight)
Creates a new (undirected) edge.
|
Modifier and Type | Method and Description |
---|---|
void |
deserialize(Serializer s)
Deserializes (loads) the object data from the given serializer.
|
boolean |
equals(Edge e)
Indicates whether the given edge equals this edge.
|
boolean |
equals(java.lang.Object o)
Checks if the given object equals this edge.
|
boolean |
equalsIgnoreWeight(Edge e)
Indicates whether the given edge equals this edge.
|
protected Graph<? extends Vertex,? extends Edge> |
getGraph()
Gets the corresponding graph of the edge.
|
int |
getID()
Gets a unique identifier of this edge based on the associated graph.
|
int |
getIndex()
Gets the current index of the edge in the list of edges of the associated graph meaning that
graph.getEdge(e.getIndex()) == e . |
Vertex |
getPredecessor()
Gets the predecessor of the edge that means the vertex at which this is an outgoing edge.
|
Vertex |
getPredecessor(Vertex origin)
Gets the predecessor by an origin.
|
Vertex |
getSuccessor()
Gets the successor of the edge that means the vertex at which this is an incoming edge.
|
Vertex |
getSuccessor(Vertex origin)
Gets the successor by an origin.
|
float |
getWeight()
Gets the weight of the edge.
|
boolean |
isDirected()
Indicates if the edge is directed or undirected.
|
boolean |
isLoop()
Indicates if this edge is a loop meaning that the predecessor is equal the successor.
|
void |
serialize(Serializer s)
Serializes (saves) the object data to the given serializer.
|
void |
setDirected(boolean directed)
Sets if the edge is directed or undirected.
|
void |
setWeight(float weight)
Sets the weight of the edge.
|
java.lang.String |
toString()
Returns a string representation of the edge more precisely the weight of the vertex.
|
protected final Vertex predecessor
protected final Vertex successor
public Edge(Vertex predecessor, Vertex successor) throws java.lang.IllegalArgumentException
0.0f
.predecessor
- the predecessor (the vertex at which this is an outgoing edge)successor
- the successor (the vertex at which this is an incoming edge)java.lang.IllegalArgumentException
- public Edge(Vertex predecessor, Vertex successor, boolean directed) throws java.lang.IllegalArgumentException
0.0f
.predecessor
- the predecessor (the vertex at which this is an outgoing edge)successor
- the successor (the vertex at which this is an incoming edge)directed
- flag that indicates whether the edge should be directed (true
) or undirected (false
) (has only an effect in mixed graphs otherwise the type of the edge is predefined)java.lang.IllegalArgumentException
- public Edge(Vertex predecessor, Vertex successor, float weight) throws java.lang.IllegalArgumentException
predecessor
- the predecessor (the vertex at which this is an outgoing edge)successor
- the successor (the vertex at which this is an incoming edge)weight
- the weight of the edgejava.lang.IllegalArgumentException
- public Edge(Vertex predecessor, Vertex successor, boolean directed, float weight) throws java.lang.IllegalArgumentException
Graph
which has an undirected edge type then the type of the edge is automatically changed
to undirected.predecessor
- the predecessor (the vertex at which this is an outgoing edge)successor
- the successor (the vertex at which this is an incoming edge)directed
- flag that indicates whether the edge should be directed (true
) or undirected (false
) (has only an effect in mixed graphs otherwise the type of the edge is predefined)weight
- the weight of the edgejava.lang.IllegalArgumentException
- public final int getID()
e1.getID() == e2.getID()
.
-1
if this edge is not yet associated with a graphpublic final int getIndex()
graph.getEdge(e.getIndex()) == e
.-1
if the edge is not associated with a graphpublic Vertex getPredecessor()
getPredecessor(Vertex)
.getPredecessor(Vertex)
public Vertex getSuccessor()
getSuccessor(Vertex)
.getSuccessor(Vertex)
public Vertex getPredecessor(Vertex origin)
Graph
regardless of
whether the edge is directed or not.
new Edge(v1, v2, 0, false)
. If you want to know the predecessor of v2 it is v1 but if
you want to know the predecessor of v1 by iterating over the incoming edges you cannot invoke
edge.getPredecessor()
because this is v1 itself. Therefore you have to invoke edge.getPredecessor(v1)
.
// iterate over the successors of v1 for(Vertex v1 : vertices) v1.getOutgoingEdge(i).getSuccessor(v1)... // iterate over the predecessors of v2 for(Vertex v2 : vertices) v2.getIncomingEdge(i).getPredecessor(v2)...
origin
- the origin from which the predecessor is considerednull
if the vertex specified as the origin has nothing to do with this edgepublic Vertex getSuccessor(Vertex origin)
Graph
regardless of
whether the edge is directed or not.
new Edge(v1, v2, 0, false)
. If you want to know the successor of v1 it is v2 but if
you want to know the successor of v2 by iterating over the outgoing edges you cannot invoke
edge.getSuccessor()
because this is v2 itself. Therefore you have to invoke edge.getSuccessor(v2)
.
// iterate over the successors of v1 for(Vertex v1 : vertices) v1.getOutgoingEdge(i).getSuccessor(v1)... // iterate over the predecessors of v1 for(Vertex v1 : vertices) v1.getIncomingEdge(i).getPredecessor(v1)...
origin
- the origin from which the successor is considerednull
if the vertex specified as the origin has nothing to do with this edgepublic float getWeight()
public void setWeight(float weight)
weight
- the weightpublic final boolean isDirected()
true
if the edge is directed or false
if the edge is undirectedpublic final void setDirected(boolean directed)
directed
- true
if the edge is directed or false
if the edge is undirectedpublic final boolean isLoop()
true
if the edge is a loop otherwise false
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(Edge e)
e.directed == this.directed && e.predecessor == this.predecessor && e.successor == this.successor && e.weight == this.weight
or if this edge is undirected and e.directed == this.directed && ((e.predecessor == this.predecessor && e.successor == this.successor) || (e.predecessor == this.successor && e.successor == this.predecessor)) && e.weight == this.weight
.e
- the edgetrue
if e equals this edge otherwise false
public final boolean equalsIgnoreWeight(Edge e)
e.directed == this.directed && e.predecessor.equals(this.predecessor) && e.successor.equals(this.successor)
or if this edge is undirected and e.directed == this.directed && ((e.predecessor.equals(this.predecessor) && e.successor.equals(this.successor)) || (e.predecessor.equals(this.successor) && e.successor.equals(this.predecessor)))
.e
- the edgetrue
if e equals this edge otherwise false
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
o
- the objecttrue
if o equals this edge otherwise false
public void serialize(Serializer s)
Serializable
serialize
in interface Serializable
s
- the serializer for the objectpublic void deserialize(Serializer s)
Serializable
deserialize
in interface Serializable
s
- the serializer