Exporters

The following exporters are available:

jgrapht.io.exporters.generate_csv(graph, format='adjacencylist', export_edge_weights=False, matrix_format_nodeid=False, matrix_format_zero_when_no_edge=True, export_vertex_id_cb=None)[source]

Export a graph to string using the CSV format.

The exporter supports various different formats which can be adjusted using the format parameter. The supported formats are the same CSV formats used by Gephi. The exporter respects rfc4180.

Parameters
  • graph – the graph

  • format – a string with the format to use. Valid are maxclique, shortestpath and coloring.

  • export_edge_weights – whether to export edge weights

  • matrix_format_node_id – only for the matrix format, whether to export node identifiers

  • matrix_format_zero_when_noedge – only for the matrix format, whether the output should contain zero for missing edges

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Returns

a string contains the exported graph

Raises

IOError – in case of an export error

jgrapht.io.exporters.generate_dimacs(graph, format='maxclique', export_edge_weights=False, export_vertex_id_cb=None)[source]

Export a graph to a string using the DIMACS format.

For a description of the formats see http://dimacs.rutgers.edu/Challenges . Note that there a lot of different formats based on each different challenge. The exports supports the shortest path challenge format, the coloring format and the maximum-clique challenge formats. By default the maximum-clique is used.

Note

In DIMACS formats the vertices are integers numbered from one. In case of default graphs (with integer vertices) this translation happens automatically. With any-hashable graphs the user must either use positive integers as vertices, or must explicitly provide a function which does the conversion to a positive integer (export_vertex_id_cb).

Briefly, one of the most common DIMACS formats is the 2nd DIMACS challenge and follows the following structure:

DIMACS G {
  c <comments> ignored during parsing of the graph
  p edge <number of nodes> <number of edges>
  e <edge source 1> <edge target 1>
  e <edge source 2> <edge target 2>
  e <edge source 3> <edge target 3>
  e <edge source 4> <edge target 4>
  ...
}

Although not specified directly in the DIMACS format documentation, this implementation also allows for the a weighted variant:

e <edge source 1> <edge target 1> <edge_weight>
Parameters
  • graph – the graph

  • format – a string with the format to use. Valid are maxclique, shortestpath and coloring.

  • export_edge_weights – whether to also export edge weights

  • export_vertex_id_cb – function which converts from vertex to positive integer identifiers to be written to the output

Returns

a string containing the exported graph

jgrapht.io.exporters.generate_dot(graph, per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_vertex_id_cb=None)[source]

Exports a graph to string using DOT format.

For a description of the format see https://en.wikipedia.org/wiki/DOT_language.

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Parameters
  • graph – The graph to export

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Returns

a string contains the exported graph

Raises

IOError – In case of an export error

jgrapht.io.exporters.generate_gexf(graph, attrs=[], per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_edge_weights=False, export_edge_labels=False, export_edge_types=False, export_meta=False, export_vertex_id_cb=None)[source]

Exports a graph to string using GEXF.

For a description of the format see https://gephi.org/gexf/format/index.html or the GEXF Primer.

Below is small example of a graph in GEXF format:

<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.2draft"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd"
    version="1.2">
    <graph defaultedgetype="undirected">
        <nodes>
        <node id="n0" label="node 0"/>
        <node id="n1" label="node 1"/>
        <node id="n2" label="node 2"/>
        <node id="n3" label="node 3"/>
        <node id="n4" label="node 4"/>
        <node id="n5" label="node 5"/>
        </nodes>
        <edges>
        <edge id="e0" source="n0" target="n2" weight="1.0"/>
        <edge id="e1" source="n0" target="n1" weight="1.0"/>
        <edge id="e2" source="n1" target="n3" weight="2.0"/>
        <edge id="e3" source="n3" target="n2"/>
        <edge id="e4" source="n2" target="n4"/>
        <edge id="e5" source="n3" target="n5"/>
        <edge id="e6" source="n5" target="n4" weight="1.1"/>
        </edges>
    </graph>
</gexf>

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Note

