Class SparseIntDirectedGraph

  • All Implemented Interfaces:
    org.jgrapht.Graph<java.lang.Integer,​java.lang.Integer>
    Direct Known Subclasses:
    SparseIntDirectedWeightedGraph

    public class SparseIntDirectedGraph
    extends org.jgrapht.graph.AbstractGraph<java.lang.Integer,​java.lang.Integer>
    A sparse directed graph.

    Assuming the graph has $n$ vertices, the vertices are numbered from $0$ to $n-1$. Similarly, edges are numbered from $0$ to $m-1$ where $m$ is the total number of edges.

    It stores two boolean incidence matrix of the graph (rows are vertices and columns are edges) as Compressed Sparse Rows (CSR). Constant time source and target lookups are provided by storing the edge lists in arrays. This is a classic format for write-once read-many use cases. Thus, the graph is unmodifiable.

    The question of whether a sparse or dense representation is more appropriate is highly dependent on various factors such as the graph, the machine running the algorithm and the algorithm itself. Wilkinson defined a matrix as "sparse" if it has enough zeros that it pays to take advantage of them. For more details see

    • Wilkinson, J. H. 1971. Linear algebra; part II: the algebraic eigenvalue problem. In Handbook for Automatic Computation, J. H. Wilkinson and C. Reinsch, Eds. Vol. 2. Springer-Verlag, Berlin, New York.
    Additional information about sparse representations can be found in the wikipedia.
    Author:
    Dimitrios Michail
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected org.jgrapht.opt.graph.sparse.CSRBooleanMatrix inIncidenceMatrix
      Incidence matrix with incoming edges
      protected org.jgrapht.opt.graph.sparse.CSRBooleanMatrix outIncidenceMatrix
      Incidence matrix with outgoing edges
      protected int[] source
      Source vertex of edge
      protected int[] target
      Target vertex of edge
      protected static java.lang.String UNMODIFIABLE  
      • Fields inherited from interface org.jgrapht.Graph

        DEFAULT_EDGE_WEIGHT
    • Constructor Summary

      Constructors 
      Constructor Description
      SparseIntDirectedGraph​(int numVertices, java.util.List<org.jgrapht.alg.util.Pair<java.lang.Integer,​java.lang.Integer>> edges)
      Create a new graph from an edge list.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Integer addEdge​(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)  
      boolean addEdge​(java.lang.Integer sourceVertex, java.lang.Integer targetVertex, java.lang.Integer e)  
      java.lang.Integer addVertex()  
      boolean addVertex​(java.lang.Integer v)  
      protected boolean assertEdgeExist​(java.lang.Integer e)
      Ensures that the specified edge exists in this graph, or else throws exception.
      protected boolean assertVertexExist​(java.lang.Integer v)
      Ensures that the specified vertex exists in this graph, or else throws exception.
      boolean containsEdge​(java.lang.Integer e)  
      boolean containsVertex​(java.lang.Integer v)  
      int degreeOf​(java.lang.Integer vertex)  
      java.util.Set<java.lang.Integer> edgeSet()  
      java.util.Set<java.lang.Integer> edgesOf​(java.lang.Integer vertex)  
      java.util.Set<java.lang.Integer> getAllEdges​(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)
      This operation costs $O(d)$ where $d$ is the out-degree of the source vertex.
      java.lang.Integer getEdge​(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)
      This operation costs $O(d)$ where $d$ is the out-degree of the source vertex.
      java.lang.Integer getEdgeSource​(java.lang.Integer e)  
      java.util.function.Supplier<java.lang.Integer> getEdgeSupplier()  
      java.lang.Integer getEdgeTarget​(java.lang.Integer e)  
      double getEdgeWeight​(java.lang.Integer e)  
      org.jgrapht.GraphType getType()  
      java.util.function.Supplier<java.lang.Integer> getVertexSupplier()  
      java.util.Set<java.lang.Integer> incomingEdgesOf​(java.lang.Integer vertex)  
      int inDegreeOf​(java.lang.Integer vertex)  
      int outDegreeOf​(java.lang.Integer vertex)  
      java.util.Set<java.lang.Integer> outgoingEdgesOf​(java.lang.Integer vertex)  
      boolean removeEdge​(java.lang.Integer e)  
      java.lang.Integer removeEdge​(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)  
      boolean removeVertex​(java.lang.Integer v)  
      void setEdgeWeight​(java.lang.Integer e, double weight)  
      java.util.Set<java.lang.Integer> vertexSet()  
      • Methods inherited from class org.jgrapht.graph.AbstractGraph

        containsEdge, equals, hashCode, removeAllEdges, removeAllEdges, removeAllEdges, removeAllVertices, toString, toStringFromSets
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface org.jgrapht.Graph

        setEdgeWeight
    • Field Detail

      • source

        protected int[] source
        Source vertex of edge
      • target

        protected int[] target
        Target vertex of edge
      • outIncidenceMatrix

        protected org.jgrapht.opt.graph.sparse.CSRBooleanMatrix outIncidenceMatrix
        Incidence matrix with outgoing edges
      • inIncidenceMatrix

        protected org.jgrapht.opt.graph.sparse.CSRBooleanMatrix inIncidenceMatrix
        Incidence matrix with incoming edges
    • Constructor Detail

      • SparseIntDirectedGraph

        public SparseIntDirectedGraph​(int numVertices,
                                      java.util.List<org.jgrapht.alg.util.Pair<java.lang.Integer,​java.lang.Integer>> edges)
        Create a new graph from an edge list.
        Parameters:
        numVertices - the number of vertices
        edges - the edge list
    • Method Detail

      • getVertexSupplier

        public java.util.function.Supplier<java.lang.Integer> getVertexSupplier()
      • getEdgeSupplier

        public java.util.function.Supplier<java.lang.Integer> getEdgeSupplier()
      • addEdge

        public java.lang.Integer addEdge​(java.lang.Integer sourceVertex,
                                         java.lang.Integer targetVertex)
      • addEdge

        public boolean addEdge​(java.lang.Integer sourceVertex,
                               java.lang.Integer targetVertex,
                               java.lang.Integer e)
      • addVertex

        public java.lang.Integer addVertex()
      • addVertex

        public boolean addVertex​(java.lang.Integer v)
      • containsEdge

        public boolean containsEdge​(java.lang.Integer e)
      • containsVertex

        public boolean containsVertex​(java.lang.Integer v)
      • edgeSet

        public java.util.Set<java.lang.Integer> edgeSet()
      • degreeOf

        public int degreeOf​(java.lang.Integer vertex)
      • edgesOf

        public java.util.Set<java.lang.Integer> edgesOf​(java.lang.Integer vertex)
      • inDegreeOf

        public int inDegreeOf​(java.lang.Integer vertex)
      • incomingEdgesOf

        public java.util.Set<java.lang.Integer> incomingEdgesOf​(java.lang.Integer vertex)
      • outDegreeOf

        public int outDegreeOf​(java.lang.Integer vertex)
      • outgoingEdgesOf

        public java.util.Set<java.lang.Integer> outgoingEdgesOf​(java.lang.Integer vertex)
      • removeEdge

        public java.lang.Integer removeEdge​(java.lang.Integer sourceVertex,
                                            java.lang.Integer targetVertex)
      • removeEdge

        public boolean removeEdge​(java.lang.Integer e)
      • removeVertex

        public boolean removeVertex​(java.lang.Integer v)
      • vertexSet

        public java.util.Set<java.lang.Integer> vertexSet()
      • getEdgeSource

        public java.lang.Integer getEdgeSource​(java.lang.Integer e)
      • getEdgeTarget

        public java.lang.Integer getEdgeTarget​(java.lang.Integer e)
      • getType

        public org.jgrapht.GraphType getType()
      • getEdgeWeight

        public double getEdgeWeight​(java.lang.Integer e)
      • setEdgeWeight

        public void setEdgeWeight​(java.lang.Integer e,
                                  double weight)
      • getEdge

        public java.lang.Integer getEdge​(java.lang.Integer sourceVertex,
                                         java.lang.Integer targetVertex)
        This operation costs $O(d)$ where $d$ is the out-degree of the source vertex.
      • getAllEdges

        public java.util.Set<java.lang.Integer> getAllEdges​(java.lang.Integer sourceVertex,
                                                            java.lang.Integer targetVertex)
        This operation costs $O(d)$ where $d$ is the out-degree of the source vertex.
      • assertVertexExist

        protected boolean assertVertexExist​(java.lang.Integer v)
        Ensures that the specified vertex exists in this graph, or else throws exception.
        Overrides:
        assertVertexExist in class org.jgrapht.graph.AbstractGraph<java.lang.Integer,​java.lang.Integer>
        Parameters:
        v - vertex
        Returns:
        true if this assertion holds.
        Throws:
        java.lang.IllegalArgumentException - if specified vertex does not exist in this graph.
      • assertEdgeExist

        protected boolean assertEdgeExist​(java.lang.Integer e)
        Ensures that the specified edge exists in this graph, or else throws exception.
        Parameters:
        e - edge
        Returns:
        true if this assertion holds.
        Throws:
        java.lang.IllegalArgumentException - if specified edge does not exist in this graph.