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 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 Graph, 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 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 exposed using IndexIdMap, which can be accessed using Graph.indexGraphVerticesMap() and Graph.indexGraphEdgesMap().

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

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

        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
      • indexGraph

        @Deprecated
        default IndexGraph indexGraph()
        Deprecated.
        this function will always return the same graph, no reason to call it
        The index graph of an IndexGraph is itself.
        Specified by:
        indexGraph in interface Graph
        Returns:
        this graph
      • indexGraphVerticesMap

        @Deprecated
        default IndexIdMap indexGraphVerticesMap()
        Deprecated.
        this function will always return the identity mapping, no reason to call it
        The IDs and indices of an IndexGraph are the same.
        Specified by:
        indexGraphVerticesMap in interface Graph
        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 it
        The IDs and indices of an IndexGraph are the same.
        Specified by:
        indexGraphEdgesMap in interface Graph
        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.

        Specified by:
        copy in interface Graph
        Returns:
        an identical copy of this graph, with the same vertices and edges, without this graph weights
      • 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 always true if copyWeights is true, there is no guarantee that g.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 interface Graph
        Parameters:
        copyWeights - if true, the weights of the vertices and 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 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 always true, there is no guarantee that g.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 interface Graph
        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 always true if copyWeights is true, there is no guarantee that g.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 interface Graph
        Parameters:
        copyWeights - if true, 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 interface Graph
        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 interface Graph
        Returns:
        a reversed view of this graph