Interface IntGraphBuilder
-
- All Superinterfaces:
GraphBuilder<Integer,Integer>
- All Known Subinterfaces:
IndexGraphBuilder
public interface IntGraphBuilder extends GraphBuilder<Integer,Integer>
A builder for int graphs.The builder is used to construct non-empty int graphs. Differing from
IntGraphFactory
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.To create a new builder, use one of the static methods
undirected()
,directed()
ornewInstance(boolean)
. For more options, create a newIntGraphFactory
and useIntGraphFactory.newBuilder()
, or useIntGraphFactory.newBuilderCopyOf(Graph)
to create a builder initialized with an existing graph vertices and edges.This interface is a specific version of
GraphBuilder
forIntGraph
.- Author:
- Barak Ugav
- See Also:
undirected()
,directed()
,IndexGraphBuilder
,IntGraphFactory
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default int
addEdge(int source, int target)
Add a new edge to the built graph, using the edge builder.void
addEdge(int source, int target, int edge)
Add a new edge to the built graph.default Integer
addEdge(Integer source, Integer target)
Deprecated.Please useaddEdge(int, int)
instead to avoid un/boxing.default void
addEdge(Integer source, Integer target, Integer edge)
Deprecated.Please useaddEdge(int, int, int)
instead to avoid un/boxing.void
addEdges(EdgeSet<? extends Integer,? extends Integer> edges)
Add multiple edges to the built graph.default Integer
addVertex()
Deprecated.Please useaddVertexInt()
instead to avoid un/boxing.void
addVertex(int vertex)
Add a new vertex to the built graph.default void
addVertex(Integer vertex)
Deprecated.Please useaddVertex(int)
instead to avoid un/boxing.default int
addVertexInt()
Add a new vertex to the built graph, using the vertex builder.void
addVertices(Collection<? extends Integer> vertices)
Add multiple vertices to the built graph.IntGraph
build()
Build a new immutable graph with the builder vertices and edges.IntGraph
buildMutable()
Build a new mutable graph with the builder vertices and edges.static IntGraphBuilder
directed()
Create a new builder that builds directed int graphs.IdBuilderInt
edgeBuilder()
Get the edge builder of this builder.IntSet
edges()
Get the set of edges that were added to the graph.<T,WeightsT extends Weights<Integer,T>>
WeightsTedgesWeights(String key)
Get the edges weights of some key.static IntGraphBuilder
newCopyOf(Graph<Integer,Integer> g)
Create a new builder initialized with an existing graph vertices and edges, without copying the weights.static IntGraphBuilder
newCopyOf(Graph<Integer,Integer> g, boolean copyVerticesWeights, boolean copyEdgesWeights)
Create a new builder initialized with an existing graph vertices and edges, with/without copying the weights.static IntGraphBuilder
newInstance(boolean directed)
Create a new builder that builds un/directed int graphs.static IntGraphBuilder
undirected()
Create a new builder that builds undirected graphs.IdBuilderInt
vertexBuilder()
Get the vertex builder of this builder.IntSet
vertices()
Get the set of vertices that were added to the graph.<T,WeightsT extends Weights<Integer,T>>
WeightsTverticesWeights(String key)
Get the vertices weights of some key.-
Methods inherited from interface com.jgalgo.graph.GraphBuilder
addEdgesWeights, addEdgesWeights, addVerticesWeights, addVerticesWeights, clear, edgesWeightsKeys, ensureEdgeCapacity, ensureVertexCapacity, isDirected, verticesWeightsKeys
-
-
-
-
Method Detail
-
vertices
IntSet vertices()
Description copied from interface:GraphBuilder
Get the set of vertices that were added to the graph.- Specified by:
vertices
in interfaceGraphBuilder<Integer,Integer>
- Returns:
- the graph vertices
-
edges
IntSet edges()
Description copied from interface:GraphBuilder
Get the set of edges that were added to the graph.- Specified by:
edges
in interfaceGraphBuilder<Integer,Integer>
- Returns:
- the graph edges
-
addVertex
void addVertex(int vertex)
Add a new vertex to the built graph.Vertices must be non negative integers.
If there is a vertex builder, namely if
vertexBuilder()
does not returnnull
, the methodaddVertexInt()
can be used, which uses the vertex builder to create the new vertex object instead of requiring the user to provide it.- Parameters:
vertex
- new vertex- Throws:
IllegalArgumentException
- ifvertex
is already in the built graph, or ifvertex
is negative, as negative identifiers are not allowed
-
addVertex
@Deprecated default void addVertex(Integer vertex)
Deprecated.Please useaddVertex(int)
instead to avoid un/boxing.Add a new vertex to the built graph.A vertex can be any non null hashable object, namely it must implement the
Object.hashCode()
andObject.equals(Object)
methods. Duplicate vertices are not allowed.If there is a vertex builder, namely if
GraphBuilder.vertexBuilder()
does not returnnull
, the methodGraphBuilder.addVertex()
can be used, which uses the vertex builder to create the new vertex object instead of requiring the user to provide it.- Specified by:
addVertex
in interfaceGraphBuilder<Integer,Integer>
- Parameters:
vertex
- new vertex- Throws:
IllegalArgumentException
- if the given vertex is negative or any of reasons specified inGraphBuilder.addVertex(Object)
-
addVertexInt
default int addVertexInt()
Add a new vertex to the built graph, using the vertex builder.Unlike
addVertex(int)
in which the vertex is provided by the user, this method uses the vertex builder obtained byvertexBuilder()
to create the new vertex and adds it to the graph.This method is equivalent to:
int vertex = vertexBuilder().build(vertices()); addVertex(vertex); return vertex;
- Returns:
- the new vertex
- Throws:
UnsupportedOperationException
- if the builder does not have a vertex builder, namely ifvertexBuilder()
returnsnull
-
addVertex
@Deprecated default Integer addVertex()
Deprecated.Please useaddVertexInt()
instead to avoid un/boxing.Add a new vertex to the built graph, using the vertex builder.Unlike
GraphBuilder.addVertex(Object)
in which the vertex is provided by the user, this method uses the vertex builder obtained byGraphBuilder.vertexBuilder()
to create the new vertex object and adds it to the graph.This method is equivalent to:
V vertex = vertexBuilder().build(vertices()); addVertex(vertex); return vertex;
- Specified by:
addVertex
in interfaceGraphBuilder<Integer,Integer>
- Returns:
- the new vertex
-
addVertices
void addVertices(Collection<? extends Integer> vertices)
Add multiple vertices to the built graph.A vertex can be any non null hashable object, namely it must implement the
Object.hashCode()
andObject.equals(Object)
methods. Duplicate vertices are not allowed.Prefer to pass
IntCollection
instead ofCollection
<Integer
> as collection of vertices.- Specified by:
addVertices
in interfaceGraphBuilder<Integer,Integer>
- Parameters:
vertices
- new vertices
-
addEdge
void addEdge(int source, int target, int edge)
Add a new edge to the built graph.If the built graph does not support self or parallel edges and the added edge is such edge, an exception will not be thrown. The edges are validated only when the graph is built, and an exception will be thrown only then.
Edges must be non negative integers.
If there is an edge builder, namely if
edgeBuilder()
does not returnnull
, the methodaddEdge(int, int)
can be used, which uses the edge builder to create the new edge object instead of requiring the user to provide it.- Parameters:
source
- a source vertextarget
- a target vertexedge
- a new edge identifier- Throws:
IllegalArgumentException
- ifedge
is already in the graph or if it is negative, as negative identifiers are not allowedNoSuchVertexException
- ifsource
ortarget
are not valid vertices identifiers
-
addEdge
@Deprecated default void addEdge(Integer source, Integer target, Integer edge)
Deprecated.Please useaddEdge(int, int, int)
instead to avoid un/boxing.Add a new edge to the built graph.If the built graph does not support self or parallel edges and the added edge is such edge, an exception will not be thrown. The edges are validated only when the graph is built, and an exception will be thrown only then.
An edge can be any non null hashable object, namely it must implement the
Object.hashCode()
andObject.equals(Object)
methods. Duplicate edges are not allowed.If there is an edge builder, namely if
GraphBuilder.edgeBuilder()
does not returnnull
, the methodGraphBuilder.addEdge(Object, Object)
can be used, which uses the edge builder to create the new edge object instead of requiring the user to provide it.- Specified by:
addEdge
in interfaceGraphBuilder<Integer,Integer>
- Parameters:
source
- a source vertextarget
- a target vertexedge
- a new edge identifier
-
addEdge
default int addEdge(int source, int target)
Add a new edge to the built graph, using the edge builder.Unlike
addEdge(int, int, int)
in which the edge (identifier) is provided by the user, this method uses the edge builder obtained byedgeBuilder()
to create the new edge object and adds it to the graph.If the graph does not support self or parallel edges and the added edge is such edge, an exception will not be thrown. The edges are validated only when the graph is built, and an exception will be thrown only then.
This method is equivalent to:
int edge = edgeBuilder().build(edges()); addEdge(source, target, edge); return edge;
- Parameters:
source
- a source vertextarget
- a target vertex- Returns:
- the new edge
- Throws:
UnsupportedOperationException
- if the builder does not have an edge builder, namely ifedgeBuilder()
returnsnull
NoSuchVertexException
- ifsource
ortarget
are not valid vertices identifiers
-
addEdge
@Deprecated default Integer addEdge(Integer source, Integer target)
Deprecated.Please useaddEdge(int, int)
instead to avoid un/boxing.Add a new edge to the built graph, using the edge builder.Unlike
GraphBuilder.addEdge(Object, Object, Object)
in which the edge (identifier) is provided by the user, this method uses the edge builder obtained byGraphBuilder.edgeBuilder()
to create the new edge object and adds it to the graph.If the built graph does not support self or parallel edges and the added edge is such edge, an exception will not be thrown. The edges are validated only when the graph is built, and an exception will be thrown only then.
This method is equivalent to:
E edge = edgeBuilder().build(edges()); addEdge(source, target, edge); return edge;
- Specified by:
addEdge
in interfaceGraphBuilder<Integer,Integer>
- Parameters:
source
- a source vertextarget
- a target vertex- Returns:
- the new edge
-
addEdges
void addEdges(EdgeSet<? extends Integer,? extends Integer> edges)
Add multiple edges to the built graph.The
EdgeSet
passed to this method contains both the edges themselves (the identifiers) and their endpoints (sources and targets), seeEdgeSet.iterator()
,EdgeIter.source()
,EdgeIter.target()
. AnEdgeSet
can be obtained from one of the methods of aGraph
, or usingEdgeSet.of(Set, Graph)
.An edge can be any non null hashable object, namely it must implement the
Object.hashCode()
andObject.equals(Object)
methods. Duplicate edges are not allowed.In the following snippet, a maximum cardinality matching is computed on a graph, and a new graph containing only the matching edges is created:
Graph<V, E> g = ...; Set<E> matching = MatchingAlgo.newInstance().computeMaximumMatching(g, null).edges(); GraphBuilder<V,E> matchingGraphBuilder = GraphBuilder.undirected(); matchingGraphBuilder.addVertices(g.vertices()); matchingGraphBuilder.addEdges(EdgeSet.of(matching, g)); Graph<V,E> matchingGraph = matchingGraphBuilder.build()
Prefer to pass
IEdgeSet
instead ofEdgeSet
<Integer
,Integer
> as set of edges. SeeIEdgeSet.of(IntSet, IntGraph)
.- Specified by:
addEdges
in interfaceGraphBuilder<Integer,Integer>
- Parameters:
edges
- the set of new edges, from which the edges identifiers as well as the endpoints (source and target) of each edge are accessible (seeEdgeSet.iterator()
,EdgeIter.source()
,EdgeIter.target()
).- Throws:
IllegalArgumentException
- if any of the given edges are negative or any of reasons specified inGraphBuilder.addEdges(EdgeSet)
-
verticesWeights
<T,WeightsT extends Weights<Integer,T>> WeightsT verticesWeights(String key)
Get the vertices weights of some key.See
Weights
for a complete documentation of the weights containers.The return object is always some sub class of
IWeights
, such asIWeightsInt
orIWeightsDouble
.- Specified by:
verticesWeights
in interfaceGraphBuilder<Integer,Integer>
- 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
-
edgesWeights
<T,WeightsT extends Weights<Integer,T>> WeightsT edgesWeights(String key)
Get the edges weights of some key.See
Weights
for a complete documentation of the weights containers.The return object is always some sub class of
IWeights
, such asIWeightsInt
orIWeightsDouble
.- Specified by:
edgesWeights
in interfaceGraphBuilder<Integer,Integer>
- 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
-
vertexBuilder
IdBuilderInt vertexBuilder()
Description copied from interface:GraphBuilder
Get the vertex builder of this builder.The vertex builder is used to create new vertices during the execution of
GraphBuilder.addVertex()
, in which the vertex identifier is not provided by the user. No vertex builder necessarily exists, and this method may return anull
value. In that case,GraphBuilder.addVertex()
cannot be used, onlyGraphBuilder.addVertex(Object)
.- Specified by:
vertexBuilder
in interfaceGraphBuilder<Integer,Integer>
- Returns:
- the vertex builder, or
null
if the there is no vertex builder
-
edgeBuilder
IdBuilderInt edgeBuilder()
Description copied from interface:GraphBuilder
Get the edge builder of this builder.The edge builder is used to create new edges during the execution of
GraphBuilder.addEdge(Object, Object)
, in which the edge identifier is not provided by the user. No edge builder necessarily exists, and this method may return anull
value. In that case,GraphBuilder.addEdge(Object, Object)
cannot be used, onlyGraphBuilder.addEdge(Object, Object, Object)
.- Specified by:
edgeBuilder
in interfaceGraphBuilder<Integer,Integer>
- Returns:
- the edge builder, or
null
if the there is no edge builder
-
build
IntGraph build()
Description copied from interface:GraphBuilder
Build a new immutable graph with the builder vertices and edges.Before the graph is built, the edges are validated. If the graph does not support self or parallel edges and such edges were added to the builder, an exception will be thrown.
- Specified by:
build
in interfaceGraphBuilder<Integer,Integer>
- Returns:
- a new immutable graph with the vertices and edges that were added to the builder.
-
buildMutable
IntGraph buildMutable()
Description copied from interface:GraphBuilder
Build a new mutable graph with the builder vertices and edges.Before the graph is built, the edges are validated. If the graph does not support self or parallel edges and such edges were added to the builder, an exception will be thrown.
- Specified by:
buildMutable
in interfaceGraphBuilder<Integer,Integer>
- Returns:
- a new mutable graph with the vertices and edges that were added to the builder.
-
undirected
static IntGraphBuilder undirected()
Create a new builder that builds undirected graphs.The graphs built by this builder will have the same default capabilities as
IntGraphFactory
, namely they will not support self edges and will support parallel edges. See the factory documentation for more information.For more options to instantiate a builder, create a new
IntGraphFactory
and use one of itsnewBuilder
methods.- Returns:
- a new empty builder for undirected graphs
-
directed
static IntGraphBuilder directed()
Create a new builder that builds directed int graphs.The graphs built by this builder will have the same default capabilities as
IntGraphFactory
, namely they will not support self edges and will support parallel edges. See the factory documentation for more information.For more options to instantiate a builder, create a new
IntGraphFactory
and use one of itsnewBuilder
methods.- Returns:
- a new empty builder for directed graphs
-
newInstance
static IntGraphBuilder newInstance(boolean directed)
Create a new builder that builds un/directed int graphs.The graphs built by this builder will have the same default capabilities as
IntGraphFactory
, namely they will not support self edges and will support parallel edges. See the factory documentation for more information.For more options to instantiate a builder, create a new
IntGraphFactory
and use one of itsnewBuilder
methods.- Parameters:
directed
- iftrue
, the new builder will build directed graphs, otherwise it will build undirected graphs- Returns:
- a new empty builder for un/directed graphs
-
newCopyOf
static IntGraphBuilder newCopyOf(Graph<Integer,Integer> 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.
For more options to instantiate a builder, create a new
IntGraphFactory
and use one of itsnewBuilder
methods.- Parameters:
g
- a graph- Returns:
- a builder initialized with the given graph vertices and edges, without the original graph vertices/edges weights.
-
newCopyOf
static IntGraphBuilder newCopyOf(Graph<Integer,Integer> g, boolean copyVerticesWeights, boolean copyEdgesWeights)
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.
For more options to instantiate a builder, create a new
IntGraphFactory
and use one of itsnewBuilder
methods.- Parameters:
g
- a graphcopyVerticesWeights
- iftrue
, the weights of the vertices will be copied from the graph to the buildercopyEdgesWeights
- iftrue
, the weights of the edges will be copied from the graph to the builder- Returns:
- a builder initialized with the given graph vertices and edges, with/without the original graph vertices/edges weights.
-
-