- All Implemented Interfaces:
java.io.Serializable
,Graph<java.lang.Integer,java.lang.Integer>
public class SparseIntDirectedWeightedGraph extends SparseIntDirectedGraph implements java.io.Serializable
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 edge weights are maintained in an array indexed by the edge
identifier. If the user does not require support for incoming edges, then the second incidence
matrix can be either completely avoided or constructed lazily. See
SparseIntDirectedWeightedGraph(int, List, IncomingEdgesSupport)
for more details.
The graph is weighted. While unmodifiable with respect to the structure of the graph, the edge weights can be changed even after the graph is constructed.
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
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description protected double[]
weights
The edge weights -
Constructor Summary
Constructors Constructor Description SparseIntDirectedWeightedGraph(int numVertices, int numEdges, java.util.function.Supplier<java.util.stream.Stream<Triple<java.lang.Integer,java.lang.Integer,java.lang.Double>>> edges, IncomingEdgesSupport incomingEdgeSupport)
Create a new graph from an edge stream.SparseIntDirectedWeightedGraph(int numVertices, java.util.List<Triple<java.lang.Integer,java.lang.Integer,java.lang.Double>> edges)
Create a new graph from an edge list.SparseIntDirectedWeightedGraph(int numVertices, java.util.List<Triple<java.lang.Integer,java.lang.Integer,java.lang.Double>> edges, IncomingEdgesSupport incomingEdgeSupport)
Create a new graph from an edge list. -
Method Summary
Modifier and Type Method Description double
getEdgeWeight(java.lang.Integer e)
Returns the weight assigned to a given edge.GraphType
getType()
Get the graph type.void
setEdgeWeight(java.lang.Integer e, double weight)
Assigns a weight to an edge.Methods inherited from class org.jgrapht.opt.graph.sparse.specifics.AbstractSparseSpecificsGraph
addEdge, addEdge, addVertex, addVertex, containsEdge, containsVertex, degreeOf, edgeSet, edgesOf, getAllEdges, getEdge, getEdgeSource, getEdgeSupplier, getEdgeTarget, getVertexSupplier, incomingEdgesOf, inDegreeOf, outDegreeOf, outgoingEdgesOf, removeEdge, removeEdge, removeVertex, vertexSet
Methods inherited from class org.jgrapht.graph.AbstractGraph
assertVertexExist, containsEdge, equals, hashCode, removeAllEdges, removeAllEdges, removeAllEdges, removeAllVertices, toString, toStringFromSets
-
Field Details
-
weights
protected double[] weightsThe edge weights
-
-
Constructor Details
-
SparseIntDirectedWeightedGraph
public SparseIntDirectedWeightedGraph(int numVertices, java.util.List<Triple<java.lang.Integer,java.lang.Integer,java.lang.Double>> edges)Create a new graph from an edge list.- Parameters:
numVertices
- the number of verticesedges
- the edge list with additional weights
-
SparseIntDirectedWeightedGraph
public SparseIntDirectedWeightedGraph(int numVertices, java.util.List<Triple<java.lang.Integer,java.lang.Integer,java.lang.Double>> edges, IncomingEdgesSupport incomingEdgeSupport)Create a new graph from an edge list.- Parameters:
numVertices
- the number of verticesedges
- the edge list with additional weightsincomingEdgeSupport
- the kind of incoming edges support needed
-
SparseIntDirectedWeightedGraph
public SparseIntDirectedWeightedGraph(int numVertices, int numEdges, java.util.function.Supplier<java.util.stream.Stream<Triple<java.lang.Integer,java.lang.Integer,java.lang.Double>>> edges, IncomingEdgesSupport incomingEdgeSupport)Create a new graph from an edge stream.- Parameters:
numVertices
- the number of verticesnumEdges
- the number of edgesedges
- a supplier of an edge stream with additional weightsincomingEdgeSupport
- the kind of incoming edges support needed
-
-
Method Details
-
getType
Description copied from interface:Graph
Get the graph type. The graph type can be used to query for additional metadata such as whether the graph supports directed or undirected edges, self-loops, multiple (parallel) edges, weights, etc. -
getEdgeWeight
public double getEdgeWeight(java.lang.Integer e)Description copied from interface:Graph
Returns the weight assigned to a given edge. Unweighted graphs return 1.0 (as defined byGraph.DEFAULT_EDGE_WEIGHT
), allowing weighted-graph algorithms to apply to them when meaningful.- Specified by:
getEdgeWeight
in interfaceGraph<java.lang.Integer,java.lang.Integer>
- Overrides:
getEdgeWeight
in classorg.jgrapht.opt.graph.sparse.specifics.AbstractSparseSpecificsGraph<org.jgrapht.opt.graph.sparse.specifics.SparseGraphSpecifics>
- Parameters:
e
- edge of interest- Returns:
- edge weight
-
setEdgeWeight
public void setEdgeWeight(java.lang.Integer e, double weight)Description copied from interface:Graph
Assigns a weight to an edge.- Specified by:
setEdgeWeight
in interfaceGraph<java.lang.Integer,java.lang.Integer>
- Overrides:
setEdgeWeight
in classorg.jgrapht.opt.graph.sparse.specifics.AbstractSparseSpecificsGraph<org.jgrapht.opt.graph.sparse.specifics.SparseGraphSpecifics>
- Parameters:
e
- edge on which to set weightweight
- new weight for edge
-