Interface IntGraph
-
- All Known Subinterfaces:
IndexGraph
public interface IntGraph extends Graph<Integer,Integer>
A discrete graph withint
vertices and edges.This interface is a specification of
Graph
for graphs withint
vertices and edges, similarly howInt2IntMap
is a specification ofMap
for maps withint
keys and values. Methods that accept a primitiveint
as an identifier are provided, and the original ones that accept aInteger
are deprecated. SpecificIEdgeSet
andIEdgeIter
are returned for edges queries, avoiding boxing of integers. Similarly, theIWeights
interface is used for weights containers, which accept primitiveint
as identifiers.Each vertex and edge in the graph is identified by a unique non negative
int
identifier. Vertices and edges may be created byaddVertexInt()
andaddEdge(int, int)
, in which case the graph implementation will choose theint
identifier and will return it to the user. Alternatively, the methodsaddVertex(int)
andaddEdge(int, int, int)
(similar the regularGraph
methods) can be used to add new vertices and edges with user chosen identifiers.Implementations of this interface are more efficient than the generic
Graph
interface, and should be used for better performance if its needed. For even better performance, consider usingIndexGraph
, which does violate theGraph
interface as its vertices and edges may change during the lifetime of the graph and therefore less user friendly, but is even more efficient.To create a new empty graph, use
newUndirected()
ornewDirected()
. The returned graph will use the default implementation. For more control over the graph details, seeIntGraphFactory
. To construct an immutable graph, useIntGraphBuilder
.// Create a directed graph with three vertices and edges between them IntGraph g = IntGraph.newDirected(); int v1 = g.addVertex(); int v2 = g.addVertex(); int v3 = g.addVertex(); int e1 = g.addEdge(v1, v2); int e2 = g.addEdge(v2, v3); int e3 = g.addEdge(v1, v3); // Assign some weights to the edges IWeightsDouble weights = g.addEdgesWeights("weightsKey", double.class); weights.set(e1, 1.2); weights.set(e2, 3.1); weights.set(e3, 15.1); IWeightFunction weightFunc = weights; // Calculate the shortest paths from v1 to all other vertices ShortestPathSingleSource ssspAlgo = ShortestPathSingleSource.newInstance(); ShortestPathSingleSource.Result ssspRes = ssspAlgo.computeShortestPaths(g, weightFunc, v1); // Print the shortest path from v1 to v3 assert ssspRes.distance(v3) == 4.3; assert ssspRes.getPath(v3).edges().equals(IntList.of(e1, e2)); System.out.println("Distance from v1 to v3 is: " + ssspRes.distance(v3)); System.out.println("The shortest path from v1 to v3 is:"); for (int e : ssspRes.getPath(v3).edges()) { int u = g.edgeSource(e), v = g.edgeTarget(e); System.out.println(" " + e + "(" + u + ", " + v + ")"); }
- Author:
- Barak Ugav
- See Also:
IntGraphFactory
,IntGraphBuilder
,IndexGraph
-
-
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 graph, using the edge builder.void
addEdge(int source, int target, int edge)
Add a new edge to the 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 graph.default Integer
addVertex()
Deprecated.Please useaddVertexInt()
instead to avoid un/boxing.void
addVertex(int vertex)
Add a new vertex to the graph.default void
addVertex(Integer vertex)
Deprecated.Please useaddVertex(int)
instead to avoid un/boxing.default int
addVertexInt()
Add a new vertex to the graph, using the vertex builder.void
addVertices(Collection<? extends Integer> vertices)
Add multiple vertices to the graph.default boolean
containsEdge(int source, int target)
Check whether the graph contains an edge with the given source and target.default boolean
containsEdge(Integer source, Integer target)
Deprecated.default IntGraph
copy()
Create a copy of this graph, with the same vertices and edges, without copying weights.default IntGraph
copy(boolean copyVerticesWeights, boolean copyEdgesWeights)
Create a copy of this graph, with the same vertices and edges, with/without copying weights.IdBuilderInt
edgeBuilder()
Get the edge builder of this graph.int
edgeEndpoint(int edge, int endpoint)
Get the other end-point of an edge.default Integer
edgeEndpoint(Integer edge, Integer endpoint)
Deprecated.Please useedgeEndpoint(int, int)
instead to avoid un/boxing.IntSet
edges()
Get the set of all edges of the graph.int
edgeSource(int edge)
Get the source vertex of an edge.default Integer
edgeSource(Integer edge)
Deprecated.Please useedgeSource(int)
instead to avoid un/boxing.<T,WeightsT extends Weights<Integer,T>>
WeightsTedgesWeights(String key)
Get the edges weights of some key.int
edgeTarget(int edge)
Get the target vertex of an edge.default Integer
edgeTarget(Integer edge)
Deprecated.Please useedgeTarget(int)
instead to avoid un/boxing.int
getEdge(int source, int target)
Get the edge whose source issource
and target istarget
.default Integer
getEdge(Integer source, Integer target)
Deprecated.Please usegetEdge(int, int)
instead to avoid un/boxing.IEdgeSet
getEdges(int source, int target)
Get the edges whose source issource
and target istarget
.default IEdgeSet
getEdges(Integer source, Integer target)
Deprecated.Please usegetEdges(int, int)
instead to avoid un/boxing.default IntGraph
immutableCopy()
Create an immutable copy of this graph, with the same vertices and edges, without copying weights.default IntGraph
immutableCopy(boolean copyVerticesWeights, boolean copyEdgesWeights)
Create an immutable copy of this graph, with the same vertices and edges, with/without copying weights.default IntGraph
immutableView()
Get an immutable view of this graph.IndexIntIdMap
indexGraphEdgesMap()
Get the index-id edges mapping of this graph.IndexIntIdMap
indexGraphVerticesMap()
Get the index-id vertices mapping of this graph.IEdgeSet
inEdges(int target)
Get the edges whose target istarget
.default IEdgeSet
inEdges(Integer target)
Deprecated.Please useinEdges(int)
instead to avoid un/boxing.void
moveEdge(int edge, int newSource, int newTarget)
Move an existing edge to new source and target vertices.default void
moveEdge(Integer edge, Integer newSource, Integer newTarget)
Deprecated.Please usemoveEdge(int, int, int)
instead to avoid un/boxing.static IntGraph
newDirected()
Create a new directed empty int graph.static IntGraph
newUndirected()
Create a new undirected empty int graph.IEdgeSet
outEdges(int source)
Get the edges whose source issource
.default IEdgeSet
outEdges(Integer source)
Deprecated.Please useoutEdges(int)
instead to avoid un/boxing.void
removeEdge(int edge)
Remove an edge from the graph.default void
removeEdge(Integer edge)
Deprecated.Please useremoveEdge(int)
instead to avoid un/boxing.void
removeEdgesOf(int vertex)
Remove all the edges of a vertex.default void
removeEdgesOf(Integer vertex)
Deprecated.Please useremoveEdgesOf(int)
instead to avoid un/boxing.void
removeInEdgesOf(int target)
Remove all edges whose target istarget
.default void
removeInEdgesOf(Integer vertex)
Deprecated.Please useremoveInEdgesOf(int)
instead to avoid un/boxing.void
removeOutEdgesOf(int source)
Remove all edges whose source issource
.default void
removeOutEdgesOf(Integer vertex)
Deprecated.Please useremoveOutEdgesOf(int)
instead to avoid un/boxing.void
removeVertex(int vertex)
Remove a vertex and all its edges from the graph.default void
removeVertex(Integer vertex)
Deprecated.Please useremoveVertex(int)
instead to avoid un/boxing.void
renameEdge(int edge, int newId)
Set a new identifier for an existing edge.default void
renameEdge(Integer edge, Integer newId)
Deprecated.Please userenameEdge(int, int)
instead to avoid un/boxing.void
renameVertex(int vertex, int newId)
Set a new identifier for an existing vertex.default void
renameVertex(Integer vertex, Integer newId)
Deprecated.Please userenameVertex(int, int)
instead to avoid un/boxing.default void
reverseEdge(int edge)
Reverse an edge by switching its source and target.default void
reverseEdge(Integer edge)
Deprecated.Please usereverseEdge(int)
instead to avoid un/boxing.default IntGraph
reverseView()
Get a reversed view of this graph.default IntGraph
subGraphCopy(Collection<Integer> vertices, Collection<Integer> edges)
Create a new graph that is a subgraph of this graph.default IntGraph
undirectedView()
Get an undirected view of this (directed) graph.IdBuilderInt
vertexBuilder()
Get the vertex builder of this graph.IntSet
vertices()
Get the set of all vertices of 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.Graph
addEdgesWeights, addEdgesWeights, addVerticesWeights, addVerticesWeights, clear, clearEdges, edgesWeightsKeys, ensureEdgeCapacity, ensureVertexCapacity, indexGraph, isAllowParallelEdges, isAllowSelfEdges, isDirected, removeEdges, removeEdgesWeights, removeVertices, removeVerticesWeights, verticesWeightsKeys
-
-
-
-
Method Detail
-
vertices
IntSet vertices()
Description copied from interface:Graph
Get the set of all vertices of the graph.Each vertex in the graph is identified by a unique non null hashable object and the returned set is a set of all these identifiers.
The Graph object does not expose an explicit method to get the number of vertices, but it can accessed using this method by
g.vertices().size()
.
-
edges
IntSet edges()
Description copied from interface:Graph
Get the set of all edges of the graph.Each edge in the graph is identified by a unique non null hashable object, and the returned set is a set of all these identifiers.
The Graph object does not expose an explicit method to get the number of edges, but it can accessed using this method by
g.edges().size()
.
-
addVertex
void addVertex(int vertex)
Add a new vertex to the graph.Vertices must be non negative integers.
If the graph have 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
- if the provided identifier is already used as identifier of one of the graph vertices, or if its negative
-
addVertex
@Deprecated default void addVertex(Integer vertex)
Deprecated.Please useaddVertex(int)
instead to avoid un/boxing.Add a new vertex to the 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 the graph have a vertex builder, namely if
Graph.vertexBuilder()
does not returnnull
, the methodGraph.addVertex()
can be used, which uses the vertex builder to create the new vertex object instead of requiring the user to provide it.
-
addVertexInt
default int addVertexInt()
Add a new vertex to the 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 object 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 graph 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 graph, using the vertex builder.Unlike
Graph.addVertex(Object)
in which the vertex is provided by the user, this method uses the vertex builder obtained byGraph.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;
-
addVertices
void addVertices(Collection<? extends Integer> vertices)
Add multiple vertices to the 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 interfaceGraph<Integer,Integer>
- Parameters:
vertices
- new vertices
-
removeVertex
void removeVertex(int vertex)
Remove a vertex and all its edges from the graph.- Parameters:
vertex
- the vertex identifier to remove- Throws:
NoSuchVertexException
- ifvertex
is not a valid vertex identifier
-
removeVertex
@Deprecated default void removeVertex(Integer vertex)
Deprecated.Please useremoveVertex(int)
instead to avoid un/boxing.Remove a vertex and all its edges from the graph.- Specified by:
removeVertex
in interfaceGraph<Integer,Integer>
- Parameters:
vertex
- the vertex identifier to remove
-
renameVertex
void renameVertex(int vertex, int newId)
Set a new identifier for an existing vertex.This method changes the identifier of an existing vertex, while keeping the edges connecting to it, along with the weights associated with it.
- Parameters:
vertex
- an existing vertex in the graphnewId
- the new vertex identifier- Throws:
NoSuchVertexException
- ifvertex
is not a valid vertex identifierIllegalArgumentException
- ifnewId
is already in the graph or ifnewId
isnull
-
renameVertex
@Deprecated default void renameVertex(Integer vertex, Integer newId)
Deprecated.Please userenameVertex(int, int)
instead to avoid un/boxing.Set a new identifier for an existing vertex.This method changes the identifier of an existing vertex, while keeping the edges connecting to it, along with the weights associated with it.
- Specified by:
renameVertex
in interfaceGraph<Integer,Integer>
- Parameters:
vertex
- an existing vertex in the graphnewId
- the new vertex identifier
-
outEdges
IEdgeSet outEdges(int source)
Get the edges whose source issource
.In case the graph is undirected, the set will contain all edges whose
source
is one of their end points.The graph object does not expose an explicit method to get the (out) degree of a vertex, but it can accessed using this method by
g.outEdges(vertex).size()
.- Parameters:
source
- a source vertex- Returns:
- all the edges whose source is
source
- Throws:
NoSuchVertexException
- ifsource
is not a valid vertex identifier
-
outEdges
@Deprecated default IEdgeSet outEdges(Integer source)
Deprecated.Please useoutEdges(int)
instead to avoid un/boxing.Get the edges whose source issource
.In case the graph is undirected, the set will contain all edges whose
source
is one of their end points.The graph object does not expose an explicit method to get the (out) degree of a vertex, but it can accessed using this method by
g.outEdges(vertex).size()
.
-
inEdges
IEdgeSet inEdges(int target)
Get the edges whose target istarget
.In case the graph is undirected, the set will contain all edges whose
target
is one of their end points.The graph object does not expose an explicit method to get the (in) degree of a vertex, but it can accessed using this method by
g.inEdges(vertex).size()
.- Parameters:
target
- a target vertex- Returns:
- all the edges whose target is
target
- Throws:
NoSuchVertexException
- iftarget
is not a valid vertex identifier
-
inEdges
@Deprecated default IEdgeSet inEdges(Integer target)
Deprecated.Please useinEdges(int)
instead to avoid un/boxing.Get the edges whose target istarget
.In case the graph is undirected, the set will contain all edges whose
target
is one of their end points.The graph object does not expose an explicit method to get the (in) degree of a vertex, but it can accessed using this method by
g.inEdges(vertex).size()
.
-
containsEdge
default boolean containsEdge(int source, int target)
Check whether the graph contains an edge with the given source and target.If the graph is undirected, the method will return
true
if there is an edge whose end-points aresource
andtarget
, regardless of which is the source and which is the target.- Parameters:
source
- the source vertextarget
- the target vertex- Returns:
true
if the graph contains an edge with the given source and target,false
otherwise- Throws:
NoSuchVertexException
- ifsource
ortarget
are not valid vertices identifiers
-
containsEdge
@Deprecated default boolean containsEdge(Integer source, Integer target)
Deprecated.Description copied from interface:Graph
Check whether the graph contains an edge with the given source and target.If the graph is undirected, the method will return
true
if there is an edge whose end-points aresource
andtarget
, regardless of which is the source and which is the target.- Specified by:
containsEdge
in interfaceGraph<Integer,Integer>
- Parameters:
source
- the source vertextarget
- the target vertex- Returns:
true
if the graph contains an edge with the given source and target,false
otherwise
-
getEdge
int getEdge(int source, int target)
Get the edge whose source issource
and target istarget
.If the graph is not directed, the return edge is an edge that its end-points are
source
andtarget
.In case there are multiple (parallel) edges between
source
andtarget
, a single arbitrary one is returned.- Parameters:
source
- a source vertextarget
- a target vertex- Returns:
- id of the edge or
-1
if no such edge exists - Throws:
NoSuchVertexException
- ifsource
ortarget
are not valid vertices identifiers
-
getEdge
@Deprecated default Integer getEdge(Integer source, Integer target)
Deprecated.Please usegetEdge(int, int)
instead to avoid un/boxing.Get the edge whose source issource
and target istarget
.If the graph is not directed, the return edge is an edge that its end-points are
source
andtarget
.In case there are multiple (parallel) edges between
source
andtarget
, a single arbitrary one is returned.
-
getEdges
IEdgeSet getEdges(int source, int target)
Get the edges whose source issource
and target istarget
.- Parameters:
source
- a source vertextarget
- a target vertex- Returns:
- all the edges whose source is
source
and target istarget
- Throws:
NoSuchVertexException
- ifsource
ortarget
are not valid vertices identifiers
-
getEdges
@Deprecated default IEdgeSet getEdges(Integer source, Integer target)
Deprecated.Please usegetEdges(int, int)
instead to avoid un/boxing.Get the edges whose source issource
and target istarget
.
-
addEdge
void addEdge(int source, int target, int edge)
Add a new edge to the graph.If the graph does not support parallel edges, and an edge between
source
andtarget
already exists, an exception will be raised. If the graph does not support self edges, andsource
andtarget
are the same vertex, an exception will be raised.Edges must be non negative integers.
If the graph have 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 the graph does not support parallel edges and an edge betweensource
andtarget
already exists or if the graph does not support self edges andsource
andtarget
are the same vertexNullPointerException
- ifedge
isnull
NoSuchVertexException
- 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 graph.If the graph does not support parallel edges, and an edge between
source
andtarget
already exists, an exception will be raised. If the graph does not support self edges, andsource
andtarget
are the same vertex, an exception will be raised.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 the graph have an edge builder, namely if
Graph.edgeBuilder()
does not returnnull
, the methodGraph.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.
-
addEdge
default int addEdge(int source, int target)
Add a new edge to the 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 parallel edges, and an edge between
source
andtarget
already exists, an exception will be raised. If the graph does not support self edges, andsource
andtarget
are the same vertex, an exception will be raised.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 graph does not have an edge builder, namely ifedgeBuilder()
returnsnull
IllegalArgumentException
- if the graph does not support parallel edges and an edge betweensource
andtarget
already exists or if the graph does not support self edges andsource
andtarget
are the same vertexNoSuchVertexException
- 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 graph, using the edge builder.Unlike
Graph.addEdge(Object, Object, Object)
in which the edge (identifier) is provided by the user, this method uses the edge builder obtained byGraph.edgeBuilder()
to create the new edge object and adds it to the graph.If the graph does not support parallel edges, and an edge between
source
andtarget
already exists, an exception will be raised. If the graph does not support self edges, andsource
andtarget
are the same vertex, an exception will be raised.This method is equivalent to:
E edge = edgeBuilder().build(edges()); addEdge(source, target, edge); return edge;
-
addEdges
void addEdges(EdgeSet<? extends Integer,? extends Integer> edges)
Add multiple edges to the 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 anotherGraph
, or usingEdgeSet.of(Set, Graph)
.If the graph does not support self edges and one of the added edges have the same vertex as source and target, an exception will be thrown. If the graph does not support parallel edges, and one of the added edges have the same source and target as one of the existing edges in the graph, or if two of the added edges have the same source and target, an exception will be thrown.
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(); Graph<V,E> matchingGraph = Graph.newUndirected(); matchingGraph.addVertices(g.vertices()); matchingGraph.addEdges(EdgeSet.of(matching, g));
Prefer to pass
IEdgeSet
instead ofEdgeSet
<Integer
,Integer
> as set of edges. SeeIEdgeSet.of(IntSet, IntGraph)
.- Specified by:
addEdges
in interfaceGraph<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()
).
-
removeEdge
void removeEdge(int edge)
Remove an edge from the graph.- Parameters:
edge
- the edge identifier- Throws:
NoSuchEdgeException
- ifedge
is not a valid edge identifier
-
removeEdge
@Deprecated default void removeEdge(Integer edge)
Deprecated.Please useremoveEdge(int)
instead to avoid un/boxing.Remove an edge from the graph.- Specified by:
removeEdge
in interfaceGraph<Integer,Integer>
- Parameters:
edge
- the edge to remove
-
removeEdgesOf
void removeEdgesOf(int vertex)
Remove all the edges of a vertex.- Parameters:
vertex
- a vertex in the graph- Throws:
NoSuchVertexException
- ifvertex
is not a valid vertex identifier
-
removeEdgesOf
@Deprecated default void removeEdgesOf(Integer vertex)
Deprecated.Please useremoveEdgesOf(int)
instead to avoid un/boxing.Remove all the edges of a vertex.- Specified by:
removeEdgesOf
in interfaceGraph<Integer,Integer>
- Parameters:
vertex
- a vertex in the graph
-
removeOutEdgesOf
void removeOutEdgesOf(int source)
Remove all edges whose source issource
.- Parameters:
source
- a vertex in the graph- Throws:
NoSuchVertexException
- ifsource
is not a valid vertex identifier
-
removeOutEdgesOf
@Deprecated default void removeOutEdgesOf(Integer vertex)
Deprecated.Please useremoveOutEdgesOf(int)
instead to avoid un/boxing.Remove all edges whose source issource
.- Specified by:
removeOutEdgesOf
in interfaceGraph<Integer,Integer>
- Parameters:
vertex
- a vertex in the graph
-
removeInEdgesOf
void removeInEdgesOf(int target)
Remove all edges whose target istarget
.- Parameters:
target
- a vertex in the graph- Throws:
NoSuchVertexException
- iftarget
is not a valid vertex identifier
-
removeInEdgesOf
@Deprecated default void removeInEdgesOf(Integer vertex)
Deprecated.Please useremoveInEdgesOf(int)
instead to avoid un/boxing.Remove all edges whose target istarget
.- Specified by:
removeInEdgesOf
in interfaceGraph<Integer,Integer>
- Parameters:
vertex
- a vertex in the graph
-
renameEdge
void renameEdge(int edge, int newId)
Set a new identifier for an existing edge.This method changes the identifier of an existing edge, while keeping the source and target of the edge, along with the weights associated with it.
- Parameters:
edge
- an existing edge in the graphnewId
- the new edge identifier- Throws:
NoSuchEdgeException
- ifedge
is not a valid edge identifierIllegalArgumentException
- ifnewId
is already in the graph or ifnewId
is negative
-
renameEdge
@Deprecated default void renameEdge(Integer edge, Integer newId)
Deprecated.Please userenameEdge(int, int)
instead to avoid un/boxing.Set a new identifier for an existing edge.This method changes the identifier of an existing edge, while keeping the source and target of the edge, along with the weights associated with it.
- Specified by:
renameEdge
in interfaceGraph<Integer,Integer>
- Parameters:
edge
- an existing edge in the graphnewId
- the new edge identifier
-
moveEdge
void moveEdge(int edge, int newSource, int newTarget)
Move an existing edge to new source and target vertices.This method changes the source and target of an existing edge, while keeping the identifier of the edge and the weights associated with it.
- Parameters:
edge
- an existing edge in the graphnewSource
- the new source vertexnewTarget
- the new target vertex- Throws:
NoSuchEdgeException
- ifedge
is not a valid edge identifierNoSuchVertexException
- ifnewSource
ornewTarget
are not valid vertices identifiersIllegalArgumentException
- ifnewSource
andnewTarget
are the same vertex and the graph does not support self edges, or if the graph does not support parallel edges and an edge betweennewSource
andnewTarget
already exists
-
moveEdge
@Deprecated default void moveEdge(Integer edge, Integer newSource, Integer newTarget)
Deprecated.Please usemoveEdge(int, int, int)
instead to avoid un/boxing.Move an existing edge to new source and target vertices.This method changes the source and target of an existing edge, while keeping the identifier of the edge and the weights associated with it.
-
reverseEdge
default void reverseEdge(int edge)
Reverse an edge by switching its source and target.- Parameters:
edge
- an existing edge in the graph- Throws:
NoSuchEdgeException
- ifedge
is not a valid edge identifierIllegalArgumentException
- if the graph does not support parallel edges and another edge which is the reverse ofedge
already exists in the graph
-
reverseEdge
@Deprecated default void reverseEdge(Integer edge)
Deprecated.Please usereverseEdge(int)
instead to avoid un/boxing.Reverse an edge by switching its source and target.- Specified by:
reverseEdge
in interfaceGraph<Integer,Integer>
- Parameters:
edge
- an existing edge in the graph
-
edgeSource
int edgeSource(int edge)
Get the source vertex of an edge.If the graph is undirected, this function return an arbitrary end-point of the edge, but always other end-point than
edgeTarget(int)
returns.- Parameters:
edge
- the edge identifier- Returns:
- the edge source vertex
- Throws:
NoSuchEdgeException
- ifedge
is not a valid edge identifier
-
edgeSource
@Deprecated default Integer edgeSource(Integer edge)
Deprecated.Please useedgeSource(int)
instead to avoid un/boxing.Get the source vertex of an edge.If the graph is undirected, this function return an arbitrary end-point of the edge, but always other end-point than
Graph.edgeTarget(Object)
returns.- Specified by:
edgeSource
in interfaceGraph<Integer,Integer>
- Parameters:
edge
- the edge identifier- Returns:
- the edge source vertex
-
edgeTarget
int edgeTarget(int edge)
Get the target vertex of an edge.If the graph is undirected, this function return an arbitrary end-point of the edge, but always the other end-point than
edgeSource(int)
returns.- Parameters:
edge
- the edge identifier- Returns:
- the edge target vertex
- Throws:
NoSuchEdgeException
- ifedge
is not a valid edge identifier
-
edgeTarget
@Deprecated default Integer edgeTarget(Integer edge)
Deprecated.Please useedgeTarget(int)
instead to avoid un/boxing.Get the target vertex of an edge.If the graph is undirected, this function return an arbitrary end-point of the edge, but always the other end-point than
Graph.edgeSource(Object)
returns.- Specified by:
edgeTarget
in interfaceGraph<Integer,Integer>
- Parameters:
edge
- the edge identifier- Returns:
- the edge target vertex
-
edgeEndpoint
int edgeEndpoint(int edge, int endpoint)
Get the other end-point of an edge.Given an edge \((u,v)\) and a vertex \(w\), assuming \(w\) is an endpoint of the edge, namely that \(w\) is either \(u\) or \(v\), the method will return the other endpoint which is not \(w\). If \(w=u\) the method will return \(v\), if \(w=v\) the method will return \(u\).
- Parameters:
edge
- an edge identifierendpoint
- one of the edge end-point- Returns:
- the other end-point of the edge
- Throws:
NoSuchEdgeException
- ifedge
is not a valid edge identifierNoSuchVertexException
- ifendpoint
is not a valid vertex identifierIllegalArgumentException
- ifendpoint
is not an endpoint of the edge
-
edgeEndpoint
@Deprecated default Integer edgeEndpoint(Integer edge, Integer endpoint)
Deprecated.Please useedgeEndpoint(int, int)
instead to avoid un/boxing.Get the other end-point of an edge.Given an edge \((u,v)\) and a vertex \(w\), assuming \(w\) is an endpoint of the edge, namely that \(w\) is either \(u\) or \(v\), the method will return the other endpoint which is not \(w\). If \(w=u\) the method will return \(v\), if \(w=v\) the method will return \(u\).
- Specified by:
edgeEndpoint
in interfaceGraph<Integer,Integer>
- Parameters:
edge
- an edge identifierendpoint
- one of the edge end-point- Returns:
- the other end-point of the edge
-
vertexBuilder
IdBuilderInt vertexBuilder()
Description copied from interface:Graph
Get the vertex builder of this graph.The vertex builder is used to create new vertices in the graph during the execution of
Graph.addVertex()
, in which the vertex identifier is not provided by the user. Not all graphs have a vertex builder, and may return anull
value. In that case,Graph.addVertex()
cannot be used, onlyGraph.addVertex(Object)
.- Specified by:
vertexBuilder
in interfaceGraph<Integer,Integer>
- Returns:
- the vertex builder of this graph, or
null
if the graph does not have a vertex builder
-
edgeBuilder
IdBuilderInt edgeBuilder()
Description copied from interface:Graph
Get the edge builder of this graph.The edge builder is used to create new edges in the graph during the execution of
Graph.addEdge(Object, Object)
, in which the edge identifier is not provided by the user. Not all graphs have an edge builder, and may return anull
value. In that case,Graph.addEdge(Object, Object)
cannot be used, onlyGraph.addEdge(Object, Object, Object)
.- Specified by:
edgeBuilder
in interfaceGraph<Integer,Integer>
- Returns:
- the edge builder of this graph, or
null
if the graph does not have an edge builder
-
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 interfaceGraph<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 interfaceGraph<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
-
indexGraphVerticesMap
IndexIntIdMap indexGraphVerticesMap()
Description copied from interface:Graph
Get the index-id vertices mapping of this graph.A regular graph contains vertices and edges which are identified by a fixed
int
IDs. AnIndexGraph
view is provided by theGraph.indexGraph()
method, which is a graph in which all methods are accessed with indices rather than fixed IDs. This method expose the mapping between the indices and the fixed IDs of the graph vertices.Note that the mapping may change during the graph lifetime, as vertices are added and removed from the graph, and a regular graph IDs are fixed, while a index graph indices are always
(0,1,2, ...,verticesNum-1)
. The returned mapping object will be updated automatically in such cases.- Specified by:
indexGraphVerticesMap
in interfaceGraph<Integer,Integer>
- Returns:
- a mapping that map vertices IDs to vertices indices
-
indexGraphEdgesMap
IndexIntIdMap indexGraphEdgesMap()
Description copied from interface:Graph
Get the index-id edges mapping of this graph.A regular graph contains vertices and edges which are identified by a fixed
int
IDs. AnIndexGraph
view is provided by theGraph.indexGraph()
method, which is a graph in which all methods are accessed with indices rather than fixed IDs. This method expose the mapping between the indices and the fixed IDs of the graph edges.Note that the mapping may change during the graph lifetime, as edges are added and removed from the graph, and a regular graph IDs are fixed, while a index graph indices are always
(0,1,2, ...,edgesNum-1)
. The returned mapping object will be updated automatically in such cases.- Specified by:
indexGraphEdgesMap
in interfaceGraph<Integer,Integer>
- Returns:
- a mapping that map edges IDs to edges indices
-
copy
default IntGraph copy()
Description copied from interface:Graph
Create a copy of this graph, with the same vertices and edges, without copying weights.An identical copy of this graph will be created, with the same vertices, edges, capabilities (inclusive) such as self edges and parallel edges support, without copying the vertices/edges weights. The returned graph will always be mutable, with no side affects on the original graph.
-
copy
default IntGraph copy(boolean copyVerticesWeights, boolean copyEdgesWeights)
Description copied from interface:Graph
Create a copy of this graph, with the same vertices and edges, with/without copying weights.An identical copy of this graph will be created, with the same vertices, edges, capabilities (inclusive) such as self edges and parallel edges support, with/without copying the vertices/edges weights. The returned graph will always be mutable, with no side affects on the original graph.
Note that although
g.equals(g.copy())
is alwaystrue
if bothcopyVerticesWeights
copyEdgesWeights
aretrue
, there is no guarantee thatg.indexGraph().equals(g.copy().indexGraph())
. Namely, when the graph is copied, new indices may be assigned to the vertices and edges.- Specified by:
copy
in interfaceGraph<Integer,Integer>
- Parameters:
copyVerticesWeights
- iftrue
, the weights of the vertices will be copied to the new graphcopyEdgesWeights
- iftrue
, the weights of the edges will be copied to the new graph- Returns:
- an identical copy of the given graph, with the same vertices and edges, with/without this graph weights
-
immutableCopy
default IntGraph immutableCopy()
Description copied from interface:Graph
Create an immutable copy of this graph, with the same vertices and edges, without copying weights.An identical copy of this graph will be created, with the same vertices and edges, without copying the vertices/edges weights. The returned graph will be immutable, and no vertices/edges/weights can be added or removed from it.
A more compact and efficient representation may be used for the graph, if its known that it will not be changed in the future. It may be more efficient to create an immutable copy of a graph and pass the copy to algorithms instead of using the original graph.
Note that although
g.equals(g.immutableCopy())
is alwaystrue
, there is no guarantee thatg.indexGraph().equals(g.immutableCopy().indexGraph())
. Namely, when the graph is copied, new indices may be assigned to the vertices and edges.- Specified by:
immutableCopy
in interfaceGraph<Integer,Integer>
- Returns:
- an immutable copy of this graph, with the same vertices and edges, without this graph weights
-
immutableCopy
default IntGraph immutableCopy(boolean copyVerticesWeights, boolean copyEdgesWeights)
Description copied from interface:Graph
Create an immutable copy of this graph, with the same vertices and edges, with/without copying weights.An identical copy of this graph will be created, with the same vertices and edges, with/without copying the vertices/edges weights. The returned graph will be immutable, and no vertices/edges/weights can be added or removed from it.
A more compact and efficient representation may be used for the graph, if its known that it will not be changed in the future. It may be more efficient to create an immutable copy of a graph and pass the copy to algorithms instead of using the original graph.
Note that although
g.equals(g.immutableCopy())
is alwaystrue
if bothcopyVerticesWeights
andcopyEdgesWeights
aretrue
, there is no guarantee thatg.indexGraph().equals(g.immutableCopy().indexGraph())
. Namely, when the graph is copied, new indices may be assigned to the vertices and edges.- Specified by:
immutableCopy
in interfaceGraph<Integer,Integer>
- Parameters:
copyVerticesWeights
- iftrue
, the weights of the vertices will be copied to the new graphcopyEdgesWeights
- iftrue
, the weights of the edges will be copied to the new graph- Returns:
- an immutable copy of this graph, with the same vertices and edges, with/without this graph weights
-
immutableView
default IntGraph immutableView()
Description copied from interface:Graph
Get an immutable view of this graph.This method return a view of this graph, namely a Graph that contains the same vertices, edges and weights, that is automatically updated when the original graph is updated. The view is immutable, namely all operations that modify the graph will throw
UnsupportedOperationException
.- Specified by:
immutableView
in interfaceGraph<Integer,Integer>
- Returns:
- an immutable view of this graph
-
reverseView
default IntGraph reverseView()
Description copied from interface:Graph
Get a reversed view of this graph.This method return a view of this graph, namely a Graph that contains the same vertices, edges and weights, that is automatically updated when the original graph is updated and vice versa. The view is reversed, namely each source and target vertices of each edge are swapped.
Note that modifying the returned view will change the original graph.
- Specified by:
reverseView
in interfaceGraph<Integer,Integer>
- Returns:
- a reversed view of this graph
-
undirectedView
default IntGraph undirectedView()
Description copied from interface:Graph
Get an undirected view of this (directed) graph.This method return a view of this graph, namely a Graph that contains the same vertices, edges and weights, that is automatically updated when the original graph is updated and vice versa. The view is undirected, namely each directed edge \((u,v)\) will exist in all the sets
g.outEdges(u)
,g.inEdges(u)
,g.outEdges(v)
andg.inEdges(u)
. The view will contain the same number of edges as this graph.The returned view will return
true
forGraph.isAllowParallelEdges()
even if the original graph does not support parallel edges. This is because the original graph could have both \((u,v)\) in \((v,u)\) without violating the parallel edges constraint, but the view will treat them as parallel edges as the direction is 'forgotten'.If this graph is undirected, this function return the graph itself.
- Specified by:
undirectedView
in interfaceGraph<Integer,Integer>
- Returns:
- an undirected view of this graph
-
subGraphCopy
default IntGraph subGraphCopy(Collection<Integer> vertices, Collection<Integer> edges)
Create a new graph that is a subgraph of this graph.If
edges
isnull
, then the created graph will be an induced subgraph of this graph, namely an induced subgraph of a graph \(G=(V,E)\) is a graph \(G'=(V',E')\) where \(V' \subseteq V\) and \(E' = \{\{u,v\} \mid u,v \in V', \{u,v\} \in E\}\).vertices
must not benull
in this case.If
vertices
isnull
, thenedges
must not benull
, and the sub graph will contain all the vertices which are either a source or a target of an edge inedges
.The created graph will have the same type (directed/undirected) as this graph. The vertices and edges of the created graph will be a subset of the vertices and edges of this graph.
The weights of both vertices and edges will not be copied to the new sub graph. For more flexible sub graph creation, see
Graphs.subGraph(Graph, Collection, Collection, boolean, boolean)
.Prefer to pass a IntCollection instead of Collection<Integer> as collections of vertices and edges.
- Specified by:
subGraphCopy
in interfaceGraph<Integer,Integer>
- Parameters:
vertices
- the vertices of the sub graph, ifnull
thenedges
must not benull
and the vertices of the sub graph will be all the vertices which are either a source or a target of an edge inedges
edges
- the edges of the sub graph, ifnull
thenvertices
must not benull
and the sub graph will be an induced subgraph of this graph- Returns:
- a new graph that is a subgraph of this graph
-
newUndirected
static IntGraph newUndirected()
Create a new undirected empty int graph.The returned graph will be implemented using the default implementation. For more control over the graph details, see
IntGraphFactory
.- Returns:
- a new undirected empty graph
-
newDirected
static IntGraph newDirected()
Create a new directed empty int graph.The returned graph will be implemented using the default implementation. For more control over the graph details, see
IntGraphFactory
.- Returns:
- a new directed empty graph
-
-