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.
Network supports the following use cases (definitions of
terms):
NetworkThe implementation classes that common.graph provides are not public, by design. To
create an instance of one of the built-in implementations of Network, use the NetworkBuilder class:
MutableNetwork<Integer, MyEdge> graph = NetworkBuilder.directed().build();
NetworkBuilder#build() returns an instance of MutableNetwork, which is a
subtype of Network 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 Network interface, or an ImmutableNetwork.
You can create an immutable copy of an existing Network using ImmutableNetwork#copyOf(Network):
ImmutableNetwork<Integer, MyEdge> immutableGraph = ImmutableNetwork.copyOf(graph);
Instances of ImmutableNetwork do not implement MutableNetwork (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 | |
| public Set | adjacentNodes(N node)
Returns the nodes which have an incident edge in common with |
| public boolean | |
| public boolean | allowsSelfLoops()
Returns true if this network allows self-loops (edges that connect a node to itself). |
| public Graph | |
| public int | degree(N node)
Returns the count of |
| public Optional | edgeConnecting(N nodeU, N nodeV)
Returns the single edge that directly connects |
| public Optional | edgeConnecting(EndpointPair<N> endpoints)
Returns the single edge that directly connects |
| public E | edgeConnectingOrNull(N nodeU, N nodeV)
Returns the single edge that directly connects |
| public E | edgeConnectingOrNull(EndpointPair<N> endpoints)
Returns the single edge that directly connects |
| public ElementOrder | |
| public Set | |
| public Set | edgesConnecting(N nodeU, N nodeV)
Returns the set of edges that each directly connect |
| public Set | edgesConnecting(EndpointPair<N> endpoints)
Returns the set of edges that each directly connect |
| public boolean | |
| public boolean | hasEdgeConnecting(N nodeU, N nodeV)
Returns true if there is an edge that directly connects |
| public boolean | hasEdgeConnecting(EndpointPair<N> endpoints)
Returns true if there is an edge that directly connects |
| public int | |
| public Set | |
| public EndpointPair | |
| public int | |
| public Set | inEdges(N node)
Returns all edges in this network which can be traversed in the direction (if any) of the edge
to end at |
| public boolean | |
| public ElementOrder | |
| public Set | |
| public int | |
| public Set | outEdges(N node)
Returns all edges in this network which can be traversed in the direction (if any) of the edge
starting from |
| 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.
|
| adjacentEdges | back to summary |
|---|---|
| public Set Returns the edges which have an
| |
| adjacentNodes | back to summary |
|---|---|
| public Set Returns the nodes which have an incident edge in common with This is equal to the union of
| |
| allowsParallelEdges | back to summary |
|---|---|
| public boolean allowsParallelEdges() Returns true if this network allows parallel edges. Attempting to add a parallel edge to a
network that does not allow them will throw an | |
| allowsSelfLoops | back to summary |
|---|---|
| public boolean allowsSelfLoops() Returns true if this network allows self-loops (edges that connect a node to itself).
Attempting to add a self-loop to a network that does not allow them will throw an | |
| asGraph | back to summary |
|---|---|
| public Graph Returns a live view of this network as a If this network | |
| degree | back to summary |
|---|---|
| public int degree(N node) Returns the count of For directed networks, this is equal to For undirected networks, this is equal to If the count is greater than
| |
| edgeConnecting | back to summary |
|---|---|
| public Optional Returns the single edge that directly connects In an undirected network, this is equal to
| |
| edgeConnecting | back to summary |
|---|---|
| public Optional Returns the single edge that directly connects If this graph is directed, the endpoints must be ordered.
| |
| edgeConnectingOrNull | back to summary |
|---|---|
| public E edgeConnectingOrNull(N nodeU, N nodeV) Returns the single edge that directly connects In an undirected network, this is equal to
| |
| edgeConnectingOrNull | back to summary |
|---|---|
| public E edgeConnectingOrNull(EndpointPair<N> endpoints) Returns the single edge that directly connects If this graph is directed, the endpoints must be ordered.
| |
| edgeOrder | back to summary |
|---|---|
| public ElementOrder Returns the order of iteration for the elements of | |
| edges | back to summary |
|---|---|
| public Set Returns all edges in this network, in the order specified by | |
| edgesConnecting | back to summary |
|---|---|
| public Set Returns the set of edges that each directly connect In an undirected network, this is equal to The resulting set of edges will be parallel (i.e. have equal
| |
| edgesConnecting | back to summary |
|---|---|
| public Set Returns the set of edges that each directly connect The resulting set of edges will be parallel (i.e. have equal If this network is directed,
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object object) Returns Thus, two networks A and B are equal if all of the following are true:
Network properties besides A reference implementation of this is provided by
| |
| hasEdgeConnecting | back to summary |
|---|---|
| public boolean hasEdgeConnecting(N nodeU, N nodeV) 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) 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 network. The hash code of a network is defined as the hash code
of a map from each of its A reference implementation of this is provided by
| |
| incidentEdges | back to summary |
|---|---|
| public Set Returns the edges whose This is equal to the union of
| |
| incidentNodes | back to summary |
|---|---|
| public EndpointPair Returns the nodes which are the endpoints of
| |
| inDegree | back to summary |
|---|---|
| public int inDegree(N node) Returns the count of If the count is greater than
| |
| inEdges | back to summary |
|---|---|
| public Set Returns all edges in this network which can be traversed in the direction (if any) of the edge
to end at In a directed network, an incoming edge's In an undirected network, this is equivalent to
| |
| isDirected | back to summary |
|---|---|
| public boolean isDirected() Returns true if the edges in this network are directed. Directed edges connect a | |
| nodeOrder | back to summary |
|---|---|
| public ElementOrder Returns the order of iteration for the elements of | |
| nodes | back to summary |
|---|---|
| public Set Returns all nodes in this network, in the order specified by | |
| outDegree | back to summary |
|---|---|
| public int outDegree(N node) Returns the count of If the count is greater than
| |
| outEdges | back to summary |
|---|---|
| public Set Returns all edges in this network which can be traversed in the direction (if any) of the edge
starting from In a directed network, an outgoing edge's In an undirected network, this is equivalent to
| |
| predecessors | back to summary |
|---|---|
| public Set Redeclares com. Returns all nodes in this network adjacent to In an undirected network, this is equivalent to
| |
| successors | back to summary |
|---|---|
| public Set Redeclares com. Returns all nodes in this network adjacent to In an undirected network, this is equivalent to This is not the same as "all nodes reachable from
| |