Class SuccinctIntUndirectedGraph

All Implemented Interfaces:
Serializable, Graph<Integer,Integer>

public class SuccinctIntUndirectedGraph extends AbstractSuccinctUndirectedGraph<Integer> implements Serializable
An immutable undirected graph with Integer edges represented using quasi-succinct data structures.

The graph representation of this implementation is similar to that of SparseIntDirectedGraph: nodes and edges are initial intervals of the natural numbers. Under the hood, however, this class uses the Elias–Fano representation of monotone sequences to represent the positions of ones in the (linearized) adjacency matrix of the graph. 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 the information-theoretical lower bound (typically, a few times smaller than a SparseIntUndirectedGraph).

Enumeration of edges is very slow. Adjacency tests are very fast and happen in almost constant time.

SuccinctUndirectedGraph is a much faster implementation with a similar footprint using IntIntSortedPair as edge type. Please read the class documentation for more information.

Author:
Sebastiano Vigna
See Also:
  • Constructor Details

    • SuccinctIntUndirectedGraph

      public SuccinctIntUndirectedGraph(Graph<Integer,E> graph)
      Creates a new immutable succinct undirected graph from a given undirected graph.
      Type Parameters:
      E - the graph edge type
      Parameters:
      graph - an undirected graph: for good results, vertices should be numbered consecutively starting from 0.
    • SuccinctIntUndirectedGraph

      public SuccinctIntUndirectedGraph(int numVertices, List<Pair<Integer,Integer>> edges)
      Creates a new immutable succinct undirected graph from an edge list.

      This constructor just builds a SparseIntUndirectedGraph and delegates to the main constructor.

      Parameters:
      numVertices - the number of vertices.
      edges - the edge list.
      See Also:
  • Method Details

    • containsEdge

      public boolean containsEdge(Integer e)
      Description copied from interface: Graph
      Returns true if this graph contains the specified edge. More formally, returns true if and only if this graph contains an edge e2 such that e.equals(e2). If the specified edge is null returns false.
      Specified by:
      containsEdge in interface Graph<Integer,Integer>
      Parameters:
      e - edge whose presence in this graph is to be tested.
      Returns:
      true if this graph contains the specified edge.
    • edgeSet

      public Set<Integer> 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.

      Specified by:
      edgeSet in interface Graph<Integer,Integer>
      Returns:
      a set of the edges contained in this graph.
    • 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".

      Specified by:
      degreeOf in interface Graph<Integer,Integer>
      Parameters:
      vertex - vertex whose degree is to be calculated.
      Returns:
      the degree of the specified vertex.
    • edgesOf

      public it.unimi.dsi.fastutil.ints.IntSet 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.
      Specified by:
      edgesOf in interface Graph<Integer,Integer>
      Parameters:
      vertex - the vertex for which a set of touching edges is to be returned.
      Returns:
      a set of all edges touching the specified vertex.
    • incomingEdgesOf

      public it.unimi.dsi.fastutil.ints.IntSet 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 interface Graph<Integer,Integer>
      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.
    • outgoingEdgesOf

      public it.unimi.dsi.fastutil.ints.IntSet 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 interface Graph<Integer,Integer>
      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 Integer getEdgeSource(Integer e)
      Description copied from interface: Graph
      Returns the source vertex of an edge. For an undirected graph, source and target are distinguishable designations (but without any mathematical meaning).
      Specified by:
      getEdgeSource in interface Graph<Integer,Integer>
      Parameters:
      e - edge of interest
      Returns:
      source vertex
    • getEdgeTarget

      public Integer getEdgeTarget(Integer e)
      Description copied from interface: Graph
      Returns the target vertex of an edge. For an undirected graph, source and target are distinguishable designations (but without any mathematical meaning).
      Specified by:
      getEdgeTarget in interface Graph<Integer,Integer>
      Parameters:
      e - edge of interest
      Returns:
      target vertex
    • getEdge

      public Integer getEdge(Integer sourceVertex, Integer targetVertex)
      Description copied from interface: Graph
      Returns an edge connecting source vertex to target vertex if such vertices and such edge exist in this graph. Otherwise returns null. If any of the specified vertices is null returns null

      In undirected graphs, the returned edge may have its source and target vertices in the opposite order.

      Specified by:
      getEdge in interface Graph<Integer,Integer>
      Parameters:
      sourceVertex - source vertex of the edge.
      targetVertex - target vertex of the edge.
      Returns:
      an edge connecting source vertex to target vertex.
    • containsEdge

      public boolean containsEdge(Integer sourceVertex, Integer targetVertex)
      Description copied from interface: Graph
      Returns true if 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 is null, returns false.
      Specified by:
      containsEdge in interface Graph<Integer,Integer>
      Overrides:
      containsEdge in class AbstractGraph<Integer,Integer>
      Parameters:
      sourceVertex - source vertex of the edge.
      targetVertex - target vertex of the edge.
      Returns:
      true if this graph contains the specified edge.
      See Also:
    • assertEdgeExist

      protected boolean assertEdgeExist(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:
      IllegalArgumentException - if specified edge does not exist in this graph.
    • iterables

      public GraphIterables<Integer,Integer> iterables()
      Description copied from interface: Graph
      Access the graph using the GraphIterables 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.
      Specified by:
      iterables in interface Graph<Integer,Integer>
      Returns:
      the graph iterables