Package com.jgalgo

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 using int 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 Graph 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) and addEdgeSwapListener(IndexSwapListener).

    An index graph may be obtained as a view from a regular Graph using Graph.indexGraph(), or it can be created by its own using IndexGraph.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 the IndexGraph as regular Graph, 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 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 visa versa is exposed using IndexIdMap, which can be accessed using Graph.indexGraphVerticesMap() and Graph.indexGraphEdgesMap().

    Author:
    Barak Ugav
    See Also:
    IndexIdMap
    • 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).

        Specified by:
        vertices in interface Graph
        Returns:
        a set containing all IDs of the graph vertices
      • 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).

        Specified by:
        edges in interface Graph
        Returns:
        a set containing all IDs of the graph edges
      • 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 interface Graph
        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 interface Graph
        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 interface Graph
        Parameters:
        source - a source vertex
        target - a target vertex
        edge - 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 interface Graph
        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 interface Graph
        Parameters:
        vertex - a vertex in the graph
      • removeOutEdgesOf

        default void removeOutEdgesOf​(int source)
        Remove all edges whose source is source.

        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 interface Graph
        Parameters:
        source - a vertex in the graph
      • removeInEdgesOf

        default void removeInEdgesOf​(int target)
        Remove all edges whose target is target.

        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 interface Graph
        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 by 0,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 by 0,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.

        Specified by:
        copy in interface Graph
        Returns:
        an identical copy of this graph
      • 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 interface Graph
        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 interface Graph
        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