Package com.powsybl.math.graph
Interface UndirectedGraph<V,E>
- All Known Implementing Classes:
UndirectedGraphImpl
public interface UndirectedGraph<V,E>
An undirected graph implementation.
Note that there is no guarantee that the indices of the vertex or edges are contiguous. To iterate over the vertices
or the edges, it is recommended to use
getVertices()
and getEdges()
.- Author:
- Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
-
Method Summary
Modifier and TypeMethodDescriptionint
Create an edge between the two specified vertices and notify theUndirectedGraphListener
s.void
Add aUndirectedGraphListener
to get notified when the graph changes.int
Create a new vertex and notify theUndirectedGraphListener
s.default void
addVertexIfNotPresent
(int v) If the specified vertex does not exist or is null, create it and notify theUndirectedGraphListener
List<gnu.trove.list.array.TIntArrayList>
findAllPaths
(int from, Predicate<V> pathComplete, Predicate<? super E> pathCancelled) Find all paths from the specified vertex.List<gnu.trove.list.array.TIntArrayList>
findAllPaths
(int from, Predicate<V> pathComplete, Predicate<? super E> pathCancelled, Comparator<gnu.trove.list.array.TIntArrayList> comparator) Find all paths from the specified vertex.Return the indices of the edges connected to the specified vertex.int
Return the number of edges.getEdgeObject
(int e) Return the value attached to the specified edge.Return the edge objects connected to the specified vertex.getEdgeObjects
(int v1, int v2) Return aList
containing the values attached to the edges between the vertices v1 and v2.Return the edge objects connected to the specified vertex.Return aStream
to iterate over the values attached to the edges.int[]
getEdges()
Return the indices of the edges.getEdgesConnectedToVertex
(int v) Return the indices of the edges connected to the specified vertex.Return anIterable
to iterate over the values attached to the edges.int
getEdgeVertex1
(int e) Return the index of the first vertex that the specified edge is connected to.int
getEdgeVertex2
(int e) Return the index of the second vertex that the specified edge is connected to.default int
Deprecated.default int
Return the maximum number of vertices that this graph can contain.int
Return the number of non-null vertices.getVertexObject
(int v) Return the value attached to the specified vertex.Return aStream
to iterate over the values attached to the vertices.int[]
Return the indices of the vertices.Return anIterable
to iterate over the values attached to the vertices.void
Prints the entire graph to the specifiedPrintStream
.void
Remove all the edges and notify theUndirectedGraphListener
s.void
Remove all the vertices of this graph.removeEdge
(int e) Remove the specified edge and notify theUndirectedGraphListener
s.void
Remove from the vertices which are not connected to any edge, and which have no associated object.void
Remove aUndirectedGraphListener
to stop listening the graph changes.removeVertex
(int v) Remove the specified vertex and notify theUndirectedGraphListener
s.void
setVertexObject
(int v, V obj) Set the value attached to the specified vertex.boolean
traverse
(int[] v, TraversalType traversalType, Traverser traverser) Traverse the entire graph, starting at each vertex index of the specified vertices array v.boolean
traverse
(int v, TraversalType traversalType, Traverser traverser) Traverse the entire graph, starting at the specified vertex v.boolean
traverse
(int v, TraversalType traversalType, Traverser traverser, boolean[] encountered) Traverse the entire graph, starting at the specified vertex v.default boolean
vertexExists
(int v) Check if a specified vertex exists.
-
Method Details
-
addVertex
int addVertex()Create a new vertex and notify theUndirectedGraphListener
s.- Returns:
- the index of the new vertex.
-
addVertexIfNotPresent
default void addVertexIfNotPresent(int v) If the specified vertex does not exist or is null, create it and notify theUndirectedGraphListener
-
vertexExists
default boolean vertexExists(int v) Check if a specified vertex exists. This method throws aPowsyblException
if the vertex index is invalid (negative). -
removeVertex
Remove the specified vertex and notify theUndirectedGraphListener
s. This method throws aPowsyblException
if the vertex doesn't exist or if an edge is connected to this vertex.- Parameters:
v
- the vertex index to remove.- Returns:
- the value attached to the vertex.
-
getVertexCount
int getVertexCount()Return the number of non-null vertices. As the contiguity of vertices is not mandatory, the number of vertices can be less than the highest vertex index.- Returns:
- the number of vertices.
-
addEdge
Create an edge between the two specified vertices and notify theUndirectedGraphListener
s. This method throws aPowsyblException
if one of the vertices doesn't exist.- Parameters:
v1
- the first end of the edge.v2
- the second end of the edge.obj
- the value attached to the edge.- Returns:
- the index of the new edge.
-
removeEdge
Remove the specified edge and notify theUndirectedGraphListener
s. This method thows aPowsyblException
if the edge doesn't exist.- Parameters:
e
- the edge index to remove.- Returns:
- the value attached to the edge.
-
removeAllEdges
void removeAllEdges()Remove all the edges and notify theUndirectedGraphListener
s. -
getEdgeCount
int getEdgeCount()Return the number of edges.- Returns:
- the number of edges.
-
getEdges
int[] getEdges()Return the indices of the edges.- Returns:
- the indices of the edges.
-
getVertices
int[] getVertices()Return the indices of the vertices.- Returns:
- the indices of the vertices.
-
getMaxVertex
Deprecated.UsegetVertexCapacity()
instead.Return the maximum number of vertices that this graph can contain. The vertex indices are in the range [0, getVertexCapacity[ As the contiguity of the vertices is not mandatory, do not use this method to iterate over the vertices. UsegetVertices()
instead. To get the number of vertices in this graph, usegetVertexCount()
.- Returns:
- the maximum number of vertices contained in this graph.
-
getVertexCapacity
default int getVertexCapacity()Return the maximum number of vertices that this graph can contain. The vertex indices are in the range [0, getVertexCapacity[ As the contiguity of the vertices is not mandatory, do not use this method to iterate over the vertices. UsegetVertices()
instead. To get the number of vertices in this graph, usegetVertexCount()
.- Returns:
- the maximum number of vertices contained in this graph.
-
getVerticesObj
Return anIterable
to iterate over the values attached to the vertices.- Returns:
- an
Iterable
to iterate over the values attached to the vertices.
-
getVertexObjectStream
Return aStream
to iterate over the values attached to the vertices.- Returns:
- a
Stream
to iterate over the values attached to the vertices.
-
getVertexObject
Return the value attached to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Parameters:
v
- the vertex index- Returns:
- the value attached to the specified vertex.
-
setVertexObject
Set the value attached to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Parameters:
v
- the vertex index.obj
- the value to attach to the vertex.
-
getEdgeVertex1
int getEdgeVertex1(int e) Return the index of the first vertex that the specified edge is connected to. This method throws aPowsyblException
if the edge doesn't exist.- Parameters:
e
- the edge index.- Returns:
- the index of the first vertex that the specified edge is connected to.
-
getEdgeObjectsConnectedToVertex
Return the edge objects connected to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Parameters:
v
- the vertex index.- Returns:
- an iterable of the edge objects
-
getEdgeObjectConnectedToVertexStream
Return the edge objects connected to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Parameters:
v
- the vertex index.- Returns:
- a stream of the edge objects
-
getEdgesConnectedToVertex
Return the indices of the edges connected to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Parameters:
v
- the vertex index.- Returns:
- an iterable of the edge indices
-
getEdgeConnectedToVertexStream
Return the indices of the edges connected to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Parameters:
v
- the vertex index.- Returns:
- a stream of the edge indices
-
getEdgeVertex2
int getEdgeVertex2(int e) Return the index of the second vertex that the specified edge is connected to. This method throws aPowsyblException
if the edge doesn't exist.- Parameters:
e
- the edge index.- Returns:
- the index of the second vertex that the specified edge is connected to.
-
removeAllVertices
void removeAllVertices()Remove all the vertices of this graph. This method throws aPowsyblException
if edges exist. -
getEdgesObject
Return anIterable
to iterate over the values attached to the edges.- Returns:
- an
Iterable
to iterate over the values attached to the edges.
-
getEdgeObjectStream
Return aStream
to iterate over the values attached to the edges.- Returns:
- a
Stream
to iterate over the values attached to the edges.
-
getEdgeObject
Return the value attached to the specified edge.- Parameters:
e
- the index of an edge.- Returns:
- the value attached to the specified edge.
-
getEdgeObjects
Return aList
containing the values attached to the edges between the vertices v1 and v2.- Returns:
- a
List
containing the values attached to the edges between the vertices v1 and v2.
-
traverse
Traverse the entire graph, starting at the specified vertex v. This method relies on aTraverser
instance to know if the traverse of the graph should continue or stop. This method throws aPowsyblException
if the encountered table size is less than the maximum vertex index. At the end of the method, the encountered array contains true for all the traversed vertices, false otherwise.- Parameters:
v
- the vertex index where the traverse has to start.traversalType
- the type of traversal (breadth-first or depth-first)traverser
- theTraverser
instance to use to know if the traverse should continue or stop.encountered
- the list of traversed vertices.- Returns:
- false if the whole traversing has to stop, meaning that a
TraverseResult.TERMINATE_TRAVERSER
has been returned from the traverser, true otherwise
-
traverse
Traverse the entire graph, starting at the specified vertex v. This method allocates a boolean array and callstraverse(int, TraversalType, Traverser, boolean[])
.- Parameters:
v
- the vertex index where the traverse has to start.traversalType
- the type of traversal (breadth-first or depth-first)traverser
- theTraverser
instance to use to know if the traverse should continue or stop.- Returns:
- false if the whole traversing has to stop, meaning that a
TraverseResult.TERMINATE_TRAVERSER
has been returned from the traverser, true otherwise
-
traverse
Traverse the entire graph, starting at each vertex index of the specified vertices array v. This method allocates a boolean array and callstraverse(int, TraversalType, Traverser, boolean[])
for each entry of the array.- Parameters:
v
- the array of vertex indices where the traverse has to start.traversalType
- the type of traversal (breadth-first or depth-first)traverser
- theTraverser
instance to use to know if the traverse should continue or stop.- Returns:
- false if the whole traversing has to stop, meaning that a
TraverseResult.TERMINATE_TRAVERSER
has been returned from the traverser, true otherwise
-
findAllPaths
List<gnu.trove.list.array.TIntArrayList> findAllPaths(int from, Predicate<V> pathComplete, Predicate<? super E> pathCancelled) Find all paths from the specified vertex. This method relies on two functions to stop the traverse when the target vertex is found or when an edge must not be traversed.- Parameters:
from
- the vertex index where the traverse has to start.pathComplete
- a function that returns true when the target vertex is found.pathCancelled
- a function that returns true when the edge must not be traversed.- Returns:
- a list that contains the index of the traversed edges.
-
findAllPaths
List<gnu.trove.list.array.TIntArrayList> findAllPaths(int from, Predicate<V> pathComplete, Predicate<? super E> pathCancelled, Comparator<gnu.trove.list.array.TIntArrayList> comparator) Find all paths from the specified vertex. This method relies on two functions to stop the traverse when the target vertex is found or when an edge must not be traversed.- Parameters:
from
- the vertex index where the traverse has to start.pathComplete
- a function that returns true when the target vertex is found.pathCancelled
- a function that returns true when the edge must not be traversed.comparator
- a comparator used to sort the paths- Returns:
- a list that contains the index of the traversed edges.
-
addListener
Add aUndirectedGraphListener
to get notified when the graph changes.- Parameters:
l
- the listener to add.
-
removeListener
Remove aUndirectedGraphListener
to stop listening the graph changes.- Parameters:
l
- the listener to remove.
-
print
Prints the entire graph to the specifiedPrintStream
. The printing relies on the two specified functions to render the values attached to a vertex or an edge.- Parameters:
out
- the output stream.vertexToString
- the function to use to render the values of vertices.edgeToString
- the function to use to render the values of edges.
-
removeIsolatedVertices
void removeIsolatedVertices()Remove from the vertices which are not connected to any edge, and which have no associated object.
-
getVertexCapacity()
instead.