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
@Beta public interface ValueGraph<N,V> extends Graph<N>
An interface for graph- structured data, whose edges have associated non-unique values.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
, andNetwork
. 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 ofGraph
, explicitly does not support parallel edges, and forbids implementations or extensions with parallel edges. If you need parallel edges, useNetwork
. (You can use a positiveInteger
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 theValueGraphBuilder
class:MutableValueGraph<Integer, Double> graph = ValueGraphBuilder.directed().build();
ValueGraphBuilder.build()
returns an instance ofMutableValueGraph
, which is a subtype ofValueGraph
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-mutatingValueGraph
interface, or anImmutableValueGraph
.You can create an immutable copy of an existing
ValueGraph
usingImmutableValueGraph.copyOf(ValueGraph)
:ImmutableValueGraph<Integer, Double> immutableGraph = ImmutableValueGraph.copyOf(graph);
Instances of
ImmutableValueGraph
do not implementMutableValueGraph
(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
All Methods Instance Methods Abstract Methods Modifier and Type Method Description V
edgeValue(java.lang.Object nodeU, java.lang.Object nodeV)
If there is an edge connectingnodeU
tonodeV
, returns the non-null value associated with that edge.V
edgeValueOrDefault(java.lang.Object nodeU, java.lang.Object nodeV, V defaultValue)
If there is an edge connectingnodeU
tonodeV
, returns the non-null value associated with that edge; otherwise, returnsdefaultValue
.boolean
equals(java.lang.Object object)
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 Detail
-
edgeValue
V edgeValue(java.lang.Object nodeU, java.lang.Object nodeV)
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:
java.lang.IllegalArgumentException
- if there is no edge connectingnodeU
tonodeV
.
-
edgeValueOrDefault
V edgeValueOrDefault(java.lang.Object nodeU, java.lang.Object nodeV, @Nullable V defaultValue)
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
boolean equals(@Nullable java.lang.Object object)
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.
-
-