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 usegetVertices()
andgetEdges()
.- Author:
- Geoffroy Jamgotchian
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description int
addEdge(int v1, int v2, E obj)
Create an edge between the two specified vertices and notify theUndirectedGraphListener
s.void
addListener(UndirectedGraphListener<V,E> l)
Add aUndirectedGraphListener
to get notified when the graph changes.int
addVertex()
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, Function<V,Boolean> pathComplete, Function<E,Boolean> pathCancelled)
Find all paths from the specified vertex.IntStream
getEdgeConnectedToVertexStream(int v)
Return the indices of the edges connected to the specified vertex.int
getEdgeCount()
Return the number of edges.E
getEdgeObject(int e)
Return the value attached to the specified edge.Stream<E>
getEdgeObjectConnectedToVertexStream(int v)
Return the edge objects connected to the specified vertex.List<E>
getEdgeObjects(int v1, int v2)
Return aList
containing the values attached to the edges between the vertices v1 and v2.List<E>
getEdgeObjectsConnectedToVertex(int v)
Return the edge objects connected to the specified vertex.Stream<E>
getEdgeObjectStream()
Return aStream
to iterate over the values attached to the edges.int[]
getEdges()
Return the indices of the edges.List<Integer>
getEdgesConnectedToVertex(int v)
Return the indices of the edges connected to the specified vertex.Iterable<E>
getEdgesObject()
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
getMaxVertex()
Deprecated.UsegetVertexCapacity()
instead.default int
getVertexCapacity()
Return the maximum number of vertices that this graph can contain.int
getVertexCount()
Return the number of non-null vertices.V
getVertexObject(int v)
Return the value attached to the specified vertex.Stream<V>
getVertexObjectStream()
Return aStream
to iterate over the values attached to the vertices.int[]
getVertices()
Return the indices of the vertices.Iterable<V>
getVerticesObj()
Return anIterable
to iterate over the values attached to the vertices.void
print(PrintStream out, Function<V,String> vertexToString, Function<E,String> edgeToString)
Prints the entire graph to the specifiedPrintStream
.void
removeAllEdges()
Remove all the edges and notify theUndirectedGraphListener
s.void
removeAllVertices()
Remove all the vertices of this graph.E
removeEdge(int e)
Remove the specified edge and notify theUndirectedGraphListener
s.void
removeIsolatedVertices(boolean andAlsoDanglingEdges)
Remove from the vertices which are not connected to any edge, and which have no associated object.void
removeListener(UndirectedGraphListener<V,E> l)
Remove aUndirectedGraphListener
to stop listening the graph changes.V
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, Traverser traverser)
Traverse the entire graph, starting at each vertex index of the specified vertices array v.boolean
traverse(int v, Traverser traverser)
Traverse the entire graph, starting at the specified vertex v.boolean
traverse(int v, 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 Detail
-
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
V removeVertex(int v)
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
int addEdge(int v1, int v2, E obj)
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
E removeEdge(int e)
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 default int 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
Iterable<V> 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
Stream<V> 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
V getVertexObject(int v)
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
void setVertexObject(int v, V obj)
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
List<E> getEdgeObjectsConnectedToVertex(int v)
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
Stream<E> getEdgeObjectConnectedToVertexStream(int v)
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
List<Integer> getEdgesConnectedToVertex(int v)
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
IntStream getEdgeConnectedToVertexStream(int v)
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
Iterable<E> 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
Stream<E> 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
E getEdgeObject(int e)
Return the value attached to the specified edge.- Parameters:
e
- the index of an edge.- Returns:
- the value attached to the specified edge.
-
getEdgeObjects
List<E> getEdgeObjects(int v1, int v2)
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
boolean traverse(int v, Traverser traverser, boolean[] encountered)
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.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
boolean traverse(int v, Traverser traverser)
Traverse the entire graph, starting at the specified vertex v. This method allocates a boolean array and callstraverse(int, Traverser, boolean[])
.- Parameters:
v
- the vertex index where the traverse has to start.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
boolean traverse(int[] v, Traverser traverser)
Traverse the entire graph, starting at each vertex index of the specified vertices array v. This method allocates a boolean array and callstraverse(int, Traverser, boolean[])
for each entry of the array.- Parameters:
v
- the array of vertex indices where the traverse has to start.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, Function<V,Boolean> pathComplete, Function<E,Boolean> 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.
-
addListener
void addListener(UndirectedGraphListener<V,E> l)
Add aUndirectedGraphListener
to get notified when the graph changes.- Parameters:
l
- the listener to add.
-
removeListener
void removeListener(UndirectedGraphListener<V,E> l)
Remove aUndirectedGraphListener
to stop listening the graph changes.- Parameters:
l
- the listener to remove.
-
print
void print(PrintStream out, Function<V,String> vertexToString, Function<E,String> edgeToString)
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(boolean andAlsoDanglingEdges)
Remove from the vertices which are not connected to any edge, and which have no associated object. IfandAlsoDanglingEdges
is true, also remove dangling edges.
-
-