Package com.jgalgo.io

Class Sparse6GraphWriter

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

    public final class Sparse6GraphWriter
    extends Object
    Write a graph in 'sparse6' format.

    'sparse6' is a format for storing undirected graphs in a compact manner, using only printable ASCII characters. Files in these format have text type and contain one line per graph. It is space-efficient for large sparse graphs. The format support graphs with vertices numbered 0..n-1 only, where n is the number of vertices, and edges numbered 0..m-1 only, where m is the number of edges. A sparse6 file contains the number of vertices, following by a list of edges, encoded in bytes, each in range 63..126 which are the printable ASCII characters. Each byte encode 6 bits.

    The format does not support specifying the ids of the edges, therefore the writer will not write them. Nevertheless, its possible to write and later read the graph using the Sparse6GraphReader class while keeping the edges ids: if the edges in the written graph are numbered 0..m-1, and when they are iterated from 0 to m-1 they are also ordered similar to (0,0),(1,0),(1,1),(2,0),(2,1),(2,2), then the reader will assigned the same ids to the edges. . This way of keeping the edges ids is enforced by the writer by default, but can be disabled by calling keepEdgesIds(boolean) with false.

    Self edges and parallel edges are supported by the format. The full description of the format can be found here, which also define the 'graph6' format, which is used for dense graph. See Graph6GraphWriter for writing graphs in 'graph6' format.

    File with a graph in 'sparse6' format usually have the extension .s6.

    Author:
    Barak Ugav
    See Also:
    Sparse6GraphReader
    • Constructor Detail

      • Sparse6GraphWriter

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

      • keepEdgesIds

        public void keepEdgesIds​(boolean enable)
        Enable or disable keeping the edges ids.

        The format does not support specifying the ids of the edges, therefore the writer will not write them. Nevertheless, its possible to write and later read the graph using the Sparse6GraphReader class while keeping the edges ids: if the edges in the written graph are numbered 0..m-1, and when they are iterated from 0 to m-1 they are also ordered similar to (0,0),(1,0),(1,1),(2,0),(2,1),(2,2), then the reader will assigned the same ids to the edges.

        If this option is enabled, which is also the default, if a graph is written by the writer and the edges iterated from 0 to m-1 are not ordered by the above order, then an exception will be thrown. If this option is disabled, the ids of the edges will be ignored and there is not guarantee that the reader will assign the same ids to the edges.

        Parameters:
        enable - true to enable keeping the edges ids, false to disable it
      • 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