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 by its own usingIndexGraph.Builder
. 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 to use theIndexGraph
as regularGraph
, as it will provide the best 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 visa versa is exposed usingIndexIdMap
, which can be accessed usingGraph.indexGraphVerticesMap()
andGraph.indexGraphEdgesMap()
.- Author:
- Barak Ugav
- See Also:
IndexIdMap
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
IndexGraph.Builder
A builder forIndexGraph
objects.
-
Method Summary
All Methods Static 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.IndexGraph
copy()
Create a copy of this graph.it.unimi.dsi.fastutil.ints.IntSet
edges()
Get the set of all edges of the graph.static IndexGraph.Builder
newBuilderDirected()
Create a directed index graph builder.static IndexGraph.Builder
newBuilderUndirected()
Create an undirected index graph builder.void
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.default IndexGraph
unmodifiableView()
Get an unmodifiable view of this graph.it.unimi.dsi.fastutil.ints.IntSet
vertices()
Get the set of all vertices of the graph.-
Methods inherited from interface com.jgalgo.Graph
addEdge, addEdgesWeights, addEdgesWeights, addVertex, addVerticesWeights, addVerticesWeights, clear, clearEdges, edgeEndpoint, edgeSource, edgeTarget, getCapabilities, getEdge, getEdges, getEdgesWeights, getEdgesWeightsKeys, getVerticesWeights, getVerticesWeightsKeys, indexGraph, indexGraphEdgesMap, indexGraphVerticesMap, inEdges, outEdges, removeEdgesWeights, removeVerticesWeights, reverseEdge
-
-
-
-
Method Detail
-
vertices
it.unimi.dsi.fastutil.ints.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
it.unimi.dsi.fastutil.ints.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
-
copy
IndexGraph copy()
Description copied from interface:Graph
Create a copy of this graph.An identical copy of this graph will be created, with the same vertices, edges, weights and capabilities.
-
unmodifiableView
default IndexGraph unmodifiableView()
Description copied from interface:Graph
Get an unmodifiable 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 unmodifiable, namely all operations that modify the graph will throw
UnsupportedOperationException
.- Specified by:
unmodifiableView
in interfaceGraph
- Returns:
- an unmodifiable 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 visa 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
-
newBuilderUndirected
static IndexGraph.Builder newBuilderUndirected()
Create an undirected index graph builder.This is the recommended way to instantiate a new undirected index graph.
- Returns:
- a new builder that can build undirected index graphs
-
newBuilderDirected
static IndexGraph.Builder newBuilderDirected()
Create a directed index graph builder.This is the recommended way to instantiate a new directed index graph.
- Returns:
- a new builder that can build directed index graphs
-
-