Package com.google.common.graph
Class GraphBuilder<N>
java.lang.Object
com.google.common.graph.GraphBuilder<N>
A builder for constructing instances of
MutableGraph
with user-defined properties.
A graph built by this class will have the following properties by default:
- does not allow self-loops
- orders
Graph.nodes()
in the order in which the elements were added
Example of use:
MutableGraph<String> graph = GraphBuilder.undirected().allowsSelfLoops(true).build();
graph.putEdge("bread", "bread");
graph.putEdge("chocolate", "peanut butter");
graph.putEdge("peanut butter", "jelly");
- Since:
- 20.0
-
Method Summary
Modifier and TypeMethodDescriptionallowsSelfLoops
(boolean allowsSelfLoops) Specifies whether the graph will allow self-loops (edges that connect a node to itself).<N1 extends N>
MutableGraph<N1> build()
Returns an emptyMutableGraph
with the properties of thisGraphBuilder
.static GraphBuilder
<Object> directed()
Returns aGraphBuilder
for building directed graphs.expectedNodeCount
(int expectedNodeCount) Specifies the expected number of nodes in the graph.static <N> GraphBuilder
<N> Returns aGraphBuilder
initialized with all properties queryable fromgraph
.<N1 extends N>
GraphBuilder<N1> nodeOrder
(ElementOrder<N1> nodeOrder) Specifies the order of iteration for the elements ofGraph.nodes()
.static GraphBuilder
<Object> Returns aGraphBuilder
for building undirected graphs.
-
Method Details
-
directed
Returns aGraphBuilder
for building directed graphs. -
undirected
Returns aGraphBuilder
for building undirected graphs. -
from
Returns aGraphBuilder
initialized with all properties queryable fromgraph
.The "queryable" properties are those that are exposed through the
Graph
interface, such asGraph.isDirected()
. Other properties, such asexpectedNodeCount(int)
, are not set in the new builder. -
allowsSelfLoops
Specifies whether the graph will allow 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 anUnsupportedOperationException
. -
expectedNodeCount
Specifies the expected number of nodes in the graph.- Throws:
IllegalArgumentException
- ifexpectedNodeCount
is negative
-
nodeOrder
Specifies the order of iteration for the elements ofGraph.nodes()
. -
build
Returns an emptyMutableGraph
with the properties of thisGraphBuilder
.
-