Interface ValueGraph<N,V>
- Type Parameters:
N
- Node parameter typeV
- Value parameter type
- All Superinterfaces:
Graph<N>
- All Known Subinterfaces:
MutableValueGraph<N,
V>
- All Known Implementing Classes:
AbstractValueGraph
,ImmutableValueGraph
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.
Capabilities
ValueGraph
supports the following use cases (definitions of
terms):
- directed graphs
- undirected graphs
- graphs that do/don't allow self-loops
- graphs whose nodes/edges are insertion-ordered, sorted, or unordered
- graphs whose edges have associated values
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.)
Building a 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.
Additional documentation
See the Guava User Guide for the common.graph
package ("Graphs Explained") for
additional documentation, including:
- Since:
- 20.0
-
Method Summary
Modifier and TypeMethodDescriptionIf there is an edge connectingnodeU
tonodeV
, returns the non-null value associated with that edge.edgeValueOrDefault
(Object nodeU, Object nodeV, V defaultValue) If there is an edge connectingnodeU
tonodeV
, returns the non-null value associated with that edge; otherwise, returnsdefaultValue
.boolean
For the defaultValueGraph
implementations, returns true ifthis == object
(reference equality).int
hashCode()
For the defaultValueGraph
implementations, returnsSystem.identityHashCode(this)
.Methods inherited from interface com.google.common.graph.Graph
adjacentNodes, allowsSelfLoops, degree, edges, inDegree, isDirected, nodeOrder, nodes, outDegree, predecessors, successors
-
Method Details
-
edgeValue
If there is an edge connectingnodeU
tonodeV
, returns the non-null value associated with that edge.In an undirected graph, this is equal to
edgeValue(nodeV, nodeU)
.- Throws:
IllegalArgumentException
- if there is no edge connectingnodeU
tonodeV
.
-
edgeValueOrDefault
If there is an edge connectingnodeU
tonodeV
, returns the non-null value associated with that edge; otherwise, returnsdefaultValue
.In an undirected graph, this is equal to
edgeValueOrDefault(nodeV, nodeU, defaultValue)
. -
equals
For the defaultValueGraph
implementations, returns true ifthis == object
(reference equality). External implementations are free to define this method as they see fit, as long as they satisfy theObject.equals(Object)
contract.To compare two
ValueGraph
s based on their contents rather than their references, seeGraphs.equivalent(ValueGraph, ValueGraph)
. -
hashCode
int hashCode()For the defaultValueGraph
implementations, returnsSystem.identityHashCode(this)
. External implementations are free to define this method as they see fit, as long as they satisfy theObject.hashCode()
contract.
-