V
- the graph vertex typeE
- the graph edge typepublic class AsWeightedGraph<V,E> extends GraphDelegator<V,E> implements Serializable, Graph<V,E>
This class can be used to make an unweighted graph weighted, to override the weights of a weighted graph, or to provide different weighted views of the same underlying graph. For instance, the edges of a graph representing a road network might have two weights associated with them: a travel time and a travel distance. Instead of creating two weighted graphs of the same network, one would simply create two weighted views of the same underlying graph.
This class offers two ways to associate a weight with an edge:
DEFAULT_EDGE_WEIGHT
Constructor and Description |
---|
AsWeightedGraph(Graph<V,E> graph,
Function<E,Double> weightFunction,
boolean cacheWeights,
boolean writeWeightsThrough)
Constructor for AsWeightedGraph which uses a weight function to compute edge weights.
|
AsWeightedGraph(Graph<V,E> graph,
Map<E,Double> weights)
Constructor for AsWeightedGraph where the weights are provided through a map.
|
AsWeightedGraph(Graph<V,E> graph,
Map<E,Double> weights,
boolean writeWeightsThrough)
Constructor for AsWeightedGraph which allows weight write propagation to be requested
explicitly.
|
Modifier and Type | Method and Description |
---|---|
double |
getEdgeWeight(E e)
Returns the weight assigned to a given edge.
|
GraphType |
getType()
Get the graph type.
|
void |
setEdgeWeight(E e,
double weight)
Assigns a weight to an edge.
|
addEdge, addEdge, addVertex, addVertex, containsEdge, containsVertex, degreeOf, edgeSet, edgesOf, getAllEdges, getDelegate, getEdge, getEdgeSource, getEdgeSupplier, getEdgeTarget, getVertexSupplier, incomingEdgesOf, inDegreeOf, outDegreeOf, outgoingEdgesOf, removeEdge, removeEdge, removeVertex, toString, vertexSet
assertVertexExist, containsEdge, equals, hashCode, removeAllEdges, removeAllEdges, removeAllEdges, removeAllVertices, toStringFromSets
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addEdge, addEdge, addVertex, addVertex, containsEdge, containsEdge, containsVertex, degreeOf, edgeSet, edgesOf, getAllEdges, getEdge, getEdgeSource, getEdgeSupplier, getEdgeTarget, getVertexSupplier, incomingEdgesOf, inDegreeOf, outDegreeOf, outgoingEdgesOf, removeAllEdges, removeAllEdges, removeAllVertices, removeEdge, removeEdge, removeVertex, setEdgeWeight, vertexSet
public AsWeightedGraph(Graph<V,E> graph, Map<E,Double> weights)
graph
- the backing graph over which a weighted view is to be created.weights
- the map containing the edge weights.NullPointerException
- if the graph or the weights are null.public AsWeightedGraph(Graph<V,E> graph, Map<E,Double> weights, boolean writeWeightsThrough)
graph
- the backing graph over which an weighted view is to be createdweights
- the map containing the edge weightswriteWeightsThrough
- if set to true, the weights will get propagated to the backing
graph in the setEdgeWeight()
method.NullPointerException
- if the graph or the weights are nullIllegalArgumentException
- if writeWeightsThrough
is set to true and
graph
is not a weighted graphpublic AsWeightedGraph(Graph<V,E> graph, Function<E,Double> weightFunction, boolean cacheWeights, boolean writeWeightsThrough)
cacheWeights
is set to true
, the weight of an edge returned by the
weightFunction
after its first invocation is stored in a map. The weight of an
edge returned by subsequent calls to @link{getEdgeWeight} for the same edge will then be
retrieved directly from the map, instead of re-invoking the weight function. If
cacheWeights
is set to false
, each invocation of
the @link{getEdgeWeight} method will invoke the weight function. Caching the edge weights is
particularly useful when pre-computing all edge weights is expensive and it is expected that
the weights of only a subset of all edges will be queried.graph
- the backing graph over which an weighted view is to be createdweightFunction
- function which maps an edge to a weightcacheWeights
- if set to true
, weights are cached once computed by the
weight functionwriteWeightsThrough
- if set to true
, the weight set directly by
the @link{setEdgeWeight} method will be propagated to the backing graph.NullPointerException
- if the graph or the weight function is nullIllegalArgumentException
- if writeWeightsThrough
is set to true and
graph
is not a weighted graphpublic double getEdgeWeight(E e)
cacheWeights
is set to true
in the constructor). If caching
was disabled, or the edge could not be found in the cache, the weight function is invoked. If
the function does not provide a weight for a given edge, the call is again propagated to the
underlying graph.getEdgeWeight
in interface Graph<V,E>
getEdgeWeight
in class GraphDelegator<V,E>
e
- edge of interestNullPointerException
- if the edge is nullpublic void setEdgeWeight(E e, double weight)
writeWeightsThrough
is set to true
,
the same weight is set in the backing graph. If this class was constructed using a weight
function, it only makes sense to invoke this method when cacheWeights
is set to
true. This method can then be used to preset weights in the cache, or to overwrite existing
values.setEdgeWeight
in interface Graph<V,E>
setEdgeWeight
in class GraphDelegator<V,E>
e
- edge on which to set weightweight
- new weight for edgeNullPointerException
- if the edge is nullpublic GraphType getType()
GraphDelegator
Copyright © 2019. All rights reserved.