Package com.powsybl.math.graph
Class UndirectedGraphImpl<V,E>
java.lang.Object
com.powsybl.math.graph.UndirectedGraphImpl<V,E>
- All Implemented Interfaces:
UndirectedGraph<V,
E>
- Author:
- Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
-
Constructor Summary
-
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.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.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
removeIsolatedVertices
(int v, gnu.trove.list.array.TIntArrayList[] adjacencyList) 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[] startingVertices, 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.boolean
vertexExists
(int v) Check if a specified vertex exists.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.powsybl.math.graph.UndirectedGraph
getMaxVertex
-
Constructor Details
-
UndirectedGraphImpl
public UndirectedGraphImpl(int vertexLimit)
-
-
Method Details
-
addVertex
public int addVertex()Description copied from interface:UndirectedGraph
Create a new vertex and notify theUndirectedGraphListener
s.- Specified by:
addVertex
in interfaceUndirectedGraph<V,
E> - Returns:
- the index of the new vertex.
-
addVertexIfNotPresent
public void addVertexIfNotPresent(int v) Description copied from interface:UndirectedGraph
If the specified vertex does not exist or is null, create it and notify theUndirectedGraphListener
- Specified by:
addVertexIfNotPresent
in interfaceUndirectedGraph<V,
E>
-
vertexExists
public boolean vertexExists(int v) Description copied from interface:UndirectedGraph
Check if a specified vertex exists. This method throws aPowsyblException
if the vertex index is invalid (negative).- Specified by:
vertexExists
in interfaceUndirectedGraph<V,
E>
-
removeVertex
Description copied from interface:UndirectedGraph
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.- Specified by:
removeVertex
in interfaceUndirectedGraph<V,
E> - Parameters:
v
- the vertex index to remove.- Returns:
- the value attached to the vertex.
-
getVertexCount
public int getVertexCount()Description copied from interface:UndirectedGraph
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.- Specified by:
getVertexCount
in interfaceUndirectedGraph<V,
E> - Returns:
- the number of vertices.
-
removeAllVertices
public void removeAllVertices()Description copied from interface:UndirectedGraph
Remove all the vertices of this graph. This method throws aPowsyblException
if edges exist.- Specified by:
removeAllVertices
in interfaceUndirectedGraph<V,
E>
-
addEdge
Description copied from interface:UndirectedGraph
Create an edge between the two specified vertices and notify theUndirectedGraphListener
s. This method throws aPowsyblException
if one of the vertices doesn't exist.- Specified by:
addEdge
in interfaceUndirectedGraph<V,
E> - 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
Description copied from interface:UndirectedGraph
Remove the specified edge and notify theUndirectedGraphListener
s. This method thows aPowsyblException
if the edge doesn't exist.- Specified by:
removeEdge
in interfaceUndirectedGraph<V,
E> - Parameters:
e
- the edge index to remove.- Returns:
- the value attached to the edge.
-
removeAllEdges
public void removeAllEdges()Description copied from interface:UndirectedGraph
Remove all the edges and notify theUndirectedGraphListener
s.- Specified by:
removeAllEdges
in interfaceUndirectedGraph<V,
E>
-
getEdgeCount
public int getEdgeCount()Description copied from interface:UndirectedGraph
Return the number of edges.- Specified by:
getEdgeCount
in interfaceUndirectedGraph<V,
E> - Returns:
- the number of edges.
-
getVertices
public int[] getVertices()Description copied from interface:UndirectedGraph
Return the indices of the vertices.- Specified by:
getVertices
in interfaceUndirectedGraph<V,
E> - Returns:
- the indices of the vertices.
-
getEdges
public int[] getEdges()Description copied from interface:UndirectedGraph
Return the indices of the edges.- Specified by:
getEdges
in interfaceUndirectedGraph<V,
E> - Returns:
- the indices of the edges.
-
getVertexCapacity
public int getVertexCapacity()Description copied from interface:UndirectedGraph
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. UseUndirectedGraph.getVertices()
instead. To get the number of vertices in this graph, useUndirectedGraph.getVertexCount()
.- Specified by:
getVertexCapacity
in interfaceUndirectedGraph<V,
E> - Returns:
- the maximum number of vertices contained in this graph.
-
getVerticesObj
Description copied from interface:UndirectedGraph
Return anIterable
to iterate over the values attached to the vertices.- Specified by:
getVerticesObj
in interfaceUndirectedGraph<V,
E> - Returns:
- an
Iterable
to iterate over the values attached to the vertices.
-
getVertexObjectStream
Description copied from interface:UndirectedGraph
Return aStream
to iterate over the values attached to the vertices.- Specified by:
getVertexObjectStream
in interfaceUndirectedGraph<V,
E> - Returns:
- a
Stream
to iterate over the values attached to the vertices.
-
getVertexObject
Description copied from interface:UndirectedGraph
Return the value attached to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Specified by:
getVertexObject
in interfaceUndirectedGraph<V,
E> - Parameters:
v
- the vertex index- Returns:
- the value attached to the specified vertex.
-
setVertexObject
Description copied from interface:UndirectedGraph
Set the value attached to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Specified by:
setVertexObject
in interfaceUndirectedGraph<V,
E> - Parameters:
v
- the vertex index.obj
- the value to attach to the vertex.
-
getEdgeVertex1
public int getEdgeVertex1(int e) Description copied from interface:UndirectedGraph
Return the index of the first vertex that the specified edge is connected to. This method throws aPowsyblException
if the edge doesn't exist.- Specified by:
getEdgeVertex1
in interfaceUndirectedGraph<V,
E> - Parameters:
e
- the edge index.- Returns:
- the index of the first vertex that the specified edge is connected to.
-
getEdgeObjectsConnectedToVertex
Description copied from interface:UndirectedGraph
Return the edge objects connected to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Specified by:
getEdgeObjectsConnectedToVertex
in interfaceUndirectedGraph<V,
E> - Parameters:
v
- the vertex index.- Returns:
- an iterable of the edge objects
-
getEdgeObjectConnectedToVertexStream
Description copied from interface:UndirectedGraph
Return the edge objects connected to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Specified by:
getEdgeObjectConnectedToVertexStream
in interfaceUndirectedGraph<V,
E> - Parameters:
v
- the vertex index.- Returns:
- a stream of the edge objects
-
getEdgesConnectedToVertex
Description copied from interface:UndirectedGraph
Return the indices of the edges connected to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Specified by:
getEdgesConnectedToVertex
in interfaceUndirectedGraph<V,
E> - Parameters:
v
- the vertex index.- Returns:
- an iterable of the edge indices
-
getEdgeConnectedToVertexStream
Description copied from interface:UndirectedGraph
Return the indices of the edges connected to the specified vertex. This method throws aPowsyblException
if the vertex doesn't exist.- Specified by:
getEdgeConnectedToVertexStream
in interfaceUndirectedGraph<V,
E> - Parameters:
v
- the vertex index.- Returns:
- a stream of the edge indices
-
getEdgeVertex2
public int getEdgeVertex2(int e) Description copied from interface:UndirectedGraph
Return the index of the second vertex that the specified edge is connected to. This method throws aPowsyblException
if the edge doesn't exist.- Specified by:
getEdgeVertex2
in interfaceUndirectedGraph<V,
E> - Parameters:
e
- the edge index.- Returns:
- the index of the second vertex that the specified edge is connected to.
-
getEdgesObject
Description copied from interface:UndirectedGraph
Return anIterable
to iterate over the values attached to the edges.- Specified by:
getEdgesObject
in interfaceUndirectedGraph<V,
E> - Returns:
- an
Iterable
to iterate over the values attached to the edges.
-
getEdgeObjectStream
Description copied from interface:UndirectedGraph
Return aStream
to iterate over the values attached to the edges.- Specified by:
getEdgeObjectStream
in interfaceUndirectedGraph<V,
E> - Returns:
- a
Stream
to iterate over the values attached to the edges.
-
getEdgeObject
Description copied from interface:UndirectedGraph
Return the value attached to the specified edge.- Specified by:
getEdgeObject
in interfaceUndirectedGraph<V,
E> - Parameters:
e
- the index of an edge.- Returns:
- the value attached to the specified edge.
-
getEdgeObjects
Description copied from interface:UndirectedGraph
Return aList
containing the values attached to the edges between the vertices v1 and v2.- Specified by:
getEdgeObjects
in interfaceUndirectedGraph<V,
E> - Returns:
- a
List
containing the values attached to the edges between the vertices v1 and v2.
-
traverse
public boolean traverse(int v, TraversalType traversalType, Traverser traverser, boolean[] encountered) Description copied from interface:UndirectedGraph
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.- Specified by:
traverse
in interfaceUndirectedGraph<V,
E> - 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
Description copied from interface:UndirectedGraph
Traverse the entire graph, starting at the specified vertex v. This method allocates a boolean array and callsUndirectedGraph.traverse(int, TraversalType, Traverser, boolean[])
.- Specified by:
traverse
in interfaceUndirectedGraph<V,
E> - 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
Description copied from interface:UndirectedGraph
Traverse the entire graph, starting at each vertex index of the specified vertices array v. This method allocates a boolean array and callsUndirectedGraph.traverse(int, TraversalType, Traverser, boolean[])
for each entry of the array.- Specified by:
traverse
in interfaceUndirectedGraph<V,
E> - Parameters:
startingVertices
- 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
public 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.This method allocates a
In the output, the paths are sorted by size considering the number of switches in each path.List
ofTIntArrayList
to store the paths, aBitSet
to store the encountered vertices and callsfindAllPaths(int, Predicate, Predicate, TIntArrayList, BitSet, List)
.- Specified by:
findAllPaths
in interfaceUndirectedGraph<V,
E> - 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
public 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.This method allocates a
List
ofTIntArrayList
to store the paths, aBitSet
to store the encountered vertices and callsfindAllPaths(int, Predicate, Predicate, TIntArrayList, BitSet, List)
. In the output, the paths are sorted by using the given comparator.- Specified by:
findAllPaths
in interfaceUndirectedGraph<V,
E> - 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
Description copied from interface:UndirectedGraph
Add aUndirectedGraphListener
to get notified when the graph changes.- Specified by:
addListener
in interfaceUndirectedGraph<V,
E> - Parameters:
l
- the listener to add.
-
removeListener
Description copied from interface:UndirectedGraph
Remove aUndirectedGraphListener
to stop listening the graph changes.- Specified by:
removeListener
in interfaceUndirectedGraph<V,
E> - Parameters:
l
- the listener to remove.
-
print
public void print(PrintStream out, Function<V, String> vertexToString, Function<E, String> edgeToString) Description copied from interface:UndirectedGraph
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.- Specified by:
print
in interfaceUndirectedGraph<V,
E> - 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
public void removeIsolatedVertices(int v, gnu.trove.list.array.TIntArrayList[] adjacencyList) -
removeIsolatedVertices
public void removeIsolatedVertices()Description copied from interface:UndirectedGraph
Remove from the vertices which are not connected to any edge, and which have no associated object.- Specified by:
removeIsolatedVertices
in interfaceUndirectedGraph<V,
E>
-