- java.lang.Object
-
- org.jgrapht.graph.AbstractGraph<Integer,E>
-
- org.jgrapht.webgraph.AbstractImmutableGraphAdapter<it.unimi.dsi.fastutil.ints.IntIntSortedPair>
-
- org.jgrapht.webgraph.ImmutableUndirectedGraphAdapter
-
- All Implemented Interfaces:
it.unimi.dsi.lang.FlyweightPrototype<ImmutableUndirectedGraphAdapter>
,Graph<Integer,it.unimi.dsi.fastutil.ints.IntIntSortedPair>
public class ImmutableUndirectedGraphAdapter extends AbstractImmutableGraphAdapter<it.unimi.dsi.fastutil.ints.IntIntSortedPair> implements it.unimi.dsi.lang.FlyweightPrototype<ImmutableUndirectedGraphAdapter>
An adapter class for undirected graphs using WebGraph'sImmutableGraph
.Nodes are instances of
Integer
corresponding to the index of a node in WebGraph. Edges are represented by anIntIntSortedPair
. Edges are canonicalized so that the left element is always smaller than or equal to the right element. Since the underlying graph is immutable, the resulting graph is unmodifiable. Edges are immutable and can be tested for equality (e.g., stored in a dictionary).You need to load a symmetric
ImmutableGraph
using one of the available load methods available, and then build an adapter:immutableGraph = ImmutableGraph.loadMapped("mygraph"); adapter = new ImmutableUndirectedGraphAdapter(immutableGraph);
It is your responsibility that the provided graph is symmetric (for each arc x → y there is an arc y → x). No check will be performed, but you can use the
Check
class to this purpose. Note that computing the number of edges of a graph requires a full scan of the edge set ifImmutableGraph.numArcs()
is not supported (the first time—then it will be cached).If you use a load method that does not provide random access, most methods will throw an
UnsupportedOperationException
.If necessary, you can adapt a big WebGraph graph with at most
Integer.MAX_VALUE
vertices using the suitable wrapper.Thread safety
This class is not thread safe: following the
FlyweightPrototype
pattern, users can access concurrently the graph by getting lightweight copies.Fast adjacency check
As it happens for the sparse representation of JGraphT, usually a WebGraph compressed representation requires scanning the adjacency list of a node to test whether a specific arc exists. However, if you adapt a WebGraph class (such as
EFGraph
) which provides skippable iterators with fast skipping, adjacency can be tested more quickly (e.g., essentially in constant time in the case ofEFGraph
).- Author:
- Sebastiano Vigna
- See Also:
AbstractImmutableBigGraphAdapter
-
-
Field Summary
-
Fields inherited from class org.jgrapht.webgraph.AbstractImmutableGraphAdapter
immutableGraph, m, n
-
Fields inherited from interface org.jgrapht.Graph
DEFAULT_EDGE_WEIGHT
-
-
Constructor Summary
Constructors Constructor Description ImmutableUndirectedGraphAdapter(it.unimi.dsi.webgraph.ImmutableGraph immutableGraph)
Creates an adapter for an undirected (i.e., symmetric) immutable graph.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
containsEdge(it.unimi.dsi.fastutil.ints.IntIntSortedPair e)
Returnstrue
if this graph contains the specified edge.ImmutableUndirectedGraphAdapter
copy()
int
degreeOf(Integer vertex)
Returns the degree of the specified vertex.Set<it.unimi.dsi.fastutil.ints.IntIntSortedPair>
edgeSet()
Returns a set of the edges contained in this graph.Set<it.unimi.dsi.fastutil.ints.IntIntSortedPair>
edgesOf(Integer vertex)
Returns a set of all edges touching the specified vertex.GraphType
getType()
Get the graph type.Set<it.unimi.dsi.fastutil.ints.IntIntSortedPair>
incomingEdgesOf(Integer vertex)
Returns a set of all edges incoming into the specified vertex.int
inDegreeOf(Integer vertex)
Returns the "in degree" of the specified vertex.GraphIterables<Integer,it.unimi.dsi.fastutil.ints.IntIntSortedPair>
iterables()
Access the graph using theGraphIterables
interface.protected it.unimi.dsi.fastutil.ints.IntIntSortedPair
makeEdge(int x, int y)
int
outDegreeOf(Integer vertex)
Returns the "out degree" of the specified vertex.Set<it.unimi.dsi.fastutil.ints.IntIntSortedPair>
outgoingEdgesOf(Integer vertex)
Returns a set of all edges outgoing from the specified vertex.-
Methods inherited from class org.jgrapht.webgraph.AbstractImmutableGraphAdapter
addEdge, addEdge, addVertex, addVertex, containsEdge, containsEdgeFast, containsVertex, getAllEdges, getEdge, getEdgeSource, getEdgeSupplier, getEdgeTarget, getEdgeWeight, getVertexSupplier, removeEdge, removeEdge, removeVertex, setEdgeWeight, vertexSet
-
Methods inherited from class org.jgrapht.graph.AbstractGraph
assertVertexExist, 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
-
-
-
-
Constructor Detail
-
ImmutableUndirectedGraphAdapter
public ImmutableUndirectedGraphAdapter(it.unimi.dsi.webgraph.ImmutableGraph immutableGraph)
Creates an adapter for an undirected (i.e., symmetric) immutable graph.It is responsibility of the caller that the provided graph has is symmetric (for each arc x → y there is an arc y → x). If this property is not true, results will be unpredictable.
- Parameters:
immutableGraph
- a symmetric immutable graph.
-
-
Method Detail
-
makeEdge
protected it.unimi.dsi.fastutil.ints.IntIntSortedPair makeEdge(int x, int y)
- Specified by:
makeEdge
in classAbstractImmutableGraphAdapter<it.unimi.dsi.fastutil.ints.IntIntSortedPair>
-
containsEdge
public boolean containsEdge(it.unimi.dsi.fastutil.ints.IntIntSortedPair e)
Description copied from interface:Graph
Returnstrue
if this graph contains the specified edge. More formally, returnstrue
if and only if this graph contains an edgee2
such thate.equals(e2)
. If the specified edge isnull
returnsfalse
.- Specified by:
containsEdge
in interfaceGraph<Integer,it.unimi.dsi.fastutil.ints.IntIntSortedPair>
- Parameters:
e
- edge whose presence in this graph is to be tested.- Returns:
true
if this graph contains the specified edge.
-
edgeSet
public Set<it.unimi.dsi.fastutil.ints.IntIntSortedPair> edgeSet()
Description copied from interface:Graph
Returns 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.
-
degreeOf
public int degreeOf(Integer vertex)
Description copied from interface:Graph
Returns the degree of the specified vertex.A degree of a vertex in an undirected graph is the number of edges touching that vertex. Edges with same source and target vertices (self-loops) are counted twice.
In directed graphs this method returns the sum of the "in degree" and the "out degree".
-
edgesOf
public Set<it.unimi.dsi.fastutil.ints.IntIntSortedPair> edgesOf(Integer vertex)
Description copied from interface:Graph
Returns 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(Integer vertex)
Description copied from interface:Graph
Returns 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:
inDegreeOf
in interfaceGraph<Integer,it.unimi.dsi.fastutil.ints.IntIntSortedPair>
- Parameters:
vertex
- vertex whose degree is to be calculated.- Returns:
- the degree of the specified vertex.
-
incomingEdgesOf
public Set<it.unimi.dsi.fastutil.ints.IntIntSortedPair> incomingEdgesOf(Integer vertex)
Description copied from interface:Graph
Returns 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:
incomingEdgesOf
in interfaceGraph<Integer,it.unimi.dsi.fastutil.ints.IntIntSortedPair>
- Parameters:
vertex
- 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(Integer vertex)
Description copied from interface:Graph
Returns 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:
outDegreeOf
in interfaceGraph<Integer,it.unimi.dsi.fastutil.ints.IntIntSortedPair>
- Parameters:
vertex
- vertex whose degree is to be calculated.- Returns:
- the degree of the specified vertex.
-
outgoingEdgesOf
public Set<it.unimi.dsi.fastutil.ints.IntIntSortedPair> outgoingEdgesOf(Integer vertex)
Description copied from interface:Graph
Returns 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:
outgoingEdgesOf
in interfaceGraph<Integer,it.unimi.dsi.fastutil.ints.IntIntSortedPair>
- 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.
-
getType
public GraphType 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.
-
copy
public ImmutableUndirectedGraphAdapter copy()
- Specified by:
copy
in interfaceit.unimi.dsi.lang.FlyweightPrototype<ImmutableUndirectedGraphAdapter>
-
iterables
public GraphIterables<Integer,it.unimi.dsi.fastutil.ints.IntIntSortedPair> iterables()
Description copied from interface:Graph
Access the graph using theGraphIterables
interface. 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.
-
-