A graph is composed of a set of nodes and a set of edges connecting pairs of nodes.
There are three primary 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.)
ValueGraphThe 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 |
|---|---|
| public Set | adjacentNodes(N node)
Redeclares com. node in
this graph.
|
| public boolean | allowsSelfLoops()
Redeclares com. |
| public Graph | |
| public int | degree(N node)
Redeclares com. node's incident edges, counting self-loops twice (equivalently,
the number of times an edge touches node).
|
| public Set | |
| public Optional | edgeValue(N nodeU, N nodeV)
Returns the value of the edge that connects |
| public Optional | edgeValue(EndpointPair<N> endpoints)
Returns the value of the edge that connects |
| public V | edgeValueOrDefault(N nodeU, N nodeV, V defaultValue)
Returns the value of the edge that connects |
| public V | edgeValueOrDefault(EndpointPair<N> endpoints, V defaultValue)
Returns the value of the edge that connects |
| public boolean | equals(Object object)
Returns |
| public boolean | hasEdgeConnecting(N nodeU, N nodeV)
Redeclares com. nodeU to nodeV.
|
| public boolean | hasEdgeConnecting(EndpointPair<N> endpoints)
Redeclares com. endpoints (in the order, if
any, specified by endpoints).
|
| public int | |
| public ElementOrder | incidentEdgeOrder()
Redeclares com. ElementOrder that specifies the order of iteration for the elements of
edges(), adjacentNodes(Object), predecessors(Object), successors(Object) and incidentEdges(Object).
|
| public Set | incidentEdges(N node)
Redeclares com. node.
|
| public int | inDegree(N node)
Redeclares com. node's incoming edges (equal to predecessors(node).size())
in a directed graph.
|
| public boolean | isDirected()
Redeclares com. |
| public ElementOrder | nodeOrder()
Redeclares com. nodes().
|
| public Set | nodes()
Redeclares com. nodeOrder().
|
| public int | outDegree(N node)
Redeclares com. node's outgoing edges (equal to successors(node).size())
in a directed graph.
|
| public Set | predecessors(N node)
Redeclares com. node which can be reached by
traversing node's incoming edges against the direction (if any) of the edge.
|
| public Set | successors(N node)
Redeclares com. node which can be reached by
traversing node's outgoing edges in the direction (if any) of the edge.
|
| adjacentNodes | back to summary |
|---|---|
| public Set Redeclares com. Returns a live view of the nodes which have an incident edge in common with This is equal to the union of If
| |
| allowsSelfLoops | back to summary |
|---|---|
| public boolean allowsSelfLoops() Redeclares com. Returns true if this graph allows self-loops (edges that connect a node to itself). Attempting
to add a self-loop to a graph that does not allow them will throw an
| |
| asGraph | back to summary |
|---|---|
| public Graph Returns a live view of this graph as a | |
| degree | back to summary |
|---|---|
| public int degree(N node) Redeclares com. Returns the count of For directed graphs, this is equal to For undirected graphs, this is equal to If the count is greater than
| |
| edges | back to summary |
|---|---|
| public Set Redeclares com. Returns all edges in this graph.
| |
| edgeValue | back to summary |
|---|---|
| public Optional Returns the value of the edge that connects
| |
| edgeValue | back to summary |
|---|---|
| public Optional Returns the value of the edge that connects If this graph is directed, the endpoints must be ordered.
| |
| edgeValueOrDefault | back to summary |
|---|---|
| public V edgeValueOrDefault(N nodeU, N nodeV, V defaultValue) Returns the value of the edge that connects In an undirected graph, this is equal to
| |
| edgeValueOrDefault | back to summary |
|---|---|
| public V edgeValueOrDefault(EndpointPair<N> endpoints, V defaultValue) Returns the value of the edge that connects If this graph is directed, the endpoints must be ordered.
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object object) Returns Thus, two value graphs A and B are equal if all of the following are true:
Graph properties besides A reference implementation of this is provided by
| |
| hasEdgeConnecting | back to summary |
|---|---|
| public boolean hasEdgeConnecting(N nodeU, N nodeV) Redeclares com. Returns true if there is an edge that directly connects In an undirected graph, this is equal to
| |
| hasEdgeConnecting | back to summary |
|---|---|
| public boolean hasEdgeConnecting(EndpointPair<N> endpoints) Redeclares com. Returns true if there is an edge that directly connects Unlike the other
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Returns the hash code for this graph. The hash code of a graph is defined as the hash code of a
map from each of its A reference implementation of this is provided by
| |
| incidentEdgeOrder | back to summary |
|---|---|
| public ElementOrder Redeclares com. Returns an
| |
| incidentEdges | back to summary |
|---|---|
| public Set Redeclares com. Returns a live view of the edges in this graph whose endpoints include This is equal to the union of incoming and outgoing edges. If
| |
| inDegree | back to summary |
|---|---|
| public int inDegree(N node) Redeclares com. Returns the count of If the count is greater than
| |
| isDirected | back to summary |
|---|---|
| public boolean isDirected() Redeclares com. Returns true if the edges in this graph are directed. Directed edges connect a
| |
| nodeOrder | back to summary |
|---|---|
| public ElementOrder Redeclares com. Returns the order of iteration for the elements of
| |
| nodes | back to summary |
|---|---|
| public Set Redeclares com. Returns all nodes in this graph, in the order specified by
| |
| outDegree | back to summary |
|---|---|
| public int outDegree(N node) Redeclares com. Returns the count of If the count is greater than
| |
| predecessors | back to summary |
|---|---|
| public Set Redeclares com. Returns a live view of all nodes in this graph adjacent to In an undirected graph, this is equivalent to If
| |
| successors | back to summary |
|---|---|
| public Set Redeclares com. Returns a live view of all nodes in this graph adjacent to In an undirected graph, this is equivalent to This is not the same as "all nodes reachable from If
| |