|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Graph<E,V>
The Graph
interface has the functionalities of both a
directed and an undirected graph.
A graph contains two types of elements: E and V. The E's are stored
in the edges and the V's in the vertices. Edges and Vertices are
seen as Position
s, containing E's or V's.
Here is the implementation of a BFS Traversal for a Graph
.
public static Sequence> bfsTraversal(Graph g){ Sequence > buffer = new LinkedSequence >(); Sequence > result = new LinkedSequence >(); Set > bufferedVertices = new HashSet >(); List > vertices = g.vertices(); for(Position v : vertices){ if(! bufferedVertices.contains(v)){ buffer.add(v); bufferedVertices.add(v); while(! buffer.isEmpty()){ Position > pFirst= buffer.first(); v = (Position )buffer.element(pFirst); buffer.delete(pFirst); result.add(v); for(Position v2 : g.adjacentVertices(v,Direction.ALL)){ if(!bufferedVertices.contains(v2) && v2 !=null){ bufferedVertices.add(v2); buffer.add(v2); } } } } } return result; }
Method Summary | |
---|---|
List<Position<V>> |
adjacentVertices(Position<?> vertex,
Direction type)
Returns a list of the vertices adjacent to vertex . |
E |
deleteEdge(Position<?> position)
Deletes the edge position . |
V |
deleteVertex(Position<?> position)
Deletes the vertex position . |
Position<V> |
destination(Position<?> edge)
Returns the destination of the edge edge |
void |
detach(Position<?> edge,
Position<?> vertex)
Separates edge from one of its extremities, the vertex vertex . |
E |
edgeElement(Position<?> edge)
Returns the element contained in edge |
List<E> |
edgeElements()
Returns a list of all elements contained in the edges. |
List<Position<E>> |
edges()
Returns a list of all the edges contained in the graph. |
List<Position<E>> |
edges(Direction type)
Returns a list of all the edges of a desired type. |
List<Position<E>> |
incidentEdges(Position<?> vertex,
Direction type)
Returns a list of the incident edges of the vertex . |
Position<E> |
insertEdge(E element)
Creates a new edge containing the element . |
Position<E> |
insertEdge(E element,
Position<?> origin,
Position<?> destination)
Creates a new edge containing the element . |
Position<V> |
insertVertex(V element)
Creates a new vertex containing the element . |
boolean |
isDirected(Position<?> edge)
Verifies if an edge is directed. |
Position<V> |
opposite(Position<?> edge,
Position<?> vertex)
Returns the vertex that is opposite to the vertex vertex on the edge edge . |
Position<V> |
origin(Position<?> edge)
Returns the origine of the edge edge |
E |
replaceEdge(Position<?> edge,
E element)
Replaces the element contained in the edge edge by element . |
V |
replaceVertex(Position<?> vertex,
V element)
Replaces the element contained in the vertex vertex by element . |
void |
reverse(Position<?> edge)
Exchanges the two extremities of an edge. |
void |
setDestination(Position<?> edge,
Position<?> vertex)
Defines the vertex vertex to be the new destination of the edge edge . |
void |
setDirected(Position<?> edge)
Defines the edge as a directed edge. |
void |
setOrigin(Position<?> edge,
Position<?> vertex)
Defines the vertex vertex to be the new origin of the edge edge . |
void |
setUndirected(Position<?> edge)
Defines the edge as an undirected edge. |
void |
swapEdge(Position<?> edge0,
Position<?> edge1)
Exchanges the content of the two edges edge0 and edge1 . |
void |
swapVertex(Position<?> vertex0,
Position<?> vertex1)
Exchanges the content of the two vertices vertex0 and vertex1 . |
V |
vertexElement(Position<?> vertex)
Returns the element contained in vertex |
List<V> |
vertexElements()
Returns a list of all elements contained in the vertices. |
List<Position<V>> |
vertices()
Returns a list of all the vertices contained in the graph. |
Methods inherited from interface ch.bfh.algo.Container |
---|
delete, element, encloses, insert, positionIterator, replace, swap |
Methods inherited from interface java.util.Collection |
---|
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray |
Method Detail |
---|
Position<V> insertVertex(V element)
element
. This
vertex is then returned (it is a
Position<V>
).
element
- the element to be used as a label for the new vertex.
Position
Position<E> insertEdge(E element)
element
. This
edge is then returned (it is a Position<E>
).
The edge does not have any extremities (adjacent
vertices). They have to be defined afterward.
element
- the element to be used as a label for the new edge.
Position
Position<E> insertEdge(E element, Position<?> origin, Position<?> destination) throws InvalidAccessorException
element
. Which
extremities are the two vertices origin
and
destination
. This edge is then returned (it is a
Position<E>
).
If the graph is directed, the new edge is directed; if we
have an undirected graph, then the new edge is also undirected.
element
- the element to be used as a label for the new edge.origin
- the vertex that will be the origin of the new edgedestination
- the vertex that will become the destination
of the new edge.
Position
InvalidAccessorException
E deleteEdge(Position<?> position) throws InvalidAccessorException
position
. The edge is
automatically removed from the two vertices at its extremities.
position
- the edge to be removed from the graph.
InvalidAccessorException
- if the position is not a
valide edge of this graph.V deleteVertex(Position<?> position) throws InvalidAccessorException
position
. This also removes all
the edges that had position for an extremity.
position
- the vertex to be removed from the graph.
InvalidAccessorException
- if the position is not a
valide vertex of this graph.E replaceEdge(Position<?> edge, E element) throws InvalidAccessorException
edge
by element
.
edge
- the edge whose element will be replaced;element
- the new label of the edge
.
edge
that has just be replaced.
InvalidAccessorException
- if the position is not a
valide edge of this graph.V replaceVertex(Position<?> vertex, V element) throws InvalidAccessorException
vertex
by element
.
vertex
- the vertex whose element will be replaced;element
- the new label of the vertex
.
vertex
that has just be replaced.
InvalidAccessorException
- if the position is not a
valide edge of this graph.void swapEdge(Position<?> edge0, Position<?> edge1) throws InvalidAccessorException
edge0
and edge1
.
edge0
- the first edgeedge1
- the second edge
InvalidAccessorException
- if one of the edges is not a
valide edge of this graph.void swapVertex(Position<?> vertex0, Position<?> vertex1) throws InvalidAccessorException
vertex0
and vertex1
.
vertex0
- the first vertexvertex1
- the second vertex
InvalidAccessorException
- if one of the vertices is not a
valide vertex of this graph.void setOrigin(Position<?> edge, Position<?> vertex) throws InvalidAccessorException
vertex
to be the new origin of the edge edge
.
If the edge is undirected, origin is simply one of the two extremities.
edge
- the edge whose origin is being definedvertex
- the new origin
InvalidAccessorException
- if the vertex or the edge do not belong to the graphvoid setDestination(Position<?> edge, Position<?> vertex) throws InvalidAccessorException
vertex
to be the new destination of the edge edge
.
If the edge is undirected, destination is simply one of the two extremities.
edge
- the edge whose destination is being definedvertex
- the new destination.
InvalidAccessorException
- if the vertex or the edge do not belong to the graphPosition<V> origin(Position<?> edge) throws InvalidAccessorException
edge
edge
- an edge
InvalidAccessorException
- if the vertex or the edge do not belong to the graphPosition<V> destination(Position<?> edge) throws InvalidAccessorException
edge
edge
- an edge
InvalidAccessorException
- if the vertex or the edge do not belong to the graphvoid detach(Position<?> edge, Position<?> vertex) throws InvalidAccessorException
edge
from one of its extremities, the vertex vertex
.
edge
- an edgevertex
- one of the two extremities of edge
InvalidAccessorException
- if the vertex or the edge do
not belong to the graph, or if the edge is not incident to the
vertex.void setDirected(Position<?> edge) throws InvalidAccessorException
edge
as a directed edge.
edge
- an edge
InvalidAccessorException
- if the edge does not belong to
the graphvoid setUndirected(Position<?> edge) throws InvalidAccessorException
edge
as an undirected edge.
edge
- an edge
InvalidAccessorException
- if the edge does not belong to
the graphboolean isDirected(Position<?> edge) throws InvalidAccessorException
edge
- an edge
edge
is directed
InvalidAccessorException
- if the edge does not belong to
the graphvoid reverse(Position<?> edge) throws InvalidAccessorException
edge
- an edge
InvalidAccessorException
- if the edge does not belong to
the graphPosition<V> opposite(Position<?> edge, Position<?> vertex) throws InvalidAccessorException
vertex
on the edge edge
. It
vertex
is the origin of edge
, then it
returns its destination, and it returns its origin if
vertex
is the destination of edge
.
edge
- an edge
InvalidAccessorException
- if the edge does not belong to
the graphList<Position<V>> vertices()
List<Position<E>> edges()
List<Position<E>> edges(Direction type)
type
can have the following values:
Direction.DIRECTED
Direction.UNDIRECTED
Direction.ALL
type
- the type of the edges
List<Position<E>> incidentEdges(Position<?> vertex, Direction type) throws InvalidAccessorException
vertex
. Depending on the value of
type
, the result is different.
type==Direction.IN
the method returns
the in incident edgestype==Direction.OUT
the method returns the
out incident edgestype==Direction.ALL
then the method
returns all the incident edges (including the undirected
ones).
vertex
- the vertextype
- the type of the edges
InvalidAccessorException
List<Position<V>> adjacentVertices(Position<?> vertex, Direction type) throws InvalidAccessorException
vertex
. Depending on the value of
type
, the result is different.
type==Direction.IN
the method returns
the vertices linked to vertex
through an in incident edgetype==Direction.OUT
the method returns the
vertices linked to vertex
through an out incident edgestype==Direction.ALL
then the method
returns all the adjacent vertices.
vertex
- the vertextype
- the type of the edges
InvalidAccessorException
List<V> vertexElements()
List<E> edgeElements()
V vertexElement(Position<?> vertex) throws InvalidAccessorException
vertex
vertex
- the vertex containing the element
InvalidAccessorException
E edgeElement(Position<?> edge) throws InvalidAccessorException
edge
edge
- the edge containing the element
InvalidAccessorException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |