V - The Object that represent a vertexW - The Object that represent the edge (more specifically the weight of the edge)public interface Graph<V,W extends java.lang.Number>
extends java.lang.Iterable<V>
| Modifier and Type | Field and Description |
|---|---|
static com.google.gson.Gson |
GSON |
static java.lang.String |
NOT_CONNECTED |
static java.lang.String |
NOT_DAG |
static java.lang.String |
PARAM_NULL |
static java.lang.String |
VERTEX_NOT_CONTAINED |
| Modifier and Type | Method and Description |
|---|---|
void |
addAllEdges(java.util.Collection<Edge<V,W>> edges)
Add all the edges of the collection to the graph.
If one of the two, or both vertices aren't contained in the graph, then the vertices will be added. Any null edges will be ignored. This method will overwrite any existing edge between the two vertex. |
void |
addAllVertices(java.util.Collection<V> vertices)
Add all the vertices contained in the collection to the graph.
If a vertex is contained in the collection and in the graph is ignored and it will not be replaced. Null vertices will be ignored and they will not be added to the graph. |
W |
addEdge(Edge<V,W> edge)
Add an edge between the two vertex.
The edge will be created from the vertex source of the edge and the vertex destination of it This method will overwrite any existing edge between the two vertex. If there was a previous edge then it is returned |
W |
addEdge(V vertex1,
V vertex2,
W weight)
Add an edge between the two vertex.
The edge will be created from the vertex V1 and the vertex V2 This method will overwrite any existing edge between the two vertex. If there was a previous edge then it is returned |
W |
addEdgeAndVertices(Edge<V,W> edge)
This particular function add an edge to the graph.
If one of the two, or both vertices of the edge aren't contained in the graph, then the vertices will be added. The edge will be created from the vertex source of the edge and the vertex destination of it This method will overwrite any existing edge between the two vertex. If there was a previous edge then it is returned |
W |
addEdgeAndVertices(V vertex1,
V vertex2,
W weight)
This particular function add an edge to the graph.
If one of the two, or both vertices aren't contained in the graph, then the vertices will be added. The edge will be created from the vertex V1 and the vertex V2 This method will overwrite any existing edge between the two vertex. If there was a previous edge then it is returned |
void |
addVertex(V vertex)
Add the vertex to the graph.
|
boolean |
addVertexIfAbsent(V vertex)
Add the specified vertex to the graph only if the graph doesn't contains it.
The graph contains a vertex only if the method contains(Object) returns true. |
boolean |
contains(V vertex)
Check if the vertex passed is contained in the graph or not.
The vertex V1 is contained in the graph G, if and only if: exist V2 in G such that V2.equals(V1) |
boolean |
containsEdge(V vertex1,
V vertex2)
Check if the edge between the two vertex passed is contained in the graph or not.
An edge between V1 and V2 is contained in the graph if and only if i can travel from V1 to V2. If one of the two vertices is not contained in the graph, then even the edge isn't |
int |
degree(V vertex)
Tells the degree of a vertex.
The degree of a vertex is the quantity of edges that have. Basically, it'll count how many edge it have. |
int |
degreeIn(V vertex)
Tells the degree of all the edges that goes to this vertex.
Basically, it'll count how many edge towards himself it have. |
int |
degreeOut(V vertex)
Tells the degree of all the edges that goes form this vertex to others.
Basically, it'll count how many edge towards any other vertex it have. |
java.util.Map<V,java.util.List<Edge<V,W>>> |
distance(V source)
Get the minimum path from the source vertex to all the possible reachable vertices.
|
java.util.List<Edge<V,W>> |
distance(V source,
V destination)
Get the minimum path from the source vertex to the destination vertex.
If the source vertex can't reach the destination, then an exception is thrown. |
java.util.Collection<Edge<V,W>> |
edges()
Get all the edges in the graph.
If the graph doesn't contains edges, it'll return an empty collection. Note: depending on the implementation, modifying the returned collection could affect the graph behavior and the changes could be reflected to the graph. |
java.util.Collection<Edge<V,W>> |
edgesOf(V vertex)
Retrieve all the edges of a particular vertex.
Note: the edges that are returned are the one that goes IN this vertex AND the edges that goes OUT of it. Note2: depending on the implementation, modifying the returned collection could affect the graph behavior and the changes could be reflected to the graph. |
java.util.Collection<V> |
getAncestors(V vertex)
Get all the vertices that have the vertex passed as their child.
Basically is the opposite of getChildren(Object)Note: depending on the implementation, modifying the returned collection could affect the graph behavior and the changes could be reflected to the graph. |
java.util.Collection<V> |
getChildren(V vertex)
Get all the vertices that are children of the vertex passed as parameter.
The vertices V(0-N) that are 'children' of a vertex V1, are all the vertices that have an edge where V1 is the source of that edge. Note: depending on the implementation, modifying the returned collection could affect the graph behavior and the changes could be reflected to the graph. |
java.util.Collection<Edge<V,W>> |
getEdgesIn(V vertex)
Retrieve all the edges of a particular vertex.
Note: the edges that are returned are the one that have this vertex as destination and another as source. Note2: depending on the implementation, modifying the returned collection could affect the graph behavior and the changes could be reflected to the graph. |
java.util.Collection<Edge<V,W>> |
getEdgesOut(V vertex)
Retrieve all the edges that goes OUT of a particular vertex.
Note: the edges that are returned are the one that have this vertex as source and another one as destination. Note2: depending on the implementation, modifying the returned collection could affect the graph behavior and the changes could be reflected to the graph. |
java.util.Collection<V> |
getMarkedWith(java.lang.Object mark)
Get all the vertices that are marked with the specific mark passed.
If there aren't vertices with that mark then it is returned an empty set. Note: depending on the implementation, modifying the returned collection could affect the graph behavior and the changes could be reflected to the graph. |
java.util.Collection<java.lang.Object> |
getMarks(V vertex)
Get all the marker of this vertex.
If the vertex doesn't have any mark, then it will return an empty set. Note: depending on the implementation, modifying the returned collection could affect the graph behavior and the changes could be reflected to the graph. |
Vertex<V> |
getVertex(V vertex)
Get an instance of the vertex linked with this graph.
For more info see Vertex |
W |
getWeight(V vertex1,
V vertex2)
Get the weight of the selected edge.
If the edge doesn't exist, then null is returned |
boolean |
isCyclic()
Tells if the graph has some cycle.
A cycle is detected if visiting the graph G starting from V1 (that is any of the vertex of G), the visit can return to V1 in any point. |
boolean |
isDAG()
Tells if the graph has the property of DAG (Directed Acyclic Graph).
A graph is a DAG only if absent of any cycle. |
static <V,W extends java.lang.Number> |
load(Graph<V,W> graph,
java.lang.String file,
java.lang.Class<V> classV,
java.lang.Class<W> classW)
Load an already saved graph in an instance of a graph.
|
void |
mark(V vertex,
java.lang.Object mark)
Add to the specified vertex the mark passed.
A vertex can have multiple marker. |
java.util.Collection<java.lang.Object> |
marks()
Get all the marks of this graph.
Specifically it will return a collection of marks where every mark as associated at least one vertex of the graph. If the graph doesn't have vertex marked then it is returned an empty collection. |
int |
numberOfEdges()
Tells how many edges are in the graph.
|
int |
numberOfVertices()
Tells how many vertices are in the graph.
|
void |
removeAllEdge()
Remove all the edges of the graph.
After this method's call the graph will have only vertices, and no edge. |
void |
removeAllEdge(V vertex)
Remove all edges form a particular vertex of the graph.
After this method's call the selected vertex will have 0 edges. It will be no longer possible to reach this vertex from any other vertex, and vice versa. |
void |
removeAllInEdge(V vertex)
Remove all the edges that goes in the vertex.
After this method's call it will be no longer possible travel to this vertex. |
void |
removeAllOutEdge(V vertex)
Remove all the edges that start from this vertex.
After this method's call it will be no longer possible travel to any vertex from this one. |
void |
removeAllVertex()
Remove all the vertex contained in the graph.
After this method's call the graph will be empty; no vertices nor edges. |
void |
removeEdge(V vertex1,
V vertex2)
Remove the edge between the two vertex.
If the edge doesn't exist, then this call does nothing. After this method's call it will be no longer possible to travel from V1 to V2, nether from V2 to V1. |
void |
removeVertex(V vertex)
Remove the selected vertex from the graph.
After this method's call the vertex will be no longer present in the graph, and nether all his edges. |
static void |
save(Graph<?,?> graph,
java.lang.String file)
Save the Graph passed as input to a file inserted as parameter.
The resulting file is a Json string representing all the graph. If the directory for getting through the file do not exist, then it is created. For now the marks are not included. |
static void |
save(Graph<?,?> graph,
java.lang.String other,
java.lang.String file)
Save the Graph passed as input to a file inserted as parameter.
The resulting file is a Json string representing all the graph. If the directory for getting through the file do not exist, then it is created. For now the marks are not included. The additional parameter is used if you want to save other as well as the graph. |
java.util.Collection<java.util.Collection<V>> |
stronglyConnectedComponents()
The strongly connected components or disconnected components of an arbitrary directed graph
form a partition into subgraphs that are themselves strongly connected.
|
Graph<V,W> |
subGraph(java.lang.Object... marker)
Get a sub-graph of the current one with only the vertex marked with the selected markers.
Each vertex will have all his edges, but only the ones with the destination marked with the same marker. If the marker is not specified or is null then the returning graph will have all the vertices that are not marked by any marker. If the graph doesn't contain any vertex with that marker then an empty graph is returned. |
Graph<V,W> |
subGraph(V source,
int depth)
Get a sub-graph of the current one based on the maximum depth that is given.
If the depth is 1 then only the source and it's children will be in the sub-graph. If the depth is 2 then only the source, it's children and it's children of it's children will be in the sub-graph. And so on. Of course the sub-graph will contain the edges that link the vertices, but only the one selected. |
java.util.List<V> |
topologicalSort()
If the current graph is a DAG, it returns a topological sort of this graph.
A topological ordering of a graph is a linear ordering of its vertices such that for every directed edge (V1, V2) from vertex V1 to vertex V2, V2 comes before V1 in the ordering. |
Graph<V,W> |
transpose()
This method will create a new Graph that is the transposed version of the original.
At the end of this method the new graph will have all the edges inverted in orientation. Example: if the graph G contains (V1, V2, V3) as vertex, and (V1->V2, V3->V2) as edges, the transpose graph G' will contain (V1, V2, V3) as vertex, and (V2->V1, V2->V3) as edges. |
void |
unMark(V vertex)
Unmark the vertex selected.
After this call the vertex will not have any marked object to himself. |
void |
unMark(V vertex,
java.lang.Object mark)
Remove the selected mark from the vertex.
|
void |
unMarkAll()
Remove all the marker to all the vertex.
After this call the getMarks(Object) applied to any vertex will return an empty set |
void |
unMarkAll(java.lang.Object mark)
Remove the selected mark from all the vertices
|
java.util.Collection<V> |
vertices()
Get all the vertices in the graph.
If the graph doesn't contains vertices, it'll return an empty collection. Note: depending on the implementation, modifying the returned collection could affect the graph behavior and the changes could be reflected to the graph. |
VisitInfo<V> |
visit(V source,
VisitStrategy<V,W> strategy,
java.util.function.Consumer<V> visit)
Visit the graph accordingly to the strategy that is passed.
This method visit the graph from the source to all the vertex that are reachable form the source. Some strategy can accept a source vertex null, because they visit all the graph anyway. |
static final java.lang.String NOT_DAG
static final java.lang.String NOT_CONNECTED
static final java.lang.String PARAM_NULL
static final java.lang.String VERTEX_NOT_CONTAINED
static final com.google.gson.Gson GSON
boolean isCyclic()
boolean isDAG()
isCyclic() )boolean contains(V vertex) throws java.lang.NullPointerException
vertex - the vertex to checkjava.lang.NullPointerException - if the vertex is nullVertex<V> getVertex(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Vertexvertex - the vertexjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphvoid addVertex(V vertex) throws java.lang.NullPointerException
vertex - the vertex to addjava.lang.NullPointerException - if the vertex is nullboolean addVertexIfAbsent(V vertex) throws java.lang.NullPointerException
contains(Object) returns true.vertex - the vertex to addjava.lang.NullPointerException - if the vertex is nullvoid addAllVertices(java.util.Collection<V> vertices) throws java.lang.NullPointerException
vertices - a collection of the vertices to addjava.lang.NullPointerException - if the set is nullvoid removeVertex(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - the vertex to removejava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not containedvoid removeAllVertex()
java.util.Collection<java.lang.Object> marks()
void mark(V vertex, java.lang.Object mark) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - the vertex to markmark - the mark to addjava.lang.NullPointerException - if one of the param is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphvoid unMark(V vertex, java.lang.Object mark) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - the vertex where remove the markmark - the mark to removejava.lang.NullPointerException - if a param is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphvoid unMark(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - the vertexjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphjava.util.Collection<V> getMarkedWith(java.lang.Object mark) throws java.lang.NullPointerException
mark - the markjava.lang.NullPointerException - if the mark is nulljava.util.Collection<java.lang.Object> getMarks(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - the vertexjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphvoid unMarkAll(java.lang.Object mark)
throws java.lang.NullPointerException
mark - the mark to removejava.lang.NullPointerException - if the mark is nullvoid unMarkAll()
getMarks(Object) applied to any vertex will return an empty setW addEdge(V vertex1, V vertex2, W weight) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex1 - a vertex of the graphvertex2 - a vertex of the graphweight - the weight of the edgejava.lang.NullPointerException - if one of the parameter is nulljava.lang.IllegalArgumentException - if one of the vertex is not contained in the graphW addEdge(Edge<V,W> edge) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
edge - the edge to addjava.lang.NullPointerException - if one of the parameter is nulljava.lang.IllegalArgumentException - if one of the vertex is not contained in the graphW addEdgeAndVertices(V vertex1, V vertex2, W weight) throws java.lang.NullPointerException
vertex1 - a vertex of the graphvertex2 - a vertex of the graphweight - the weight of the edgejava.lang.NullPointerException - if one of the parameter is nullW addEdgeAndVertices(Edge<V,W> edge) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
edge - the edge to addjava.lang.NullPointerException - if one of the parameter is nulljava.lang.IllegalArgumentExceptionvoid addAllEdges(java.util.Collection<Edge<V,W>> edges) throws java.lang.NullPointerException
edges - the edges to addjava.lang.NullPointerException - if the set is nullW getWeight(V vertex1, V vertex2) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex1 - a vertex of the graphvertex2 - a vertex of the graphjava.lang.NullPointerException - if one of the parameters is nulljava.lang.IllegalArgumentException - if one of the vertex is not contained in the graphvoid removeEdge(V vertex1, V vertex2) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex1 - a vertex of the graphvertex2 - a vertex of the graphjava.lang.NullPointerException - if one of the parameters is nulljava.lang.IllegalArgumentException - if one of the vertex is not contained in the graphvoid removeAllInEdge(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - a vertex of the graphjava.lang.NullPointerException - if one of the parameters is nulljava.lang.IllegalArgumentException - if one of the vertex is not contained in the graphvoid removeAllOutEdge(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - a vertex of the graphjava.lang.NullPointerException - if one of the parameters is nulljava.lang.IllegalArgumentException - if one of the vertex is not contained in the graphvoid removeAllEdge(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - a vertex of the graphjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if one of the vertex is not contained in the graphvoid removeAllEdge()
boolean containsEdge(V vertex1, V vertex2) throws java.lang.NullPointerException
vertex1 - a vertex of the graphvertex2 - a vertex of the graphjava.lang.NullPointerException - if one of the parameters is nulljava.util.Collection<V> vertices()
java.util.Collection<Edge<V,W>> edges()
java.util.Collection<Edge<V,W>> edgesOf(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - a vertex of the graphjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphjava.util.Collection<Edge<V,W>> getEdgesIn(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - a vertex of the graphjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphjava.util.Collection<Edge<V,W>> getEdgesOut(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - a vertex of the graphjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphjava.util.Collection<V> getChildren(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - the source vertexjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphjava.util.Collection<V> getAncestors(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
getChildren(Object)vertex - a vertex of the graphjava.lang.NullPointerException - if one of the parameters is nulljava.lang.IllegalArgumentException - if one of the vertex is not contained in the graphint degreeIn(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - a vertex of the graphjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphint degreeOut(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - a vertex of the graphjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphint degree(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
vertex - a vertex of the graphjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphint numberOfVertices()
int numberOfEdges()
VisitInfo<V> visit(V source, VisitStrategy<V,W> strategy, java.util.function.Consumer<V> visit) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
source - the source vertex of the visitstrategy - the algorithm for visiting the graphvisit - the function to apply at each vertexjava.lang.NullPointerException - if one of the parameter is null (except the consumer)java.lang.IllegalArgumentException - if the vertex is not in the graphGraph<V,W> transpose()
java.util.List<V> topologicalSort() throws java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException - if the graph is not a DAG (see isDAG())java.util.Collection<java.util.Collection<V>> stronglyConnectedComponents()
Graph<V,W> subGraph(V source, int depth) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
source - the source vertexdepth - the maximum depth (must be a positive number, if >=0 a graph containing only the source is returned)java.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not containedGraph<V,W> subGraph(java.lang.Object... marker)
marker - one or more markersjava.util.List<Edge<V,W>> distance(V source, V destination) throws java.lang.NullPointerException, java.lang.IllegalArgumentException, java.lang.UnsupportedOperationException
source - the vertex where to startdestination - the destination chosenjava.lang.NullPointerException - if one of the parameter is null (except the consumer)java.lang.IllegalArgumentException - if the vertex is not in the graphjava.lang.UnsupportedOperationException - if from the source it's not possible to reach the destinationjava.util.Map<V,java.util.List<Edge<V,W>>> distance(V source) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
source - the vertex where to startjava.lang.NullPointerException - if one of the parameter is null (except the consumer)java.lang.IllegalArgumentException - if the vertex is not in the graphstatic void save(Graph<?,?> graph, java.lang.String file) throws java.io.IOException
graph - the graph to savefile - the name of the filejava.io.IOException - for various reason that appear in the message, but the most common is that the file is not found.static void save(Graph<?,?> graph, java.lang.String other, java.lang.String file) throws java.io.IOException
graph - the graph to saveother - other things to savefile - the name of the filejava.io.IOException - for various reason that appear in the message, but the most common is that the file is not found.static <V,W extends java.lang.Number> java.lang.String load(Graph<V,W> graph, java.lang.String file, java.lang.Class<V> classV, java.lang.Class<W> classW) throws java.io.IOException, java.lang.NullPointerException, com.google.gson.JsonSyntaxException
V - the parameter needed for the vertexW - the parameter needed for the weightgraph - the graph to load withfile - the file where the graph is savedclassV - the class used for the VertexclassW - the class used for the Weightjava.io.IOException - for any possible reason, the most common: the file doesn't existjava.lang.NullPointerException - if the graph is nullcom.google.gson.JsonSyntaxException - if the file is malformed or corrupted