V - the verticesW - the weight of the edgespublic class MapGraph<V,W extends java.lang.Number> extends java.lang.Object implements Graph<V,W>
GSON, NOT_CONNECTED, NOT_DAG, PARAM_NULL, VERTEX_NOT_CONTAINED| Constructor and Description |
|---|
MapGraph() |
| 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 Graph.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 Graph.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. |
java.util.Iterator<V> |
iterator() |
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. |
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 Graph.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. |
public boolean isCyclic()
Graphpublic boolean isDAG()
GraphGraph.isCyclic() )public Vertex<V> getVertex(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphVertexpublic void addVertex(V vertex) throws java.lang.NullPointerException
Graphpublic boolean addVertexIfAbsent(V vertex) throws java.lang.NullPointerException
GraphGraph.contains(Object) returns true.addVertexIfAbsent in interface Graph<V,W extends java.lang.Number>vertex - the vertex to addjava.lang.NullPointerException - if the vertex is nullpublic void addAllVertices(java.util.Collection<V> vertices) throws java.lang.NullPointerException
GraphaddAllVertices in interface Graph<V,W extends java.lang.Number>vertices - a collection of the vertices to addjava.lang.NullPointerException - if the set is nullpublic void removeVertex(V vertex) throws java.lang.NullPointerException
GraphremoveVertex in interface Graph<V,W extends java.lang.Number>vertex - the vertex to removejava.lang.NullPointerException - if the vertex is nullpublic void removeAllVertex()
GraphremoveAllVertex in interface Graph<V,W extends java.lang.Number>public boolean contains(V vertex) throws java.lang.NullPointerException
Graphpublic java.util.Collection<java.lang.Object> marks()
Graphpublic void mark(V vertex, java.lang.Object mark) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Graphpublic void unMark(V vertex, java.lang.Object mark) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Graphpublic void unMark(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Graphpublic java.util.Collection<V> getMarkedWith(java.lang.Object mark) throws java.lang.NullPointerException
GraphgetMarkedWith in interface Graph<V,W extends java.lang.Number>mark - the markjava.lang.NullPointerException - if the mark is nullpublic java.util.Collection<java.lang.Object> getMarks(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphgetMarks in interface Graph<V,W extends java.lang.Number>vertex - the vertexjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphpublic void unMarkAll(java.lang.Object mark)
Graphpublic void unMarkAll()
GraphGraph.getMarks(Object) applied to any vertex will return an empty setpublic W addEdge(V vertex1, V vertex2, W weight) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphaddEdge in interface Graph<V,W extends java.lang.Number>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 graphpublic W addEdge(Edge<V,W> edge) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphaddEdge in interface Graph<V,W extends java.lang.Number>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 graphpublic W addEdgeAndVertices(V vertex1, V vertex2, W weight) throws java.lang.NullPointerException
GraphaddEdgeAndVertices in interface Graph<V,W extends java.lang.Number>vertex1 - a vertex of the graphvertex2 - a vertex of the graphweight - the weight of the edgejava.lang.NullPointerException - if one of the parameter is nullpublic W addEdgeAndVertices(Edge<V,W> edge) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphaddEdgeAndVertices in interface Graph<V,W extends java.lang.Number>edge - the edge to addjava.lang.NullPointerException - if one of the parameter is nulljava.lang.IllegalArgumentExceptionpublic void addAllEdges(java.util.Collection<Edge<V,W>> edges) throws java.lang.NullPointerException
GraphaddAllEdges in interface Graph<V,W extends java.lang.Number>edges - the edges to addjava.lang.NullPointerException - if the set is nullpublic W getWeight(V vertex1, V vertex2) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphgetWeight in interface Graph<V,W extends java.lang.Number>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 graphpublic void removeEdge(V vertex1, V vertex2) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphremoveEdge in interface Graph<V,W extends java.lang.Number>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 graphpublic void removeAllInEdge(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphremoveAllInEdge in interface Graph<V,W extends java.lang.Number>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 graphpublic void removeAllOutEdge(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphremoveAllOutEdge in interface Graph<V,W extends java.lang.Number>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 graphpublic void removeAllEdge(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphremoveAllEdge in interface Graph<V,W extends java.lang.Number>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 graphpublic void removeAllEdge()
GraphremoveAllEdge in interface Graph<V,W extends java.lang.Number>public boolean containsEdge(V vertex1, V vertex2) throws java.lang.NullPointerException
GraphcontainsEdge in interface Graph<V,W extends java.lang.Number>vertex1 - a vertex of the graphvertex2 - a vertex of the graphjava.lang.NullPointerException - if one of the parameters is nullpublic java.util.Collection<V> vertices()
Graphpublic java.util.Collection<Edge<V,W>> edges()
Graphpublic java.util.Collection<Edge<V,W>> edgesOf(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Graphpublic java.util.Collection<Edge<V,W>> getEdgesIn(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphgetEdgesIn in interface Graph<V,W extends java.lang.Number>vertex - a vertex of the graphjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphpublic java.util.Collection<Edge<V,W>> getEdgesOut(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphgetEdgesOut in interface Graph<V,W extends java.lang.Number>vertex - a vertex of the graphjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphpublic java.util.Collection<V> getChildren(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphgetChildren in interface Graph<V,W extends java.lang.Number>vertex - the source vertexjava.lang.NullPointerException - if the vertex is nulljava.lang.IllegalArgumentException - if the vertex is not contained in the graphpublic java.util.Collection<V> getAncestors(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphGraph.getChildren(Object)getAncestors in interface Graph<V,W extends java.lang.Number>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 graphpublic int degreeIn(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Graphpublic int degreeOut(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Graphpublic int degree(V vertex) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Graphpublic int numberOfVertices()
GraphnumberOfVertices in interface Graph<V,W extends java.lang.Number>public int numberOfEdges()
GraphnumberOfEdges in interface Graph<V,W extends java.lang.Number>public VisitInfo<V> visit(V source, VisitStrategy<V,W> strategy, java.util.function.Consumer<V> visit) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Graphvisit in interface Graph<V,W extends java.lang.Number>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 graphpublic Graph<V,W> transpose()
Graphpublic java.util.List<V> topologicalSort() throws java.lang.UnsupportedOperationException
GraphtopologicalSort in interface Graph<V,W extends java.lang.Number>java.lang.UnsupportedOperationException - if the graph is not a DAG (see Graph.isDAG())public java.util.Collection<java.util.Collection<V>> stronglyConnectedComponents()
GraphstronglyConnectedComponents in interface Graph<V,W extends java.lang.Number>public Graph<V,W> subGraph(V source, int depth) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
GraphsubGraph in interface Graph<V,W extends java.lang.Number>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 containedpublic Graph<V,W> subGraph(java.lang.Object... marker)
Graphpublic java.util.List<Edge<V,W>> distance(V source, V destination) throws java.lang.NullPointerException, java.lang.IllegalArgumentException, java.lang.UnsupportedOperationException
Graphdistance in interface Graph<V,W extends java.lang.Number>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 destinationpublic java.util.Map<V,java.util.List<Edge<V,W>>> distance(V source) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Graphdistance in interface Graph<V,W extends java.lang.Number>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 graph