Package com.jgalgo.io

Class DimacsGraphReader

java.lang.Object
com.jgalgo.io.DimacsGraphReader
All Implemented Interfaces:
GraphReader<Integer,Integer>

public final class DimacsGraphReader extends Object
Read a graph in 'DIMACS' format.

The DIMACS format is the graph format used by the 'Center of Discrete Mathematics and Theoretical Computer Science' for their graph challenges. There are many sub-formats, but the most common are the 'edge' and 'sp' formats, which are supported by this class. A DIMACS file contains a short header which specify the sub-format along with the number of edges and vertices, followed by the edges themselves. The edges are specified by a pair of vertices, and in the 'sp' format, also by a weight. The vertices are numbered from 1 to n, where n is the number of vertices, and similarly the edges are numbered from 1 to m, where m is the number of edges. Only undirected graphs are supported by this format.

The 'edge' format is the simplest, and is used for unweighted undirected graphs. An example file is:

 c this is a comment
 c this is the graph with vertices {1,2,3,4,5} and edges {1=(1,2),2=(2,3),3=(2,4),4=(3,4),5=(4,5)}
 p edge 5 5
 e 1 2
 e 2 3
 e 2 4
 e 3 4
 e 4 5
 

The 'sp' format is used for (integer) weighted undirected graphs. An example file is:

 c this is a comment
 c this is the graph with vertices {1,2,3,4,5} and edges {1=(1,2),2=(2,3),3=(2,4),4=(3,4),5=(4,5)}
 c the weights of the edges are {1=5,2=13,3=2,4=-7,5=0}
 p sp 5 5
 e 1 2 3
 e 2 3 13
 e 2 4 2
 e 3 4 -7
 e 4 5 0
 

The reader will identify the format automatically by looking at the header of the file. If the format contains edge weights ('sp' format), the built graph will have edges integer weights keys by "weight", or a key chosen by the user using setEdgeWeightsKey(String). See Graph.edgesWeights(String).

Author:
Barak Ugav
See Also:
  • Constructor Details

    • DimacsGraphReader

      public DimacsGraphReader()
      Create a new reader.
  • Method Details

    • setEdgeWeightsKey

      public void setEdgeWeightsKey(String weightsKey)
      Sets the key of the edge weights that will be read.

      When the reader reads a graph in the 'sp' format a WeightsInt weights will be added to the built graph. By default, the weights will be added with key "weight". Use this method to specify a different key.

      Parameters:
      weightsKey - the key of the edge weights that will be read
      See Also:
    • readGraph

      public IntGraph readGraph(Reader reader)
      Description copied from interface: GraphReader
      Read a graph from an I/O reader.
      Parameters:
      reader - an I/O reader that contain a graph description
      Returns:
      a new graph read from the reader
    • readGraph

      public IntGraph readGraph(File file)
      Description copied from interface: GraphReader
      Read a graph from a file.
      Parameters:
      file - a file that contain a graph description
      Returns:
      a new graph read from the file
    • readGraph

      public IntGraph readGraph(String path)
      Description copied from interface: GraphReader
      Read a graph from a file, given a path to it.
      Parameters:
      path - a path to a file that contain a graph description
      Returns:
      a new graph read from the file
    • readIntoBuilder

      public final IntGraphBuilder readIntoBuilder(Reader reader)
      Description copied from interface: GraphReader
      Read a graph from an I/O reader into a GraphBuilder.
      Specified by:
      readIntoBuilder in interface GraphReader<Integer,Integer>
      Parameters:
      reader - an I/O reader that contain a graph description
      Returns:
      a graph builder containing the vertices and edge read from the reader