public class UndirectedGraphImpl<V,E> extends Object implements UndirectedGraph<V,E>
Constructor and Description |
---|
UndirectedGraphImpl() |
Modifier and Type | Method and Description |
---|---|
int |
addEdge(int v1,
int v2,
E obj)
Create an edge between the two specified vertices and notify the
UndirectedGraphListener s. |
void |
addListener(UndirectedGraphListener l)
Add a
UndirectedGraphListener to get notified when the graph changes. |
int |
addVertex()
Create a new vertex and notify the
UndirectedGraphListener s. |
void |
addVertexIfNotPresent(int v)
If the specified vertex does not exist or is null, create it and notify the
UndirectedGraphListener |
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 a
List containing the values attached to the edges between the vertices v1 and v2. |
Stream<E> |
getEdgeObjectStream()
Return a
Stream to iterate over the values attached to the edges. |
int[] |
getEdges()
Return the indices of the edges.
|
Iterable<E> |
getEdgesObject()
Return an
Iterable 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 a
Stream to iterate over the values attached to the vertices. |
int[] |
getVertices()
Return the indices of the vertices.
|
Iterable<V> |
getVerticesObj()
Return an
Iterable 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 specified
PrintStream . |
void |
removeAllEdges()
Remove all the edges and notify the
UndirectedGraphListener s. |
void |
removeAllVertices()
Remove all the vertices of this graph.
|
E |
removeEdge(int e)
Remove the specified edge and notify the
UndirectedGraphListener s. |
void |
removeListener(UndirectedGraphListener l)
Remove a
UndirectedGraphListener to stop listening the graph changes. |
V |
removeVertex(int v)
Remove the specified vertex and notify the
UndirectedGraphListener 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.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getMaxVertex
public int addVertex()
UndirectedGraph
UndirectedGraphListener
s.addVertex
in interface UndirectedGraph<V,E>
public void addVertexIfNotPresent(int v)
UndirectedGraph
UndirectedGraphListener
addVertexIfNotPresent
in interface UndirectedGraph<V,E>
public boolean vertexExists(int v)
UndirectedGraph
PowsyblException
if the vertex index is invalid (negative).vertexExists
in interface UndirectedGraph<V,E>
public V removeVertex(int v)
UndirectedGraph
UndirectedGraphListener
s.
This method throws a PowsyblException
if the vertex doesn't exist or if an edge is connected to this vertex.removeVertex
in interface UndirectedGraph<V,E>
v
- the vertex index to remove.public int getVertexCount()
UndirectedGraph
getVertexCount
in interface UndirectedGraph<V,E>
public void removeAllVertices()
UndirectedGraph
PowsyblException
if edges exist.removeAllVertices
in interface UndirectedGraph<V,E>
public int addEdge(int v1, int v2, E obj)
UndirectedGraph
UndirectedGraphListener
s.
This method throws a PowsyblException
if one of the vertices doesn't exist.addEdge
in interface UndirectedGraph<V,E>
v1
- the first end of the edge.v2
- the second end of the edge.obj
- the value attached to the edge.public E removeEdge(int e)
UndirectedGraph
UndirectedGraphListener
s.
This method thows a PowsyblException
if the edge doesn't exist.removeEdge
in interface UndirectedGraph<V,E>
e
- the edge index to remove.public void removeAllEdges()
UndirectedGraph
UndirectedGraphListener
s.removeAllEdges
in interface UndirectedGraph<V,E>
public int getEdgeCount()
UndirectedGraph
getEdgeCount
in interface UndirectedGraph<V,E>
public int[] getVertices()
UndirectedGraph
getVertices
in interface UndirectedGraph<V,E>
public int[] getEdges()
UndirectedGraph
getEdges
in interface UndirectedGraph<V,E>
public int getVertexCapacity()
UndirectedGraph
UndirectedGraph.getVertices()
instead.
To get the number of vertices in this graph, use UndirectedGraph.getVertexCount()
.getVertexCapacity
in interface UndirectedGraph<V,E>
public Iterable<V> getVerticesObj()
UndirectedGraph
Iterable
to iterate over the values attached to the vertices.getVerticesObj
in interface UndirectedGraph<V,E>
Iterable
to iterate over the values attached to the vertices.public Stream<V> getVertexObjectStream()
UndirectedGraph
Stream
to iterate over the values attached to the vertices.getVertexObjectStream
in interface UndirectedGraph<V,E>
Stream
to iterate over the values attached to the vertices.public V getVertexObject(int v)
UndirectedGraph
PowsyblException
if the vertex doesn't exist.getVertexObject
in interface UndirectedGraph<V,E>
v
- the vertex indexpublic void setVertexObject(int v, V obj)
UndirectedGraph
PowsyblException
if the vertex doesn't exist.setVertexObject
in interface UndirectedGraph<V,E>
v
- the vertex index.obj
- the value to attach to the vertex.public int getEdgeVertex1(int e)
UndirectedGraph
PowsyblException
if the edge doesn't exist.getEdgeVertex1
in interface UndirectedGraph<V,E>
e
- the edge index.public int getEdgeVertex2(int e)
UndirectedGraph
PowsyblException
if the edge doesn't exist.getEdgeVertex2
in interface UndirectedGraph<V,E>
e
- the edge index.public Iterable<E> getEdgesObject()
UndirectedGraph
Iterable
to iterate over the values attached to the edges.getEdgesObject
in interface UndirectedGraph<V,E>
Iterable
to iterate over the values attached to the edges.public Stream<E> getEdgeObjectStream()
UndirectedGraph
Stream
to iterate over the values attached to the edges.getEdgeObjectStream
in interface UndirectedGraph<V,E>
Stream
to iterate over the values attached to the edges.public E getEdgeObject(int e)
UndirectedGraph
getEdgeObject
in interface UndirectedGraph<V,E>
e
- the index of an edge.public List<E> getEdgeObjects(int v1, int v2)
UndirectedGraph
List
containing the values attached to the edges between the vertices v1 and v2.getEdgeObjects
in interface UndirectedGraph<V,E>
List
containing the values attached to the edges between the vertices v1 and v2.public void traverse(int v, Traverser traverser, boolean[] encountered)
UndirectedGraph
Traverser
instance to know if the traverse of the graph should continue or stop.
This method throws a PowsyblException
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.traverse
in interface UndirectedGraph<V,E>
v
- the vertex index where the traverse has to start.traverser
- the Traverser
instance to use to know if the traverse should continue or stop.encountered
- the list of traversed vertices.public void traverse(int v, Traverser traverser)
UndirectedGraph
UndirectedGraph.traverse(int, Traverser, boolean[])
.traverse
in interface UndirectedGraph<V,E>
v
- the vertex index where the traverse has to start.traverser
- the Traverser
instance to use to know if the traverse should continue or stop.public List<gnu.trove.list.array.TIntArrayList> findAllPaths(int from, Function<V,Boolean> pathComplete, Function<E,Boolean> pathCancelled)
List
of TIntArrayList
to store the paths, a BitSet
to store the encountered vertices
and calls findAllPaths(int, Function, Function, TIntArrayList, BitSet, List)
.findAllPaths
in interface UndirectedGraph<V,E>
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.public void addListener(UndirectedGraphListener l)
UndirectedGraph
UndirectedGraphListener
to get notified when the graph changes.addListener
in interface UndirectedGraph<V,E>
l
- the listener to add.public void removeListener(UndirectedGraphListener l)
UndirectedGraph
UndirectedGraphListener
to stop listening the graph changes.removeListener
in interface UndirectedGraph<V,E>
l
- the listener to remove.public void print(PrintStream out, Function<V,String> vertexToString, Function<E,String> edgeToString)
UndirectedGraph
PrintStream
.
The printing relies on the two specified functions to render the values attached to a vertex or an edge.print
in interface UndirectedGraph<V,E>
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.Copyright © 2020. All rights reserved.