Class SparseIntUndirectedGraph
- java.lang.Object
-
- org.jgrapht.graph.AbstractGraph<java.lang.Integer,java.lang.Integer>
-
- org.jgrapht.opt.graph.sparse.SparseIntUndirectedGraph
-
- All Implemented Interfaces:
org.jgrapht.Graph<java.lang.Integer,java.lang.Integer>
- Direct Known Subclasses:
SparseIntUndirectedWeightedGraph
public class SparseIntUndirectedGraph extends org.jgrapht.graph.AbstractGraph<java.lang.Integer,java.lang.Integer>Sparse undirected 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 the boolean incidence matrix of the graph (rows are vertices and columns are edges) as Compressed Sparse Rows (CSR). In order to also support constant time source and target lookups from an edge identifier we also store the transposed of the incidence matrix again in compressed sparse rows format. 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.
- Author:
- Dimitrios Michail
-
-
Field Summary
Fields Modifier and Type Field Description protected org.jgrapht.opt.graph.sparse.CSRBooleanMatrixincidenceMatrixprotected org.jgrapht.opt.graph.sparse.CSRBooleanMatrixincidenceMatrixTprotected static java.lang.StringUNMODIFIABLE
-
Constructor Summary
Constructors Constructor Description SparseIntUndirectedGraph(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.IntegeraddEdge(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)booleanaddEdge(java.lang.Integer sourceVertex, java.lang.Integer targetVertex, java.lang.Integer e)java.lang.IntegeraddVertex()booleanaddVertex(java.lang.Integer v)protected booleanassertEdgeExist(java.lang.Integer e)Ensures that the specified edge exists in this graph, or else throws exception.protected booleanassertVertexExist(java.lang.Integer v)Ensures that the specified vertex exists in this graph, or else throws exception.booleancontainsEdge(java.lang.Integer e)booleancontainsVertex(java.lang.Integer v)intdegreeOf(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 degree of the source vertex.java.lang.IntegergetEdge(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)This operation costs $O(d)$ where $d$ is the degree of the source vertex.java.lang.IntegergetEdgeSource(java.lang.Integer e)java.util.function.Supplier<java.lang.Integer>getEdgeSupplier()java.lang.IntegergetEdgeTarget(java.lang.Integer e)doublegetEdgeWeight(java.lang.Integer e)org.jgrapht.GraphTypegetType()java.util.function.Supplier<java.lang.Integer>getVertexSupplier()java.util.Set<java.lang.Integer>incomingEdgesOf(java.lang.Integer vertex)intinDegreeOf(java.lang.Integer vertex)intoutDegreeOf(java.lang.Integer vertex)java.util.Set<java.lang.Integer>outgoingEdgesOf(java.lang.Integer vertex)booleanremoveEdge(java.lang.Integer e)java.lang.IntegerremoveEdge(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)booleanremoveVertex(java.lang.Integer v)voidsetEdgeWeight(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
-
-
-
-
Field Detail
-
UNMODIFIABLE
protected static final java.lang.String UNMODIFIABLE
- See Also:
- Constant Field Values
-
incidenceMatrix
protected org.jgrapht.opt.graph.sparse.CSRBooleanMatrix incidenceMatrix
-
incidenceMatrixT
protected org.jgrapht.opt.graph.sparse.CSRBooleanMatrix incidenceMatrixT
-
-
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()
-
getType
public org.jgrapht.GraphType getType()
-
getEdgeWeight
public double getEdgeWeight(java.lang.Integer e)
-
setEdgeWeight
public void setEdgeWeight(java.lang.Integer e, double weight)
-
getEdgeSource
public java.lang.Integer getEdgeSource(java.lang.Integer e)
-
getEdgeTarget
public java.lang.Integer getEdgeTarget(java.lang.Integer e)
-
getEdge
public java.lang.Integer getEdge(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)This operation costs $O(d)$ where $d$ is the 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 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:
assertVertexExistin classorg.jgrapht.graph.AbstractGraph<java.lang.Integer,java.lang.Integer>- Parameters:
v- vertex- Returns:
trueif 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:
trueif this assertion holds.- Throws:
java.lang.IllegalArgumentException- if specified edge does not exist in this graph.
-
-