Interface IndexGraph
-
- All Superinterfaces:
Graph
public interface IndexGraph extends Graph
A graph whose vertices and edges identifiers are indices.The
Graph
interface provide addition, removal and querying of vertices and edges, all usingint
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 aGraph
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
addVertexSwapListener(IndexSwapListener)
andaddEdgeSwapListener(IndexSwapListener)
.An index graph may be obtained as a view from a regular
Graph
usingGraph.indexGraph()
, or it can be created on its own usingIndexGraphFactory
. 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 theIndexGraph
as a regularGraph
, as it will expose an identical functionality while providing better performance.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 usingGraph.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 exposed usingIndexIdMap
, which can be accessed usingGraph.indexGraphVerticesMap()
andGraph.indexGraphEdgesMap()
.- Author:
- Barak Ugav
- See Also:
IndexIdMap
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default void
addEdge(int source, int target, int edge)
Deprecated.void
addEdgeSwapListener(IndexSwapListener listener)
Adds a listener that will be called each time a edge swap is performed.default void
addVertex(int vertex)
Deprecated.void
addVertexSwapListener(IndexSwapListener listener)
Adds a listener that will be called each time a vertex swap is performed.default IndexGraph
copy()
Create a copy of this graph, with the same vertices and edges, without copying weights.default IndexGraph
copy(boolean copyWeights)
Create a copy of this graph, with the same vertices and edges, with/without copying weights.IntSet
edges()
Get the set of all edges of the graph.default IndexGraph
immutableCopy()
Create an immutable copy of this graph, with the same vertices and edges, without copying weights.default IndexGraph
immutableCopy(boolean copyWeights)
Create an immutable copy of this graph, with the same vertices and edges, with/without copying weights.default IndexGraph
immutableView()
Get an immutable view of this graph.default IndexGraph
indexGraph()
Deprecated.this function will always return the same graph, no reason to call itdefault IndexIdMap
indexGraphEdgesMap()
Deprecated.this function will always return the identity mapping, no reason to call itdefault IndexIdMap
indexGraphVerticesMap()
Deprecated.this function will always return the identity mapping, no reason to call itvoid
removeEdge(int edge)
Remove an edge from the graph.default void
removeEdgesOf(int vertex)
Remove all the edges of a vertex.void
removeEdgeSwapListener(IndexSwapListener listener)
Removes an edge swap listener.default void
removeInEdgesOf(int target)
Remove all edges whose target istarget
.default 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
removeVertexSwapListener(IndexSwapListener listener)
Removes a vertex swap listener.default IndexGraph
reverseView()
Get a reversed view of this graph.IntSet
vertices()
Get the set of all vertices of the graph.-
Methods inherited from interface com.jgalgo.graph.Graph
addEdge, addEdgesWeights, addEdgesWeights, addVertex, addVerticesWeights, addVerticesWeights, clear, clearEdges, edgeEndpoint, edgeSource, edgeTarget, getCapabilities, getEdge, getEdges, getEdgesWeights, getEdgesWeightsKeys, getVerticesWeights, getVerticesWeightsKeys, inEdges, outEdges, removeEdgesWeights, removeVerticesWeights, reverseEdge
-
-
-
-
Method Detail
-
vertices
IntSet vertices()
Get the set of all vertices of the graph.Each vertex in the graph is identified by a unique non negative integer ID 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 negative integer ID, 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)
.
-
addVertex
@Deprecated default void addVertex(int vertex)
Deprecated.Unsupported operation.Index graphs vertices IDs are always
(0,1,2, ...,verticesNum-1)
and do not support user chosen IDs.- Specified by:
addVertex
in interfaceGraph
- Parameters:
vertex
- a user chosen identifier for the new vertex- Throws:
UnsupportedOperationException
- always
-
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
addVertexSwapListener(IndexSwapListener)
.- Specified by:
removeVertex
in interfaceGraph
- Parameters:
vertex
- the vertex identifier to remove
-
addEdge
@Deprecated default void addEdge(int source, int target, int edge)
Deprecated.Unsupported operation.Index graphs edges IDs are always
(0,1,2, ...,edgesNum-1)
and do not support user chosen IDs.- Specified by:
addEdge
in interfaceGraph
- Parameters:
source
- a source vertextarget
- a target vertexedge
- a user chosen identifier for the new edge- Throws:
UnsupportedOperationException
- always
-
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
addEdgeSwapListener(IndexSwapListener)
.- Specified by:
removeEdge
in interfaceGraph
- Parameters:
edge
- the edge identifier
-
removeEdgesOf
default 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
addEdgeSwapListener(IndexSwapListener)
.- Specified by:
removeEdgesOf
in interfaceGraph
- Parameters:
vertex
- a vertex in the graph
-
removeOutEdgesOf
default 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
addEdgeSwapListener(IndexSwapListener)
.- Specified by:
removeOutEdgesOf
in interfaceGraph
- Parameters:
source
- a vertex in the graph
-
removeInEdgesOf
default 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
addEdgeSwapListener(IndexSwapListener)
.- Specified by:
removeInEdgesOf
in interfaceGraph
- Parameters:
target
- a vertex in the graph
-
addVertexSwapListener
void addVertexSwapListener(IndexSwapListener listener)
Adds a listener that will be called each time a vertex 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.- Parameters:
listener
- a swap listener that will be called each time a vertex swap is performed
-
removeVertexSwapListener
void removeVertexSwapListener(IndexSwapListener listener)
Removes a vertex swap listener.After a listener was added using
addVertexSwapListener(IndexSwapListener)
, this method removes may remove it.- Parameters:
listener
- a swap listener that should be removed
-
addEdgeSwapListener
void addEdgeSwapListener(IndexSwapListener listener)
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.- Parameters:
listener
- a swap listener that will be called each time a edge swap is performed
-
removeEdgeSwapListener
void removeEdgeSwapListener(IndexSwapListener listener)
Removes an edge swap listener.After a listener was added using
addEdgeSwapListener(IndexSwapListener)
, this method removes may remove it.- Parameters:
listener
- a swap listener that should be removed
-
indexGraph
@Deprecated default IndexGraph 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
- Returns:
- this graph
-
indexGraphVerticesMap
@Deprecated default IndexIdMap 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
- Returns:
- a mapping that map vertices IDs to vertices indices
-
indexGraphEdgesMap
@Deprecated default IndexIdMap 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
- Returns:
- a mapping that map edges IDs to edges indices
-
copy
default IndexGraph 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), without copying the vertices/edges weights. The returned Graph will always be modifiable, with no side affects on the original graph.
-
copy
default IndexGraph copy(boolean copyWeights)
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), with/without copying the vertices/edges weights. The returned Graph will always be modifiable, with no side affects on the original graph.
Note that although
g.equals(g.copy())
is alwaystrue
ifcopyWeights
istrue
, 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.
-
immutableCopy
default IndexGraph 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
- Returns:
- an immutable copy of this graph, with the same vertices and edges, without this graph weights
-
immutableCopy
default IndexGraph immutableCopy(boolean copyWeights)
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
ifcopyWeights
istrue
, 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
- Parameters:
copyWeights
- iftrue
, the weights of the vertices and 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 IndexGraph 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
- Returns:
- an immutable view of this graph
-
reverseView
default IndexGraph 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
- Returns:
- a reversed view of this graph
-
-