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>
public class UndirectedGraphImpl<V,E> extends Object implements UndirectedGraph<V,E>
- Author:
- Geoffroy Jamgotchian
-
-
Constructor Summary
Constructors Constructor Description UndirectedGraphImpl()
-
Method Summary
All Methods Instance Methods Concrete 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 l)
Add aUndirectedGraphListener
to get notified when the graph changes.int
addVertex()
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, Function<V,Boolean> pathComplete, Function<E,Boolean> pathCancelled)
Find all paths from the specified vertex.int
getEdgeCount()
Return the number of edges.E
getEdgeObject(int e)
Return the value attached to the specified edge.List<E>
getEdgeObjects(int v1, int v2)
Return aList
containing the values attached to the edges between the vertices v1 and v2.Stream<E>
getEdgeObjectStream()
Return aStream
to iterate over the values attached to the edges.int[]
getEdges()
Return the indices of the edges.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.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
removeListener(UndirectedGraphListener 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.void
traverse(int v, Traverser traverser)
Traverse the entire graph, starting at the specified vertex v.void
traverse(int v, 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
-
-
-
-
Method Detail
-
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
public V removeVertex(int v)
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
public int addEdge(int v1, int v2, E obj)
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
public E removeEdge(int e)
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
public Iterable<V> 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
public Stream<V> 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
public V getVertexObject(int v)
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
public void setVertexObject(int v, V obj)
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.
-
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
public Iterable<E> 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
public Stream<E> 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
public E getEdgeObject(int e)
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
public List<E> getEdgeObjects(int v1, int v2)
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 void traverse(int v, 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.traverser
- theTraverser
instance to use to know if the traverse should continue or stop.encountered
- the list of traversed vertices.
-
traverse
public void traverse(int v, Traverser traverser)
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, Traverser, boolean[])
.- Specified by:
traverse
in interfaceUndirectedGraph<V,E>
- 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.
-
findAllPaths
public 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.. This method allocates aList
ofTIntArrayList
to store the paths, aBitSet
to store the encountered vertices and callsfindAllPaths(int, Function, Function, 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.
-
addListener
public void addListener(UndirectedGraphListener l)
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
public void removeListener(UndirectedGraphListener l)
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.
-
-