N
- Node parameter typeV
- Value parameter type@Beta public interface ValueGraph<N,V> extends Graph<N>
A graph is composed of a set of nodes and a set of edges connecting pairs of nodes.
There are three main interfaces provided to represent graphs. In order of increasing
complexity they are: Graph
, ValueGraph
, and Network
. You should generally
prefer the simplest interface that satisfies your use case. See the
"Choosing the right graph type" section of the Guava User Guide for more details.
ValueGraph
supports the following use cases (definitions of
terms):
ValueGraph
, as a subtype of Graph
, explicitly does not support parallel edges,
and forbids implementations or extensions with parallel edges. If you need parallel edges, use
Network
. (You can use a positive Integer
edge value as a loose representation of
edge multiplicity, but the *degree()
and mutation methods will not reflect your
interpretation of the edge value as its multiplicity.)
ValueGraph
The implementation classes that `common.graph` provides are not public, by design. To create
an instance of one of the built-in implementations of ValueGraph
, use the ValueGraphBuilder
class:
MutableValueGraph<Integer, Double> graph = ValueGraphBuilder.directed().build();
ValueGraphBuilder.build()
returns an instance of MutableValueGraph
, which is a
subtype of ValueGraph
that provides methods for adding and removing nodes and edges. If
you do not need to mutate a graph (e.g. if you write a method than runs a read-only algorithm on
the graph), you should use the non-mutating ValueGraph
interface, or an ImmutableValueGraph
.
You can create an immutable copy of an existing ValueGraph
using ImmutableValueGraph.copyOf(ValueGraph)
:
ImmutableValueGraph<Integer, Double> immutableGraph = ImmutableValueGraph.copyOf(graph);
Instances of ImmutableValueGraph
do not implement MutableValueGraph
(obviously!) and are contractually guaranteed to be unmodifiable and thread-safe.
The Guava User Guide has more information on (and examples of) building graphs.
See the Guava User Guide for the common.graph
package ("Graphs Explained") for
additional documentation, including:
Modifier and Type | Method and Description |
---|---|
V |
edgeValue(Object nodeU,
Object nodeV)
If there is an edge connecting
nodeU to nodeV , returns the non-null value
associated with that edge. |
V |
edgeValueOrDefault(Object nodeU,
Object nodeV,
V defaultValue)
If there is an edge connecting
nodeU to nodeV , returns the non-null value
associated with that edge; otherwise, returns defaultValue . |
boolean |
equals(Object object)
For the default
ValueGraph implementations, returns true if this == object
(reference equality). |
int |
hashCode()
For the default
ValueGraph implementations, returns System.identityHashCode(this) . |
adjacentNodes, allowsSelfLoops, degree, edges, inDegree, isDirected, nodeOrder, nodes, outDegree, predecessors, successors
V edgeValue(Object nodeU, Object nodeV)
nodeU
to nodeV
, returns the non-null value
associated with that edge.
In an undirected graph, this is equal to edgeValue(nodeV, nodeU)
.
IllegalArgumentException
- if there is no edge connecting nodeU
to nodeV
.V edgeValueOrDefault(Object nodeU, Object nodeV, @Nullable V defaultValue)
nodeU
to nodeV
, returns the non-null value
associated with that edge; otherwise, returns defaultValue
.
In an undirected graph, this is equal to edgeValueOrDefault(nodeV, nodeU,
defaultValue)
.
boolean equals(@Nullable Object object)
ValueGraph
implementations, returns true if this == object
(reference equality). External implementations are free to define this method as they see fit,
as long as they satisfy the Object.equals(Object)
contract.
To compare two ValueGraph
s based on their contents rather than their references, see
Graphs.equivalent(ValueGraph, ValueGraph)
.
int hashCode()
ValueGraph
implementations, returns System.identityHashCode(this)
. External implementations are free to define this method as they
see fit, as long as they satisfy the Object.hashCode()
contract.Copyright © 2010-2016. All Rights Reserved.