Custom attributes need to be registered in the attrs parameter which accepts a list of tuple(name, category, type, default_value). Type and default value may None. Category must be either node or edge.

Parameters
  • graph – The graph to export

  • attrs – a list of tuples (name, category, type, default_value)

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_edge_weights – whether to export edge weights

  • export_edge_labels – whether to export edge labels

  • export_edge_types – whether to export edge types

  • export_meta – whether to export meta tag

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Returns

a string contains the exported graph

Raises

IOError – In case of an export error

jgrapht.io.exporters.generate_gml(graph, export_edge_weights=False, export_vertex_labels=False, export_edge_labels=False, per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_vertex_id_cb=None)[source]

Export a graph to a string in GML format (Graph Modelling Language).

Below is small example of a graph in GML format:

graph [
    node [
        id 1
    ]
    node [
        id 2
        label "Node 2 has an optional label"
    ]
    node [
        id 3
    ]
    edge [
        source 1
        target 2
        weight 2.0
        label "Edge between 1 and 2"
    ]
    edge [
        source 2
        target 3
        weight 3.0
        label "Edge between 2 and 3"
    ]
]

Note

The exporter escapes all strings as Java strings.

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Parameters
  • graph – the graph

  • export_edge_weights – whether to export edge weights

  • export_vertex_labels – whether to export a vertex attribute called “label”. Even if such an attribute is not provided explicitly, it will be autogenerated.

  • export_edge_labels – whether to export an edge attribute called “label”. Even if such an attribute is not provided explicitly, it will be autogenerated

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Returns

a string contains the exported graph

Raises

IOError – In case of an export error

jgrapht.io.exporters.generate_graph6(graph)[source]

Exports a graph to string using graph6 format.

See https://users.cecs.anu.edu.au/~bdm/data/formats.txt for a description of the format.

Parameters

graph – The graph to export

Returns

a string contains the exported graph

Raises

IOError – In case of an export error

jgrapht.io.exporters.generate_graphml(graph, attrs=[], per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_edge_weights=False, export_vertex_labels=False, export_edge_labels=False, export_vertex_id_cb=None)[source]

Exports a graph to string using GraphML.

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Note

Custom attributes need to be registered in the attrs parameter which accepts a list of tuple(name, category, type, default_value). Type and default value may None. Category must be either node or edge.

Parameters
  • graph – The graph to export

  • attrs – a list of tuples (name, category, type, default_value)

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_edge_weights – whether to export edge weights

  • export_vertex_labels – whether to export vertex labels

  • export_edge_labels – whether to export edge labels

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Returns

a string contains the exported graph

Raises

IOError – In case of an export error

jgrapht.io.exporters.generate_json(graph, per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_vertex_id_cb=None)[source]

Exports a graph to string using JSON.

The output is one object which contains:

  • A member named nodes whose value is an array of nodes.

  • A member named edges whose value is an array of edges.

  • Two members named creator and version for metadata.

Each node contains an identifier and possibly other attributes. Similarly each edge contains the source and target vertices, a possible identifier and possible other attributes.

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Parameters
  • graph – The graph to export

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Returns

a string contains the exported graph

Raises

IOError – In case of an export error

jgrapht.io.exporters.generate_lemon(graph, export_edge_weights=False, escape_strings=False, export_vertex_id_cb=None)[source]

Exports a graph to a string using the Lemon graph format (LGF). This is the custom graph format used in the Lemon graph library.

Parameters
  • graph – the graph

  • export_edge_weights – whether to also export edge weights

  • escape_strings – whether to escape all strings as Java strings

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Returns

a string contains the exported graph

Raises

IOError – In case of an export error

jgrapht.io.exporters.generate_sparse6(graph)[source]

Exports a graph to string using sparse6 format.

See https://users.cecs.anu.edu.au/~bdm/data/formats.txt for a description of the format.

Parameters

graph – The graph to export

Returns

a string contains the exported graph

Raises

IOError – In case of an export error

jgrapht.io.exporters.write_csv(graph, filename, format='adjacencylist', export_edge_weights=False, matrix_format_nodeid=False, matrix_format_zero_when_no_edge=True, export_vertex_id_cb=None)[source]

