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)
. SeeGraph.edgesWeights(String)
.- Author:
- Barak Ugav
- See Also:
- DIMACS Graph Format,
DimacsGraphWriter
-
-
Constructor Summary
Constructors Constructor Description DimacsGraphReader()
Create a new reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description IntGraph
readGraph(File file)
Read a graph from a file.IntGraph
readGraph(Reader reader)
Read a graph from an I/O reader.IntGraph
readGraph(String path)
Read a graph from a file, given a path to it.IntGraphBuilder
readIntoBuilder(Reader reader)
Read a graph from an I/O reader into aGraphBuilder
.void
setEdgeWeightsKey(String weightsKey)
Sets the key of the edge weights that will be read.
-
-
-
Method Detail
-
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:
Graph.edgesWeights(String)
-
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 aGraphBuilder
.- Specified by:
readIntoBuilder
in interfaceGraphReader<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
-
-