Package com.jgalgo.io

Class DimacsGraphWriter

  • All Implemented Interfaces:
    GraphWriter<Integer,​Integer>

    public final class DimacsGraphWriter
    extends Object
    Write 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
     

    By default, the writer will write the graph in the 'edge' format. To write the graph in the 'sp' format (with edges weights), use the setEdgeWeights(String) method to specify the key of the edge weights.

    Author:
    Barak Ugav
    See Also:
    DIMACS Graph Format, DimacsGraphReader
    • Constructor Detail

      • DimacsGraphWriter

        public DimacsGraphWriter()
        Create a new writer.
    • Method Detail

      • setEdgeWeights

        public void setEdgeWeights​(String weightsKey)
        Set the key of the edge weights to write.

        By default, the writer will write the graph in the 'edge' format, which is unweighted format. To write the graph in the 'sp' format (with weights), use this method to specify the key of the edge weights. See the class documentation for more details.

        The weights must be integer, namely the Weights must be WeightFunctionInt. It can be any of the following: WeightsByte, WeightsShort or WeightsInt.

        Parameters:
        weightsKey - the key of the edge weights to write, or null to write the graph in the 'edge' format (unweighted)
        See Also:
        Graph.edgesWeights(String)
      • writeGraph

        public final void writeGraph​(Graph<V,​E> graph,
                                     Writer writer)
        Description copied from interface: GraphWriter
        Write a graph to an I/O writer.
        Specified by:
        writeGraph in interface GraphWriter<V,​E>
        Parameters:
        graph - a graph
        writer - an I/O writer to which the graph description will be written to