Reading and writing Graphs

Saving using Graphs.jllg format.

Graphs may be written to I/O streams and files using the savegraph function and read with the loadgraph function. The default graph format is a bespoke compressed Graphs.jl format LG.

Example


g = erdos_renyi(5, 0.2)

savegraph("mygraph.lgz", g)
reloaded_g = loadgraph("mygraph.lgz")

In addition, graphs can also be saved in an uncompressed format using the compress=false option.


savegraph("mygraph.lg", g, compress=false)

reloaded_g = loadgraph("mygraph.lg")

Finally, dictionaries of graphs can also be saved and subsequently re-loaded one by one.

graph_dict = {"g1" => erdos_renyi(5, 0.1),
              "g2" => erdos_renyi(10, 0.2),
              "g3" => erdos_renyi(2, 0.9)}

savegraph("mygraph_dict.lg", graph_dict)

# Re-load only graph g1
reloaded_g1 = loadgraph("mygraph_dict.lg", "g1")

Full docs

Graphs.loadgraphMethod
loadgraph(file, gname="graph", format=LGFormat())

Read a graph named gname from file in the format format.

Implementation Notes

gname is graph-format dependent and is only used if the file contains multiple graphs; if the file format does not support multiple graphs, this value is ignored. The default value may change in the future.

Graphs.loadgraphsMethod
loadgraphs(file, format=LGFormat())

Load multiple graphs from file in the format format. Return a dictionary mapping graph name to graph.

Implementation Notes

For unnamed graphs the default name "graph" will be used. This default may change in the future.

Graphs.savegraphMethod
savegraph(file, g, gname="graph", format=LGFormat)

Saves a graph g with name gname to file in the format format. Return the number of graphs written.

Implementation Notes

The default graph name assigned to gname may change in the future.

Graphs.savegraphMethod
savegraph(file, g, d, format=LGFormat)

Save a dictionary of graphname => graph to file in the format format. Return the number of graphs written.

Implementation Notes

Will only work if the file format supports multiple graph types.

Reading and Writing using other formats using GraphIO

The GraphIO.jl library provides tools for importing and exporting graph objects using common file types like edgelists, GraphML, Pajek NET, and more.