Class DimacsGraphWriter
- java.lang.Object
-
- com.jgalgo.io.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 Summary
Constructors Constructor Description DimacsGraphWriter()
Create a new writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
setEdgeWeights(String weightsKey)
Set the key of the edge weights to write.void
writeGraph(Graph<V,E> graph, Writer writer)
Write a graph to an I/O writer.void
writeGraphImpl(Graph<Integer,Integer> graph, Writer writer)
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.jgalgo.io.GraphWriter
writeGraph, writeGraph
-
-
-
-
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 beWeightFunctionInt
. It can be any of the following:WeightsByte
,WeightsShort
orWeightsInt
.- Parameters:
weightsKey
- the key of the edge weights to write, ornull
to write the graph in the 'edge' format (unweighted)- See Also:
Graph.edgesWeights(String)
-
writeGraphImpl
public void writeGraphImpl(Graph<Integer,Integer> graph, Writer writer) throws IOException
- Throws:
IOException
-
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 interfaceGraphWriter<V,E>
- Parameters:
graph
- a graphwriter
- an I/O writer to which the graph description will be written to
-
-