Export a graph using the CSV format.

The exporter supports various different formats which can be adjusted using the format parameter. The supported formats are the same CSV formats used by Gephi. The exporter respects rfc4180.

Parameters
  • graph – the graph

  • filename – the filename

  • format – a string with the format to use. Valid are maxclique, shortestpath and coloring.

  • export_edge_weights – whether to export edge weights

  • matrix_format_node_id – only for the matrix format, whether to export node identifiers

  • matrix_format_zero_when_noedge – only for the matrix format, whether the output should contain zero for missing edges

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Raises

IOError – in case of an export error

jgrapht.io.exporters.write_dimacs(graph, filename, format='maxclique', export_edge_weights=False, export_vertex_id_cb=None)[source]

Export a graph using the DIMACS format.

For a description of the formats see http://dimacs.rutgers.edu/Challenges . Note that there a lot of different formats based on each different challenge. The exports supports the shortest path challenge format, the coloring format and the maximum-clique challenge formats. By default the maximum-clique is used.

Note

In DIMACS formats the vertices are integers numbered from one. In case of default graphs (with integer vertices) this translation happens automatically. With any-hashable graphs the user must either use positive integers as vertices, or must explicitly provide a function which does the conversion to a positive integer (export_vertex_id_cb).

Briefly, one of the most common DIMACS formats is the 2nd DIMACS challenge and follows the following structure:

DIMACS G {
  c <comments> ignored during parsing of the graph
  p edge <number of nodes> <number of edges>
  e <edge source 1> <edge target 1>
  e <edge source 2> <edge target 2>
  e <edge source 3> <edge target 3>
  e <edge source 4> <edge target 4>
  ...
}

Although not specified directly in the DIMACS format documentation, this implementation also allows for the a weighted variant:

e <edge source 1> <edge target 1> <edge_weight>
Parameters
  • graph – the graph

  • filename – the filename

  • format – a string with the format to use. Valid are maxclique, shortestpath and coloring.

  • export_edge_weights – whether to also export edge weights

  • export_vertex_id_cb – function which converts from vertex to positive integer identifiers to be written to the output

jgrapht.io.exporters.write_dot(graph, filename, per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_vertex_id_cb=None)[source]

Exports a graph to DOT format.

For a description of the format see https://en.wikipedia.org/wiki/DOT_language.

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Parameters
  • graph – The graph to export

  • filename – Filename to write

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Raises

IOError – In case of an export error

jgrapht.io.exporters.write_gexf(graph, filename, attrs=[], per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_edge_weights=False, export_edge_labels=False, export_edge_types=False, export_meta=False, export_vertex_id_cb=None)[source]

Exports a graph to a GEXF file.

For a description of the format see https://gephi.org/gexf/format/index.html or the GEXF Primer.

Below is small example of a graph in GEXF format:

<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.2draft"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd"
    version="1.2">
    <graph defaultedgetype="undirected">
        <nodes>
        <node id="n0" label="node 0"/>
        <node id="n1" label="node 1"/>
        <node id="n2" label="node 2"/>
        <node id="n3" label="node 3"/>
        <node id="n4" label="node 4"/>
        <node id="n5" label="node 5"/>
        </nodes>
        <edges>
        <edge id="e0" source="n0" target="n2" weight="1.0"/>
        <edge id="e1" source="n0" target="n1" weight="1.0"/>
        <edge id="e2" source="n1" target="n3" weight="2.0"/>
        <edge id="e3" source="n3" target="n2"/>
        <edge id="e4" source="n2" target="n4"/>
        <edge id="e5" source="n3" target="n5"/>
        <edge id="e6" source="n5" target="n4" weight="1.1"/>
        </edges>
    </graph>
</gexf>

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Note

Custom attributes need to be registered in the attrs parameter which accepts a list of tuple(name, category, type, default_value). Type and default value may None. Category must be either node or edge.

