V
- the graph vertex typeE
- the graph edge typepublic class MutableNetworkAdapter<V,E> extends BaseNetworkAdapter<V,E,com.google.common.graph.MutableNetwork<V,E>> implements Graph<V,E>, Cloneable, Serializable
MutableNetwork
.
Changes in the adapter such as adding or removing vertices and edges are reflected in the underlying network. Example usage:
MutableNetwork<String, DefaultEdge> mutableNetwork = NetworkBuilder.directed().allowsParallelEdges(true).allowsSelfLoops(true).build(); Graph<String, DefaultEdge> graph = new MutableNetworkAdapter<>( mutableNetwork, SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER); graph.addVertex("v1"); System.out.println(mutableNetwork.nodes().contains("v1")); // outputs true
Modifier and Type | Field and Description |
---|---|
protected static String |
GRAPH_IS_UNWEIGHTED |
edgeSupplier, LOOPS_NOT_ALLOWED, network, unmodifiableEdgeSet, unmodifiableVertexSet, vertexSupplier
DEFAULT_EDGE_WEIGHT
Constructor and Description |
---|
MutableNetworkAdapter(com.google.common.graph.MutableNetwork<V,E> network)
Create a new network adapter.
|
MutableNetworkAdapter(com.google.common.graph.MutableNetwork<V,E> network,
Supplier<V> vertexSupplier,
Supplier<E> edgeSupplier)
Create a new network adapter.
|
Modifier and Type | Method and Description |
---|---|
E |
addEdge(V sourceVertex,
V targetVertex)
Creates a new edge in this graph, going from the source vertex to the target vertex, and
returns the created edge.
|
boolean |
addEdge(V sourceVertex,
V targetVertex,
E e)
Adds the specified edge to this graph, going from the source vertex to the target vertex.
|
V |
addVertex()
Creates a new vertex in this graph and returns it.
|
boolean |
addVertex(V v)
Adds the specified vertex to this graph if not already present.
|
Object |
clone()
Returns a shallow copy of this graph instance.
|
boolean |
removeEdge(E e)
Removes the specified edge from the graph.
|
E |
removeEdge(V sourceVertex,
V targetVertex)
Removes an edge going from source vertex to target vertex, if such vertices and such edge
exist in this graph.
|
boolean |
removeVertex(V v)
Removes the specified vertex from this graph including all its touching edges if present.
|
void |
setEdgeWeight(E e,
double weight)
Assigns a weight to an edge.
|
containsEdge, containsVertex, degreeOf, edgeSet, edgesOf, getAllEdges, getEdge, getEdgeSource, getEdgeSupplier, getEdgeTarget, getEdgeWeight, getType, getVertexSupplier, incomingEdgesOf, inDegreeOf, outDegreeOf, outgoingEdgesOf, setEdgeSupplier, setVertexSupplier, vertexSet
assertVertexExist, containsEdge, equals, hashCode, removeAllEdges, removeAllEdges, removeAllEdges, removeAllVertices, toString, toStringFromSets
finalize, getClass, notify, notifyAll, wait, wait, wait
containsEdge, containsEdge, containsVertex, degreeOf, edgeSet, edgesOf, getAllEdges, getEdge, getEdgeSource, getEdgeSupplier, getEdgeTarget, getEdgeWeight, getType, getVertexSupplier, incomingEdgesOf, inDegreeOf, outDegreeOf, outgoingEdgesOf, removeAllEdges, removeAllEdges, removeAllVertices, setEdgeWeight, vertexSet
protected static final String GRAPH_IS_UNWEIGHTED
public MutableNetworkAdapter(com.google.common.graph.MutableNetwork<V,E> network)
network
- the mutable networkpublic E addEdge(V sourceVertex, V targetVertex)
Graph
null
.
The source and target vertices must already be contained in this graph. If they are not found
in graph IllegalArgumentException
is thrown.
This method creates the new edge e
using this graph's edge supplier (see
Graph.getEdgeSupplier()
). For the new edge to be added e
must not be
equal to any other edge the graph (even if the graph allows edge-multiplicity). More
formally, the graph must not contain any edge e2
such that
e2.equals(e)
. If such
e2
is found then the newly created edge e
is abandoned, the method leaves
this graph unchanged and returns null
.
If the underlying graph implementation's Graph.getEdgeSupplier()
returns
null
, then this method cannot create edges and throws an
UnsupportedOperationException
.
addEdge
in interface Graph<V,E>
sourceVertex
- source vertex of the edge.targetVertex
- target vertex of the edge.
null
.Graph.getEdgeSupplier()
public boolean addEdge(V sourceVertex, V targetVertex, E e)
Graph
e
, to this graph if this graph contains no edge e2
such that
e2.equals(e)
. If this graph already contains such an edge, the call leaves this
graph unchanged and returns false. Some graphs do not allow edge-multiplicity. In
such cases, if the graph already contains an edge from the specified source to the specified
target, than this method does not change the graph and returns
false
. If the edge was added to the graph, returns
true
.
The source and target vertices must already be contained in this graph. If they are not found in graph IllegalArgumentException is thrown.
addEdge
in interface Graph<V,E>
sourceVertex
- source vertex of the edge.targetVertex
- target vertex of the edge.e
- edge to be added to this graph.Graph.addEdge(Object, Object)
,
Graph.getEdgeSupplier()
public V addVertex()
Graph
This method creates the new vertex v
using this graph's vertex supplier (see
Graph.getVertexSupplier()
). For the new vertex to be added v
must not
be equal to any other vertex in the graph. More formally, the graph must not contain any
vertex v2
such that v2.equals(v)
. If such
v2
is found then the newly created vertex v
is abandoned, the method
leaves this graph unchanged and throws an IllegalArgumentException
.
If the underlying graph implementation's Graph.getVertexSupplier()
returns
null
, then this method cannot create vertices and throws an
UnsupportedOperationException
.
Care must also be taken when interchanging calls to methods Graph.addVertex(Object)
and Graph.addVertex()
. In such a case the user must make sure never to add vertices
in the graph using method Graph.addVertex(Object)
, which are going to be returned in
the future by the supplied vertex supplier. Such a sequence will result into an
IllegalArgumentException
when calling method Graph.addVertex()
.
addVertex
in interface Graph<V,E>
Graph.getVertexSupplier()
public boolean addVertex(V v)
Graph
v
, to this graph if this graph contains no vertex
u
such that
u.equals(v)
. If this graph already contains such vertex, the call leaves this graph
unchanged and returns false. In combination with the restriction on constructors,
this ensures that graphs never contain duplicate vertices.public E removeEdge(V sourceVertex, V targetVertex)
Graph
null
otherwise.removeEdge
in interface Graph<V,E>
sourceVertex
- source vertex of the edge.targetVertex
- target vertex of the edge.null
if no edge removed.public boolean removeEdge(E e)
Graph
e2
such that e2.equals(e)
, if the graph contains such edge. Returns
true if the graph contained the specified edge. (The graph will not contain the
specified edge once the call returns).
If the specified edge is null
returns
false
.
removeEdge
in interface Graph<V,E>
e
- edge to be removed from this graph, if present.true
if and only if the graph contained the specified edge.public boolean removeVertex(V v)
Graph
u
such that u.equals(v)
, the call removes all edges that touch
u
and then removes u
itself. If no such u
is found,
the call leaves the graph unchanged. Returns true if the graph contained the
specified vertex. (The graph will not contain the specified vertex once the call returns).
If the specified vertex is null
returns
false
.
removeVertex
in interface Graph<V,E>
v
- vertex to be removed from this graph, if present.true
if the graph contained the specified vertex; false
otherwise.public void setEdgeWeight(E e, double weight)
Graph
setEdgeWeight
in interface Graph<V,E>
e
- edge on which to set weightweight
- new weight for edgepublic Object clone()
clone
in class Object
RuntimeException
- in case the clone is not supportedObject.clone()
Copyright © 2019. All rights reserved.