Class DimacsGraphWriter
- All Implemented Interfaces:
GraphWriter<Integer,
Integer>
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:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
setEdgeWeights
(String weightsKey) Set the key of the edge weights to write.final void
writeGraph
(Graph<Integer, Integer> 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
-
Constructor Details
-
DimacsGraphWriter
public DimacsGraphWriter()Create a new writer.
-
-
Method Details
-
setEdgeWeights
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:
-
writeGraphImpl
- Throws:
IOException
-
writeGraph
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
-