- All Implemented Interfaces:
java.io.Serializable,Graph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>
public class SuccinctDirectedGraph extends AbstractSuccinctDirectedGraph<it.unimi.dsi.fastutil.ints.IntIntPair> implements java.io.Serializable
IntIntPair edges represented using quasi-succinct data
structures.
The graph representation of this implementation uses the Elias–Fano representation of monotone sequences to represent the positions of ones in the
(linearized) adjacency matrix of the graph. Edges are represented by instances of
IntIntPair. Instances are serializable and thread safe.
If the vertex set is compact (i.e., vertices are numbered from 0 consecutively), space usage will
be close to twice the information-theoretical lower bound (typically, a few times smaller than a
SparseIntDirectedGraph). If you drop
support for incoming edges the space will close to the information-theoretical lower bound .
All accessors are very fast. Adjacency tests are very fast and happen in almost constant time.
SuccinctIntDirectedGraph is a much slower implementation with a similar footprint using
Integer as edge type. Please read the class documentation
for more information.
For convenience, and as a compromise with the approach of SuccinctIntDirectedGraph, this
class provides methods getEdgeFromIndex() and
getIndexFromEdge() that map bijectively the edge set into a contiguous set of longs.
- Author:
- Sebastiano Vigna
- See Also:
SuccinctIntDirectedGraph, Serialized Form
-
Nested Class Summary
Nested classes/interfaces inherited from class org.jgrapht.sux4j.AbstractSuccinctDirectedGraph
AbstractSuccinctDirectedGraph.CumulativeDegrees, AbstractSuccinctDirectedGraph.CumulativeSuccessors<E> -
Field Summary
Fields inherited from class org.jgrapht.sux4j.AbstractSuccinctGraph
m, n, sourceShift, targetMask, UNMODIFIABLE -
Constructor Summary
Constructors Constructor Description SuccinctDirectedGraph(int numVertices, int numEdges, java.util.function.Supplier<java.util.stream.Stream<Pair<java.lang.Integer,java.lang.Integer>>> edges)Creates a new immutable succinct directed graph from a supplier of streams of edges, supporting both outgoing and incoming edges.SuccinctDirectedGraph(int numVertices, int numEdges, java.util.function.Supplier<java.util.stream.Stream<Pair<java.lang.Integer,java.lang.Integer>>> edges, boolean incomingEdgesSupport)Creates a new immutable succinct directed graph from a supplier of streams of edges, choosing whether to support incoming edges.SuccinctDirectedGraph(int numVertices, java.util.List<Pair<java.lang.Integer,java.lang.Integer>> edges)Creates a new immutable succinct directed graph from an edge list, supporting both outgoing and incoming edges.SuccinctDirectedGraph(int numVertices, java.util.List<Pair<java.lang.Integer,java.lang.Integer>> edges, boolean incomingEdgesSupport)Creates a new immutable succinct directed graph from an edge list, choosing whether to support incoming edges.SuccinctDirectedGraph(Graph<java.lang.Integer,E> graph)Creates a new immutable succinct directed graph from a given directed graph, supporting both outgoing and incoming edges.SuccinctDirectedGraph(Graph<java.lang.Integer,E> graph, boolean incomingEdgesSupport)Creates a new immutable succinct directed graph from a given directed graph, choosing whether to support incoming edges. -
Method Summary
Modifier and Type Method Description booleancontainsEdge(it.unimi.dsi.fastutil.ints.IntIntPair e)Returnstrueif this graph contains the specified edge.booleancontainsEdge(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)Returnstrueif and only if this graph contains an edge going from the source vertex to the target vertex.java.util.Set<it.unimi.dsi.fastutil.ints.IntIntPair>edgeSet()Returns a set of the edges contained in this graph.java.util.Set<it.unimi.dsi.fastutil.ints.IntIntPair>edgesOf(java.lang.Integer vertex)Returns a set of all edges touching the specified vertex.it.unimi.dsi.fastutil.ints.IntIntPairgetEdge(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)Returns an edge connecting source vertex to target vertex if such vertices and such edge exist in this graph.it.unimi.dsi.fastutil.ints.IntIntPairgetEdgeFromIndex(long i)Returns the edge with given index.java.lang.IntegergetEdgeSource(it.unimi.dsi.fastutil.ints.IntIntPair e)Returns the source vertex of an edge.java.lang.IntegergetEdgeTarget(it.unimi.dsi.fastutil.ints.IntIntPair e)Returns the target vertex of an edge.longgetIndexFromEdge(it.unimi.dsi.fastutil.ints.IntIntPair e)Returns the index associated with the given edge.java.util.Set<it.unimi.dsi.fastutil.ints.IntIntPair>incomingEdgesOf(java.lang.Integer target)Returns a set of all edges incoming into the specified vertex.intinDegreeOf(java.lang.Integer vertex)Returns the "in degree" of the specified vertex.GraphIterables<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>iterables()Access the graph using theGraphIterablesinterface.intoutDegreeOf(java.lang.Integer vertex)Returns the "out degree" of the specified vertex.java.util.Set<it.unimi.dsi.fastutil.ints.IntIntPair>outgoingEdgesOf(java.lang.Integer vertex)Returns a set of all edges outgoing from the specified vertex.Methods inherited from class org.jgrapht.sux4j.AbstractSuccinctGraph
addEdge, addEdge, addVertex, addVertex, assertVertexExist, containsVertex, getAllEdges, getEdgeSupplier, getEdgeWeight, getVertexSupplier, removeEdge, removeEdge, removeVertex, setEdgeWeight, vertexSetMethods inherited from class org.jgrapht.graph.AbstractGraph
equals, hashCode, removeAllEdges, removeAllEdges, removeAllEdges, removeAllVertices, toString, toStringFromSets
-
Constructor Details
-
SuccinctDirectedGraph
Creates a new immutable succinct directed graph from a given directed graph, choosing whether to support incoming edges.- Type Parameters:
E- the graph edge type- Parameters:
graph- a directed graph: for good results, vertices should be numbered consecutively starting from 0.incomingEdgesSupport- whether to support incoming edges or not.
-
SuccinctDirectedGraph
Creates a new immutable succinct directed graph from a given directed graph, supporting both outgoing and incoming edges.- Type Parameters:
E- the graph edge type- Parameters:
graph- a directed graph: for good results, vertices should be numbered consecutively starting from 0.
-
SuccinctDirectedGraph
public SuccinctDirectedGraph(int numVertices, java.util.List<Pair<java.lang.Integer,java.lang.Integer>> edges, boolean incomingEdgesSupport)Creates a new immutable succinct directed graph from an edge list, choosing whether to support incoming edges.This constructor just builds a
SparseIntDirectedGraphand delegates to the main constructor.- Parameters:
numVertices- the number of vertices.edges- the edge list.incomingEdgesSupport- whether to support incoming edges or not.- See Also:
SuccinctDirectedGraph(Graph)
-
SuccinctDirectedGraph
public SuccinctDirectedGraph(int numVertices, java.util.List<Pair<java.lang.Integer,java.lang.Integer>> edges)Creates a new immutable succinct directed graph from an edge list, supporting both outgoing and incoming edges.This constructor just builds a
SparseIntDirectedGraphand delegates to the main constructor.- Parameters:
numVertices- the number of vertices.edges- the edge list.- See Also:
SuccinctDirectedGraph(Graph)
-
SuccinctDirectedGraph
public SuccinctDirectedGraph(int numVertices, int numEdges, java.util.function.Supplier<java.util.stream.Stream<Pair<java.lang.Integer,java.lang.Integer>>> edges, boolean incomingEdgesSupport)Creates a new immutable succinct directed graph from a supplier of streams of edges, choosing whether to support incoming edges.This constructor just builds a
SparseIntDirectedGraphand delegates to the main constructor.- Parameters:
numVertices- the number of vertices.numEdges- the number of edges.edges- a supplier of streams of edges.incomingEdgesSupport- whether to support incoming edges or not.- See Also:
SuccinctDirectedGraph(Graph)
-
SuccinctDirectedGraph
public SuccinctDirectedGraph(int numVertices, int numEdges, java.util.function.Supplier<java.util.stream.Stream<Pair<java.lang.Integer,java.lang.Integer>>> edges)Creates a new immutable succinct directed graph from a supplier of streams of edges, supporting both outgoing and incoming edges.This constructor just builds a
SparseIntDirectedGraphand delegates to the main constructor.- Parameters:
numVertices- the number of vertices.numEdges- the number of edges.edges- a supplier of streams of edges.- See Also:
SuccinctDirectedGraph(Graph)
-
-
Method Details
-
containsEdge
public boolean containsEdge(it.unimi.dsi.fastutil.ints.IntIntPair e)Description copied from interface:GraphReturnstrueif this graph contains the specified edge. More formally, returnstrueif and only if this graph contains an edgee2such thate.equals(e2). If the specified edge isnullreturnsfalse.- Specified by:
containsEdgein interfaceGraph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>- Parameters:
e- edge whose presence in this graph is to be tested.- Returns:
trueif this graph contains the specified edge.
-
edgeSet
public java.util.Set<it.unimi.dsi.fastutil.ints.IntIntPair> edgeSet()Description copied from interface:GraphReturns a set of the edges contained in this graph. The set is backed by the graph, so changes to the graph are reflected in the set. If the graph is modified while an iteration over the set is in progress, the results of the iteration are undefined.The graph implementation may maintain a particular set ordering (e.g. via
LinkedHashSet) for deterministic iteration, but this is not required. It is the responsibility of callers who rely on this behavior to only use graph implementations which support it. -
edgesOf
public java.util.Set<it.unimi.dsi.fastutil.ints.IntIntPair> edgesOf(java.lang.Integer vertex)Description copied from interface:GraphReturns a set of all edges touching the specified vertex. If no edges are touching the specified vertex returns an empty set. -
inDegreeOf
public int inDegreeOf(java.lang.Integer vertex)Description copied from interface:GraphReturns the "in degree" of the specified vertex.The "in degree" of a vertex in a directed graph is the number of inward directed edges from that vertex. See http://mathworld.wolfram.com/Indegree.html.
In the case of undirected graphs this method returns the number of edges touching the vertex. Edges with same source and target vertices (self-loops) are counted twice.
- Specified by:
inDegreeOfin interfaceGraph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>- Parameters:
vertex- vertex whose degree is to be calculated.- Returns:
- the degree of the specified vertex.
-
incomingEdgesOf
public java.util.Set<it.unimi.dsi.fastutil.ints.IntIntPair> incomingEdgesOf(java.lang.Integer target)Description copied from interface:GraphReturns a set of all edges incoming into the specified vertex.In the case of undirected graphs this method returns all edges touching the vertex, thus, some of the returned edges may have their source and target vertices in the opposite order.
- Specified by:
incomingEdgesOfin interfaceGraph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>- Parameters:
target- the vertex for which the list of incoming edges to be returned.- Returns:
- a set of all edges incoming into the specified vertex.
-
outDegreeOf
public int outDegreeOf(java.lang.Integer vertex)Description copied from interface:GraphReturns the "out degree" of the specified vertex.The "out degree" of a vertex in a directed graph is the number of outward directed edges from that vertex. See http://mathworld.wolfram.com/Outdegree.html.
In the case of undirected graphs this method returns the number of edges touching the vertex. Edges with same source and target vertices (self-loops) are counted twice.
- Specified by:
outDegreeOfin interfaceGraph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>- Parameters:
vertex- vertex whose degree is to be calculated.- Returns:
- the degree of the specified vertex.
-
outgoingEdgesOf
public java.util.Set<it.unimi.dsi.fastutil.ints.IntIntPair> outgoingEdgesOf(java.lang.Integer vertex)Description copied from interface:GraphReturns a set of all edges outgoing from the specified vertex.In the case of undirected graphs this method returns all edges touching the vertex, thus, some of the returned edges may have their source and target vertices in the opposite order.
- Specified by:
outgoingEdgesOfin interfaceGraph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>- Parameters:
vertex- the vertex for which the list of outgoing edges to be returned.- Returns:
- a set of all edges outgoing from the specified vertex.
-
getEdgeSource
public java.lang.Integer getEdgeSource(it.unimi.dsi.fastutil.ints.IntIntPair e)Description copied from interface:GraphReturns the source vertex of an edge. For an undirected graph, source and target are distinguishable designations (but without any mathematical meaning).- Specified by:
getEdgeSourcein interfaceGraph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>- Parameters:
e- edge of interest- Returns:
- source vertex
-
getEdgeTarget
public java.lang.Integer getEdgeTarget(it.unimi.dsi.fastutil.ints.IntIntPair e)Description copied from interface:GraphReturns the target vertex of an edge. For an undirected graph, source and target are distinguishable designations (but without any mathematical meaning).- Specified by:
getEdgeTargetin interfaceGraph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>- Parameters:
e- edge of interest- Returns:
- target vertex
-
getIndexFromEdge
public long getIndexFromEdge(it.unimi.dsi.fastutil.ints.IntIntPair e)Returns the index associated with the given edge.- Parameters:
e- an edge of the graph.- Returns:
- the index associated with the edge, or −1 if the edge is not part of the graph.
- See Also:
getEdgeFromIndex(long)
-
getEdgeFromIndex
public it.unimi.dsi.fastutil.ints.IntIntPair getEdgeFromIndex(long i)Returns the edge with given index.- Parameters:
i- an index between 0 (included) and the number of edges (excluded).- Returns:
- the pair with index
i. - See Also:
getIndexFromEdge(IntIntPair)
-
getEdge
public it.unimi.dsi.fastutil.ints.IntIntPair getEdge(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)Description copied from interface:GraphReturns an edge connecting source vertex to target vertex if such vertices and such edge exist in this graph. Otherwise returnsnull. If any of the specified vertices isnullreturnsnullIn undirected graphs, the returned edge may have its source and target vertices in the opposite order.
-
containsEdge
public boolean containsEdge(java.lang.Integer sourceVertex, java.lang.Integer targetVertex)Description copied from interface:GraphReturnstrueif and only if this graph contains an edge going from the source vertex to the target vertex. In undirected graphs the same result is obtained when source and target are inverted. If any of the specified vertices does not exist in the graph, or if isnull, returnsfalse.- Specified by:
containsEdgein interfaceGraph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>- Overrides:
containsEdgein classAbstractGraph<java.lang.Integer,it.unimi.dsi.fastutil.ints.IntIntPair>- Parameters:
sourceVertex- source vertex of the edge.targetVertex- target vertex of the edge.- Returns:
trueif this graph contains the specified edge.- See Also:
Graph.containsEdge(Object, Object)
-
iterables
Description copied from interface:GraphAccess the graph using theGraphIterablesinterface. This allows accessing graphs without the restrictions imposed by 32-bit arithmetic. Moreover, graph implementations are free to implement this interface without explicitly materializing intermediate results.
-