Interface GraphBuilder<V,E>
-
- Type Parameters:
V
- the vertices typeE
- the edges type
- All Known Subinterfaces:
IndexGraphBuilder
,IntGraphBuilder
public interface GraphBuilder<V,E>
A builder for graphs.The builder is used to construct non-empty graphs. Differing from
GraphFactory
which create new empty graphs, the builder is used to add vertices and edges before actually creating the graph. This capability is required to create immutable graphs, but can also be used to build mutable graph and may gain a performance boost compared to creating an empty graph and adding the same vertices and edges.- Author:
- Barak Ugav
- See Also:
newUndirected()
,newDirected()
,GraphFactory
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
addEdge(V source, V target, E edge)
Add a new edge to the graph.default <T,WeightsT extends Weights<E,T>>
WeightsTaddEdgesWeights(String key, Class<? super T> type)
Add a new weights container associated with the edges of the built graph.<T,WeightsT extends Weights<E,T>>
WeightsTaddEdgesWeights(String key, Class<? super T> type, T defVal)
Add a new weights container associated with the edges of the built graph with default value.void
addVertex(V vertex)
Add a new vertex to the graph.default <T,WeightsT extends Weights<V,T>>
WeightsTaddVerticesWeights(String key, Class<? super T> type)
Add a new weights container associated with the vertices of the built graph.<T,WeightsT extends Weights<V,T>>
WeightsTaddVerticesWeights(String key, Class<? super T> type, T defVal)
Add a new weights container associated with the vertices of built graph with default value.Graph<V,E>
build()
Build a new immutable graph with the builder vertices and edges.Graph<V,E>
buildMutable()
Build a new mutable graph with the builder vertices and edges.void
clear()
Clear the builder by removing all vertices and edges added to it.Set<E>
edges()
Get the set of edges that were added to the graph.void
expectedEdgesNum(int edgesNum)
Hint about the number of edges expected to be added to the builder.void
expectedVerticesNum(int verticesNum)
Hint about the number of vertices expected to be added to the builder.<T,WeightsT extends Weights<E,T>>
WeightsTgetEdgesWeights(String key)
Get the edges weights of some key.Set<String>
getEdgesWeightsKeys()
Get the keys of all the associated edges weights.<T,WeightsT extends Weights<V,T>>
WeightsTgetVerticesWeights(String key)
Get the vertices weights of some key.Set<String>
getVerticesWeightsKeys()
Get the keys of all the associated vertices weights.static <V,E>
GraphBuilder<V,E>newDirected()
Create a new builder that builds directed graphs.static <V,E>
GraphBuilder<V,E>newFrom(Graph<V,E> g)
Create a new builder initialized with an existing graph vertices and edges, without copying the weights.static <V,E>
GraphBuilder<V,E>newFrom(Graph<V,E> g, boolean copyWeights)
Create a new builder initialized with an existing graph vertices and edges, with/without copying the weights.static <V,E>
GraphBuilder<V,E>newUndirected()
Create a new builder that builds undirected graphs.Set<V>
vertices()
Get the set of vertices that were added to the graph.
-
-
-
Method Detail
-
vertices
Set<V> vertices()
Get the set of vertices that were added to the graph.- Returns:
- the graph vertices
-
addVertex
void addVertex(V vertex)
Add a new vertex to the graph.- Parameters:
vertex
- the new vertex
-
addEdge
void addEdge(V source, V target, E edge)
Add a new edge to the graph.- Parameters:
source
- the source vertex of the new edgetarget
- the target vertex of the new edgeedge
- the new edge
-
expectedVerticesNum
void expectedVerticesNum(int verticesNum)
Hint about the number of vertices expected to be added to the builder.This method does not affect the built graph, only the builder itself.
- Parameters:
verticesNum
- the expected number of vertices to be added to the builder
-
expectedEdgesNum
void expectedEdgesNum(int edgesNum)
Hint about the number of edges expected to be added to the builder.This method does not affect the built graph, only the builder itself.
- Parameters:
edgesNum
- the expected number of edges to be added to the builder
-
getVerticesWeights
<T,WeightsT extends Weights<V,T>> WeightsT getVerticesWeights(String key)
Get the vertices weights of some key.See
Weights
for a complete documentation of the weights containers.- Type Parameters:
T
- The weight data typeWeightsT
- the weights container, used to avoid casts of containers of primitive types such asWeightsInt
,WeightsDouble
ect.- Parameters:
key
- key of the weights- Returns:
- vertices weights of the key, or
null
if no container found with the specified key
-
addVerticesWeights
default <T,WeightsT extends Weights<V,T>> WeightsT addVerticesWeights(String key, Class<? super T> type)
Add a new weights container associated with the vertices of the built graph.See
Weights
for a complete documentation of the weights containers.- Type Parameters:
T
- The weight data typeWeightsT
- the weights container, used to avoid casts of containers of primitive types such asWeightsInt
,WeightsDouble
ect.- Parameters:
key
- key of the weightstype
- the type of the weights, used for primitive types weights- Returns:
- a new weights container
- Throws:
IllegalArgumentException
- if a vertices weights container with the same key already exists in the graph
-
addVerticesWeights
<T,WeightsT extends Weights<V,T>> WeightsT addVerticesWeights(String key, Class<? super T> type, T defVal)
Add a new weights container associated with the vertices of built graph with default value.See
Weights
for a complete documentation of the weights containers.- Type Parameters:
T
- The weight data typeWeightsT
- the weights container, used to avoid casts of containers of primitive types such asWeightsInt
,WeightsDouble
ect.- Parameters:
key
- key of the weightstype
- the type of the weights, used for primitive types weightsdefVal
- default value use for the weights container- Returns:
- a new weights container
- Throws:
IllegalArgumentException
- if a vertices weights container with the same key already exists in the graph
-
getVerticesWeightsKeys
Set<String> getVerticesWeightsKeys()
Get the keys of all the associated vertices weights.See
IWeights
for a complete documentation of the weights containers.- Returns:
- the keys of all the associated vertices weights
-
getEdgesWeights
<T,WeightsT extends Weights<E,T>> WeightsT getEdgesWeights(String key)
Get the edges weights of some key.See
Weights
for a complete documentation of the weights containers.- Type Parameters:
T
- The weight data typeWeightsT
- the weights container, used to avoid casts of containers of primitive types such asWeightsInt
,WeightsDouble
ect.- Parameters:
key
- key of the weights- Returns:
- edges weights of the key, or
null
if no container found with the specified key
-
addEdgesWeights
default <T,WeightsT extends Weights<E,T>> WeightsT addEdgesWeights(String key, Class<? super T> type)
Add a new weights container associated with the edges of the built graph.See
Weights
for a complete documentation of the weights containers.- Type Parameters:
T
- The weight data typeWeightsT
- the weights container, used to avoid casts of containers of primitive types such asWeightsInt
,WeightsDouble
ect.- Parameters:
key
- key of the weightstype
- the type of the weights, used for primitive types weights- Returns:
- a new weights container
- Throws:
IllegalArgumentException
- if a edges weights container with the same key already exists in the graph
-
addEdgesWeights
<T,WeightsT extends Weights<E,T>> WeightsT addEdgesWeights(String key, Class<? super T> type, T defVal)
Add a new weights container associated with the edges of the built graph with default value.See
Weights
for a complete documentation of the weights containers.- Type Parameters:
T
- The weight data typeWeightsT
- the weights container, used to avoid casts of containers of primitive types such asWeightsInt
,WeightsDouble
ect.- Parameters:
key
- key of the weightstype
- the type of the weights, used for primitive types weightsdefVal
- default value use for the weights container- Returns:
- a new weights container
- Throws:
IllegalArgumentException
- if a edges weights container with the same key already exists in the graph
-
getEdgesWeightsKeys
Set<String> getEdgesWeightsKeys()
Get the keys of all the associated edges weights.See
IWeights
for a complete documentation of the weights containers.- Returns:
- the keys of all the associated edges weights
-
clear
void clear()
Clear the builder by removing all vertices and edges added to it.
-
build
Graph<V,E> build()
Build a new immutable graph with the builder vertices and edges.- Returns:
- a new immutable graph with the vertices and edges that were added to the builder.
-
buildMutable
Graph<V,E> buildMutable()
Build a new mutable graph with the builder vertices and edges.- Returns:
- a new mutable graph with the vertices and edges that were added to the builder.
-
newUndirected
static <V,E> GraphBuilder<V,E> newUndirected()
Create a new builder that builds undirected graphs.- Type Parameters:
V
- the vertices typeE
- the edges type- Returns:
- a new empty builder for undirected graphs
-
newDirected
static <V,E> GraphBuilder<V,E> newDirected()
Create a new builder that builds directed graphs.- Type Parameters:
V
- the vertices typeE
- the edges type- Returns:
- a new empty builder for directed graphs
-
newFrom
static <V,E> GraphBuilder<V,E> newFrom(Graph<V,E> g)
Create a new builder initialized with an existing graph vertices and edges, without copying the weights.If the given graph is directed, the new builder will build directed graphs, and similarly for undirected graphs.
- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graph- Returns:
- a builder initialized with the given graph vertices and edges, without the original graph vertices/edges weights.
-
newFrom
static <V,E> GraphBuilder<V,E> newFrom(Graph<V,E> g, boolean copyWeights)
Create a new builder initialized with an existing graph vertices and edges, with/without copying the weights.If the given graph is directed, the new builder will build directed graphs, and similarly for undirected graphs.
- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphcopyWeights
- iftrue
, the weights of the vertices and edges will be copied to the new graph- Returns:
- a builder initialized with the given graph vertices and edges, with/without the original graph vertices/edges weights.
-
-