Parameters
  • graph – The graph to export

  • filename – Filename to write

  • attrs – a list of tuples (name, category, type, default_value)

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_edge_weights – whether to export edge weights

  • export_edge_labels – whether to export edge labels

  • export_edge_types – whether to export edge types

  • export_meta – whether to export meta tag

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Raises

IOError – In case of an export error

jgrapht.io.exporters.write_gml(graph, filename, export_edge_weights=False, export_vertex_labels=False, export_edge_labels=False, per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_vertex_id_cb=None)[source]

Export a graph in GML format (Graph Modelling Language).

Below is small example of a graph in GML format:

graph [
    node [
        id 1
    ]
    node [
        id 2
        label "Node 2 has an optional label"
    ]
    node [
        id 3
    ]
    edge [
        source 1
        target 2
        weight 2.0
        label "Edge between 1 and 2"
    ]
    edge [
        source 2
        target 3
        weight 3.0
        label "Edge between 2 and 3"
    ]
]

Note

The exporter escapes all strings as Java strings.

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Parameters
  • graph – the graph

  • filename – the filename

  • export_edge_weights – whether to export edge weights

  • export_vertex_labels – whether to export a vertex attribute called “label”. Even if such an attribute is not provided explicitly, it will be autogenerated.

  • export_edge_labels – whether to export an edge attribute called “label”. Even if such an attribute is not provided explicitly, it will be autogenerated

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Raises

IOError – In case of an export error

jgrapht.io.exporters.write_graph6(graph, filename)[source]

Exports a graph to graph6 format.

See https://users.cecs.anu.edu.au/~bdm/data/formats.txt for a description of the format.

Parameters
  • graph – The graph to export

  • filename – Filename to write

Raises

IOError – In case of an export error

jgrapht.io.exporters.write_graphml(graph, filename, attrs=[], per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_edge_weights=False, export_vertex_labels=False, export_edge_labels=False, export_vertex_id_cb=None)[source]

Exports a graph to a GraphML file.

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Note

Custom attributes need to be registered in the attrs parameter which accepts a list of tuple(name, category, type, default_value). Type and default value may None. Category must be either graph, node or edge.

Parameters
  • graph – The graph to export

  • filename – Filename to write

  • attrs – a list of tuples (name, category, type, default_value)

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_edge_weights – whether to export edge weights

  • export_vertex_labels – whether to export vertex labels

  • export_edge_labels – whether to export edge labels

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Raises

IOError – In case of an export error

jgrapht.io.exporters.write_json(graph, filename, per_vertex_attrs_dict=None, per_edge_attrs_dict=None, export_vertex_id_cb=None)[source]

Exports a graph using JSON.

The output is one object which contains:

  • A member named nodes whose value is an array of nodes.

  • A member named edges whose value is an array of edges.

  • Two members named creator and version for metadata.

Each node contains an identifier and possibly other attributes. Similarly each edge contains the source and target vertices, a possible identifier and possible other attributes.

Note

Custom attributes are supported with per vertex and per edge dictionaries. These custom attributes are merged with the attributes of any-hashable graphs.

Parameters
  • graph – The graph to export

  • filename – Filename to write

  • per_vertex_attrs_dict – per vertex attribute dicts

  • per_edge_attrs_dict – per edge attribute dicts

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Raises

IOError – In case of an export error

jgrapht.io.exporters.write_lemon(graph, filename, export_edge_weights=False, escape_strings=False, export_vertex_id_cb=None)[source]

Exports a graph into Lemon graph format (LGF). This is the custom graph format used in the Lemon graph library.

Parameters
  • graph – the graph

  • filename – the filename

  • export_edge_weights – whether to also export edge weights

  • escape_strings – whether to escape all strings as Java strings

  • export_vertex_id_cb – function which converts from vertex to identifier to be written to the output

Raises

IOError – In case of an export error

jgrapht.io.exporters.write_sparse6(graph, filename)[source]

Exports a graph to sparse6 format.

See https://users.cecs.anu.edu.au/~bdm/data/formats.txt for a description of the format.

Parameters
  • graph – The graph to export

  • filename – Filename to write

Raises

IOError – In case of an export error