Interface IndexGraph
The Graph
interface provide addition, removal and querying of vertices and edges, all using some hashable
identifiers. These identifiers are fixed, and once a vertex or edge is assigned an ID, it will not change during the
graph lifetime. On the other hand, an Index graph is a IntGraph
object in which the vertices and edges
identifiers of the graph are always (0,1,2, ...,verticesNum-1)
and (0,1,2, ...,edgesNum-1)
.
The index graph invariants allow for a great performance boost, as a simple array or bitmap can be used to associate
a value/weight/flag with each vertex/edge. But it does come with a cost: to maintain the invariants, implementations
may need to rename existing vertices or edges during the graph lifetime. These renames can be subscribed-to using
addVertexRemoveListener(IndexRemoveListener)
and addEdgeRemoveListener(IndexRemoveListener)
.
An index graph may be obtained as a view from a regular Graph
using Graph.indexGraph()
, or it can be
created on its own using IndexGraphFactory
. In cases where no removal of vertices or edges is required, and
there is no need to use pre-defined IDs, there is no drawback of using the IndexGraph
as a regular
IntGraph
, as it will expose an identical functionality while providing better performance.
If an IndexGraph
is obtained from a regular Graph
(or IntGraph
) using
Graph.indexGraph()
, the IndexGraph
should not be modified directly. Vertices/edges/weights should be
added/removed only via the original fixed identifiers graph.
All graph algorithms implementations should operation on Index graphs only, for best performance. If a regular
Graph
is provided to an algorithm, the Index graph should be retrieved using Graph.indexGraph()
, the
algorithm expensive logic should operate on the returned Index graph and finally the result should be transformed
back to the regular graph IDs. The mapping from a regular graph IDs to indices and vice versa is provided by
IndexIdMap
, which can be accessed using Graph.indexGraphVerticesMap()
and
Graph.indexGraphEdgesMap()
.
To create a new empty index graph, use newUndirected()
or newDirected()
. The returned graph will
use the default implementation. For more control over the graph details, see IndexGraphFactory
. To construct
an immutable index graph, use IndexGraphBuilder
.
- Author:
- Barak Ugav
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionint
addEdge
(int source, int target) Add a new edge to the graph, using the edge builder.default void
addEdge
(int source, int target, int edge) Deprecated.void
addEdgeRemoveListener
(IndexRemoveListener listener) Adds a listener that will be called each time a edge swap is performed.void
Add multiple edges to the graph.addEdgesReassignIds
(IEdgeSet edges) Add multiple edges to the graph and re-assign ids for them.addEdgesWeights
(String key, Class<? super T> type) Add a new weights container associated with the edges of this graph.addEdgesWeights
(String key, Class<? super T> type, T defVal) Add a new weights container associated with the edges of this graph with default value.default void
addVertex
(int vertex) Deprecated.useaddVertexInt()
insteadint
Add a new vertex to the graph, using the vertex builder.void
addVertexRemoveListener
(IndexRemoveListener listener) Adds a listener that will be called each time a vertex remove or swap is performed.void
addVertices
(Collection<? extends Integer> vertices) Add multiple vertices to the graph.addVerticesWeights
(String key, Class<? super T> type) Add a new weights container associated with the vertices of this graph.addVerticesWeights
(String key, Class<? super T> type, T defVal) Add a new weights container associated with the vertices of this graph with default value.void
clear()
Clear the graph completely by removing all vertices and edges.void
Remove all the edges from the graph.default IndexGraph
copy()
Create a copy of this graph, with the same vertices and edges, without copying weights.default IndexGraph
copy
(boolean copyVerticesWeights, boolean copyEdgesWeights) Create a copy of this graph, with the same vertices and edges, with/without copying weights.default IdBuilderInt
Get the edge builder of this graph.edges()
Get the set of all edges of the graph.default IndexGraph
Create an immutable copy of this graph, with the same vertices and edges, without copying weights.default IndexGraph
immutableCopy
(boolean copyVerticesWeights, boolean copyEdgesWeights) Create an immutable copy of this graph, with the same vertices and edges, with/without copying weights.default IndexGraph
Get an immutable view of this graph.default IndexGraph
Deprecated.this function will always return the same graph, no reason to call itDeprecated.this function will always return the identity mapping, no reason to call itDeprecated.this function will always return the identity mapping, no reason to call itstatic IndexGraph
Create a new directed empty index graph.static IndexGraph
Create a new undirected empty index graph.void
removeEdge
(int edge) Remove an edge from the graph.void
removeEdgeRemoveListener
(IndexRemoveListener listener) Removes an edge remove listener.void
removeEdgesOf
(int vertex) Remove all the edges of a vertex.void
removeEdgesWeights
(String key) Remove a weight type associated with the edges of the graph.void
removeInEdgesOf
(int target) Remove all edges whose target istarget
.void
removeOutEdgesOf
(int source) Remove all edges whose source issource
.void
removeVertex
(int vertex) Remove a vertex and all its edges from the graph.void
Removes a vertex remove listener.void
Remove a weight type associated with the vertices of the graph.default void
renameEdge
(int edge, int newId) Deprecated.unsupported operationdefault void
renameVertex
(int vertex, int newId) Deprecated.unsupported operationdefault IndexGraph
Get a reversed view of this graph.default IndexGraph
Get an undirected view of this (directed) graph.default IdBuilderInt
Get the vertex builder of this graph.vertices()
Get the set of all vertices of the graph.Methods inherited from interface com.jgalgo.graph.Graph
edgesWeightsKeys, ensureEdgeCapacity, ensureVertexCapacity, isAllowParallelEdges, isAllowSelfEdges, isDirected, removeEdges, removeVertices, verticesWeightsKeys
Methods inherited from interface com.jgalgo.graph.IntGraph
addEdge, addEdge, addVertex, addVertex, containsEdge, containsEdge, edgeEndpoint, edgeEndpoint, edgeSource, edgeSource, edgesWeights, edgeTarget, edgeTarget, getEdge, getEdge, getEdges, getEdges, inEdges, inEdges, moveEdge, moveEdge, outEdges, outEdges, removeEdge, removeEdgesOf, removeInEdgesOf, removeOutEdgesOf, removeVertex, renameEdge, renameVertex, reverseEdge, reverseEdge, subGraphCopy, verticesWeights
-
Method Details
-
vertices
IntSet vertices()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()
.In an Index graph, the set of vertices are always
(0,1,2, ...,verticesNum-1)
. -
edges
IntSet edges()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()
.In an Index graph, the set of edges are always
(0,1,2, ...,edgesNum-1)
. -
addVertexInt
int addVertexInt()Add a new vertex to the graph, using the vertex builder.Unlike
IntGraph.addVertex(int)
in which the vertex is provided by the user, this method uses the vertex builder obtained byIntGraph.vertexBuilder()
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;
The vertex created by this method will be assigned the next available index,
verticesNum
. For example, if the graph currently contains the vertices0,1,2
, the next vertex added will be3
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
addVertexInt
in interfaceIntGraph
- Returns:
- the new vertex
-
addVertex
Deprecated.useaddVertexInt()
insteadAdd a new vertex to the graph.Vertices must be non negative integers.
If the graph have a vertex builder, namely if
IntGraph.vertexBuilder()
does not returnnull
, the methodIntGraph.addVertexInt()
can be used, which uses the vertex builder to create the new vertex object instead of requiring the user to provide it.Index graphs vertices IDs are always
(0,1,2, ...,verticesNum-1)
therefore the only vertex ID that can be added isverticesNum
. For any other vertex passed to this method, an exception will be thrown. IfverticesNum
is passed, this method is equivalent toaddVertexInt()
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
addVertex
in interfaceIntGraph
- Parameters:
vertex
- new vertex- Throws:
IllegalArgumentException
- ifvertex
is notverticesNum
-
addVertices
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.Index graphs vertices IDs are always
(0,1,2, ...,verticesNum-1)
therefore the only vertices that can be added are(verticesNum,verticesNum+1,verticesNum+2, ...)
. For any other vertices passed to this method, an exception will be thrown.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
addVertices
in interfaceGraph<Integer,
Integer> - Specified by:
addVertices
in interfaceIntGraph
- Parameters:
vertices
- new vertices
-
removeVertex
void removeVertex(int vertex) Remove a vertex and all its edges from the graph.After removing a vertex, the graph implementation may swap and rename vertices to maintain its invariants. Theses renames can be subscribed using
addVertexRemoveListener(IndexRemoveListener)
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
removeVertex
in interfaceIntGraph
- Parameters:
vertex
- the vertex identifier to remove
-
renameVertex
Deprecated.unsupported operationUnsupported operation.- Specified by:
renameVertex
in interfaceIntGraph
- Parameters:
vertex
- an existing vertex in the graphnewId
- the new vertex identifier- Throws:
UnsupportedOperationException
- always
-
addEdge
int addEdge(int source, int target) Add a new edge to the graph, using the edge builder.Unlike
IntGraph.addEdge(int, int, int)
in which the edge (identifier) is provided by the user, this method uses the edge builder obtained byIntGraph.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:
int edge = edgeBuilder().build(edges()); addEdge(source, target, edge); return edge;
The edge created by this method will be assigned the next available index,
edgesNum
. For example, if the graph currently contains the edges0,1,2
, the next edge added will be3
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead. -
addEdge
Deprecated.useaddEdge(int, int)
insteadAdd 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
IntGraph.edgeBuilder()
does not returnnull
, the methodIntGraph.addEdge(int, int)
can be used, which uses the edge builder to create the new edge object instead of requiring the user to provide it.Index graphs edges IDs are always
(0,1,2, ...,edgesNum-1)
therefore the only edge ID that can be added isedgesNum
. For any other edge passed to this method, an exception will be thrown. IfedgesNum
is passed, this method is equivalent toaddEdge(int, int)
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
addEdge
in interfaceIntGraph
- Parameters:
source
- a source vertextarget
- a target vertexedge
- a new edge identifier- Throws:
IllegalArgumentException
- ifedge
is notedgesNum
-
addEdges
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)
.Index graphs edges IDs are always
(0,1,2, ...,edgesNum-1)
therefore the only edges that can be added are(edgesNum,edgesNum+1,edgesNum+2, ...)
. For any other edges passed to this method, an exception will be thrown. If there is no need to keep the identifiers of the edges, consider usingaddEdgesReassignIds(IEdgeSet)
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
addEdges
in interfaceGraph<Integer,
Integer> - Specified by:
addEdges
in interfaceIntGraph
- 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()
).
-
addEdgesReassignIds
Add multiple edges to the graph and re-assign ids for them.The
IEdgeSet
passed to this method contains the endpoints (sources and targets) of the edges, seeEdgeSet.iterator()
,EdgeIter.source()
,EdgeIter.target()
. The identifiers of the edges, which are also accessible viaIEdgeSet
are ignored, and new identifiers (indices) are assigned to the added edges. AnIEdgeSet
can be obtained from one of the methods of anotherIntGraph
, or usingIEdgeSet.of(IntSet, IntGraph)
.The identifiers assigned to the newly added edges are
(edgesNum,edgesNum+1,edgesNum+2, ...)
matching the iteration order of the provided set. This method different thanaddEdges(EdgeSet)
in a similar way thataddEdge(int, int)
is different thanaddEdge(int, int, int)
.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.
In the following snippet, a maximum cardinality matching is computed on a graph, and a new graph containing only the matching edges is created. It would be wrong to use
addEdges(EdgeSet)
in this example, as there is no guarantee that the added edges ids are(0, 1, 2, ...)
, which is a requirement to maintain the index graph invariants.IndexGraph g = ...; IntSet matching = (IntSet) MatchingAlgo.newInstance().computeMaximumMatching(g, null).edges(); IndexGraph matchingGraph = IndexGraph.newUndirected(); matchingGraph.addVertices(g.vertices()); matchingGraph.addEdgesReassignIds(IEdgeSet.of(matching, g));
If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Parameters:
edges
- the set of edges to add. Only the endpoints of the edges is considered, while the edges identifiers are ignored.- Returns:
- the set of newly edge identifiers added to the graph,
(edgesNum,edgesNum+1,edgesNum+2, ...)
. The edges are assigned the indices in the order they are iterated in the given set
-
removeEdge
void removeEdge(int edge) Remove an edge from the graph.After removing an edge, the graph implementation may swap and rename edges to maintain its invariants. Theses renames can be subscribed using
addEdgeRemoveListener(IndexRemoveListener)
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
removeEdge
in interfaceIntGraph
- Parameters:
edge
- the edge identifier
-
removeEdgesOf
void removeEdgesOf(int vertex) Remove all the edges of a vertex.After removing an edge, the graph implementation may swap and rename edges to maintain its invariants. Theses renames can be subscribed using
addEdgeRemoveListener(IndexRemoveListener)
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
removeEdgesOf
in interfaceIntGraph
- Parameters:
vertex
- a vertex in the graph
-
removeOutEdgesOf
void removeOutEdgesOf(int source) Remove all edges whose source issource
.After removing an edge, the graph implementation may swap and rename edges to maintain its invariants. Theses renames can be subscribed using
addEdgeRemoveListener(IndexRemoveListener)
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
removeOutEdgesOf
in interfaceIntGraph
- Parameters:
source
- a vertex in the graph
-
removeInEdgesOf
void removeInEdgesOf(int target) Remove all edges whose target istarget
.After removing an edge, the graph implementation may swap and rename edges to maintain its invariants. Theses renames can be subscribed using
addEdgeRemoveListener(IndexRemoveListener)
.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
removeInEdgesOf
in interfaceIntGraph
- Parameters:
target
- a vertex in the graph
-
renameEdge
Deprecated.unsupported operationUnsupported operation.- Specified by:
renameEdge
in interfaceIntGraph
- Parameters:
edge
- an existing edge in the graphnewId
- the new edge identifier- Throws:
UnsupportedOperationException
- always
-
clear
void clear()Clear the graph completely by removing all vertices and edges.This function might be used to reuse an already allocated graph object.
Note that this function also clears any weights associated with the vertices or edges.
If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead. -
clearEdges
void clearEdges()Remove all the edges from the graph.Note that this function also clears any weights associated with the edges.
If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
clearEdges
in interfaceGraph<Integer,
Integer>
-
vertexBuilder
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)
.The vertex builder returned by this method always assign the next available index,
verticesNum
, given the current set of vertices(0,1,2, ...,verticesNum-1)
. For example, if the graph currently contains the vertices0,1,2
, the next vertex added will be3
. The builder simply returns the current vertices set size, without validating that the set is actually(0,1,2, ...,verticesNum-1)
.- Specified by:
vertexBuilder
in interfaceGraph<Integer,
Integer> - Specified by:
vertexBuilder
in interfaceIntGraph
- Returns:
- the vertex builder of this graph, or
null
if the graph does not have a vertex builder
-
edgeBuilder
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)
.The edge builder returned by this method always assign the next available index,
edgesNum
, given the current set of edges(0,1,2, ...,edgesNum-1)
. For example, if the graph currently contains the edges0,1,2
, the next edge added will be3
. The builder simply returns the current edges set size, without validating that the set is actually(0,1,2, ...,edgesNum-1)
.- Specified by:
edgeBuilder
in interfaceGraph<Integer,
Integer> - Specified by:
edgeBuilder
in interfaceIntGraph
- Returns:
- the edge builder of this graph, or
null
if the graph does not have an edge builder
-
addVerticesWeights
default <T,WeightsT extends Weights<Integer, WeightsT addVerticesWeightsT>> (String key, Class<? super T> type) Add a new weights container associated with the vertices of this graph.The created weights will be bounded to this graph, and will be updated when the graph is updated (when vertices are added or removed). To create an external weights container, for example in cases the graph is a user input and we are not allowed to modify it, use
Weights.createExternalVerticesWeights(Graph, Class)
.Graph<String, Int> g = ...; g.newVertex("Alice"); g.newVertex("Bob"); Weights<String> names = g.addVerticesWeights("surname", String.class); names.set("Alice", "Miller"); names.set("Bob", "Jones"); WeightsInt ages = g.addVerticesWeights("age", int.class); ages.set("Alice", 42); ages.set("Bob", 35);
See
Weights
for a complete documentation of the weights containers.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
addVerticesWeights
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,null
is not allowedtype
- the type of the weights, used for primitive types weights- Returns:
- a new weights container
-
addVerticesWeights
<T,WeightsT extends Weights<Integer, WeightsT addVerticesWeightsT>> (String key, Class<? super T> type, T defVal) Add a new weights container associated with the vertices of this graph with default value.The created weights will be bounded to this graph, and will be updated when the graph is updated. To create an external weights container, for example in cases the graph is a user input we are not allowed to modify it, use
Weights.createExternalVerticesWeights(Graph, Class, Object)
.Graph<String, Int> g = ...; g.newVertex("Alice"); g.newVertex("Bob"); g.newVertex("Charlie"); Weights<String> names = g.addVerticesWeights("name", String.class, "Unknown"); names.set("Alice", "Miller"); names.set("Bob", "Jones"); assert "Miller".equals(names.get("Alice")) assert "Jones".equals(names.get("Bob")) assert "Unknown".equals(names.get("Charlie"))
See
Weights
for a complete documentation of the weights containers.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
addVerticesWeights
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,null
is not allowedtype
- the type of the weights, used for primitive types weightsdefVal
- default value use for the weights container- Returns:
- a new weights container
-
removeVerticesWeights
Remove a weight type associated with the vertices of the graph.See
Weights
for a complete documentation of the weights containers.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
removeVerticesWeights
in interfaceGraph<Integer,
Integer> - Parameters:
key
- the key of the weights
-
addEdgesWeights
default <T,WeightsT extends Weights<Integer, WeightsT addEdgesWeightsT>> (String key, Class<? super T> type) Add a new weights container associated with the edges of this graph.The created weights will be bounded to this graph, and will be updated when the graph is updated. To create an external weights container, for example in cases the graph is a user input you are not allowed to modify it, use
Weights.createExternalEdgesWeights(Graph, Class)
.Graph<String, Integer> g = ...; g.addVertex("Berlin"); g.addVertex("Leipzig"); g.addVertex("Dresden"); g.addEdge("Berlin", "Leipzig", 9); g.addEdge("Berlin", "Dresden", 13); Weights<String> roadTypes = g.addEdgesWeights("roadType", String.class); roadTypes.set(9, "Asphalt"); roadTypes.set(13, "Gravel"); WeightsDouble roadLengths = g.addEdgesWeights("roadLength", double.class); roadLengths.set(9, 42); roadLengths.set(13, 35);
See
Weights
for a complete documentation of the weights containers.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
addEdgesWeights
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,null
is not allowedtype
- the type of the weights, used for primitive types weights- Returns:
- a new weights container
-
addEdgesWeights
<T,WeightsT extends Weights<Integer, WeightsT addEdgesWeightsT>> (String key, Class<? super T> type, T defVal) Add a new weights container associated with the edges of this graph with default value.The created weights will be bounded to this graph, and will be updated when the graph is updated. To create an external weights container, for example in cases the graph is a user input we are not allowed to modify it, use
Weights.createExternalEdgesWeights(Graph, Class, Object)
.Graph<String, Integer> g = ...; g.addVertex("Berlin"); g.addVertex("Leipzig"); g.addVertex("Dresden"); g.addEdge("Berlin", "Leipzig", 9); g.addEdge("Berlin", "Dresden", 13); g.addEdge("Dresden", "Leipzig", 14); Weights<String> roadTypes = g.addEdgesWeights("roadType", String.class, "Unknown"); roadTypes.set(9, "Asphalt"); roadTypes.set(13, "Gravel"); assert "Asphalt".equals(names.get(9)) assert "Gravel".equals(names.get(13)) assert "Unknown".equals(names.get(14))
See
Weights
for a complete documentation of the weights containers.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
addEdgesWeights
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,null
is not allowedtype
- the type of the weights, used for primitive types weightsdefVal
- default value use for the weights container- Returns:
- a new weights container
-
removeEdgesWeights
Remove a weight type associated with the edges of the graph.See
Weights
for a complete documentation of the weights containers.If this index graph object was obtained from a regular
Graph
usingGraph.indexGraph()
, this method should not be called. Use the original graph instead.- Specified by:
removeEdgesWeights
in interfaceGraph<Integer,
Integer> - Parameters:
key
- the key of the weights
-
addVertexRemoveListener
Adds a listener that will be called each time a vertex remove or swap is performed.An
IndexGraph
may rename vertices during its lifetime to maintain the invariant that all vertices are identified by0,1,2,...,verticesNum-1
. This method can be used to track these changes, by registering a listener that will be invoked each time such a rename is performed.If a vertex is removed with the last index (
verticesNum-1
), the vertex can simply be removed. Otherwise, the vertex will be swapped with the last vertex and then removed. In both cases, the listener will be called.- Parameters:
listener
- a remove listener that will be called each time a vertex remove or swap is performed
-
removeVertexRemoveListener
Removes a vertex remove listener.After a listener was added using
addVertexRemoveListener(IndexRemoveListener)
, this method can remove it.- Parameters:
listener
- a remove listener that should be removed
-
addEdgeRemoveListener
Adds a listener that will be called each time a edge swap is performed.An
IndexGraph
may rename edges during its lifetime to maintain the invariant that all edges are identified by0,1,2,...,edgesNum-1
. This method can be used to track these changes, by registering a listener that will be invoked each time such a rename is performed.If an edge is removed with the last index (
edgesNum-1
), the edge can simply be removed. Otherwise, the edge will be swapped with the last edge and then removed. In both cases, the listener will be called.- Parameters:
listener
- a remove listener that will be called each time a edge remove or swap is performed
-
removeEdgeRemoveListener
Removes an edge remove listener.After a listener was added using
addEdgeRemoveListener(IndexRemoveListener)
, this method can remove it.- Parameters:
listener
- a remove listener that should be removed
-
indexGraph
Deprecated.this function will always return the same graph, no reason to call itThe index graph of anIndexGraph
is itself.- Specified by:
indexGraph
in interfaceGraph<Integer,
Integer> - Returns:
- this graph
-
indexGraphVerticesMap
Deprecated.this function will always return the identity mapping, no reason to call itThe IDs and indices of anIndexGraph
are the same.- Specified by:
indexGraphVerticesMap
in interfaceGraph<Integer,
Integer> - Specified by:
indexGraphVerticesMap
in interfaceIntGraph
- Returns:
- a mapping that map vertices IDs to vertices indices
-
indexGraphEdgesMap
Deprecated.this function will always return the identity mapping, no reason to call itThe IDs and indices of anIndexGraph
are the same.- Specified by:
indexGraphEdgesMap
in interfaceGraph<Integer,
Integer> - Specified by:
indexGraphEdgesMap
in interfaceIntGraph
- Returns:
- a mapping that map edges IDs to edges indices
-
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
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> - Specified by:
copy
in interfaceIntGraph
- 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
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> - Specified by:
immutableCopy
in interfaceIntGraph
- Returns:
- an immutable copy of this graph, with the same vertices and edges, without this graph weights
-
immutableCopy
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> - Specified by:
immutableCopy
in interfaceIntGraph
- 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
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> - Specified by:
immutableView
in interfaceIntGraph
- Returns:
- an immutable view of this graph
-
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> - Specified by:
reverseView
in interfaceIntGraph
- Returns:
- a reversed view of this graph
-
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> - Specified by:
undirectedView
in interfaceIntGraph
- Returns:
- an undirected view of this graph
-
newUndirected
Create a new undirected empty index graph.The returned graph will be implemented using the default implementation. For more control over the graph details, see
IndexGraphFactory
.- Returns:
- a new undirected empty index graph
-
newDirected
Create a new directed empty index graph.The returned graph will be implemented using the default implementation. For more control over the graph details, see
IndexGraphFactory
.- Returns:
- a new directed empty index graph
-
addEdge(int, int)
instead