Reading and writing Graphs
Graphs may be written to I/O streams and files using the writegraph
function and read with the readgraph
function. Currently supported common graph formats are GraphML, GML, Gexf, DOT, Pajek .net, graph-tool gt. If fast I/O and small memory footprint is a priority, use the .gt
binary format.
For network types, when using appropriate formats (e.g. .gml
, .graphml
, .gexf
) properties of graph, vertices, and edges will be read/written as well.
Examples
julia> g = Graph(10, 20)
Graph{Int64}(10, 20)
julia> writegraph("mygraph.gml", g) #format is inferred from the extension
julia> h = readgraph("mygraph.gml")
Graph{Int64}(10, 20)
julia> g == h
true
julia> g = Network(10, 20)
Network(10, 20) with [] graph, [] vertex, [] edge properties
julia> gprop!(g, "A", rand());
julia> eprop!(g, "B", EdgeMap(g, e -> rand()));
julia> vprop!(g, "C", VertexMap(g, v -> rand()));
julia> writenetwork("mygraph.gml", g) #format is inferred from the extension
julia> h = readnetwork("mygraph.gml")
Network(10, 20) with ["A"] graph, ["C"] vertex, ["B"] edge properties
julia> g == h
true
Methods
Erdos.readgraph
— Methodreadgraph(filename, G=Graph)
readgraph(filename, t, G=Graph; compressed=false)
Reads a graph from filename
in the format t
. Returns a graph of type G
or the corresponding digraph/graph type. Compressed files can eventually be read.
Supported formats are :gml, :dot, :graphml, :gexf, :net, :gt
.
If no format is provided, it will be inferred from filename
.
readgraph(s::Symbol, G=Graph)
Read a graph identified by s
from Erdos datasets collection (e.g. s=:karate
). They are stored in the gt
binary format in the datasets
directory of the package. For a list of available graph refer to the documentation.
Erdos.readnetwork
— Methodreadnetwork(filename, G=Network)
readnetwork(filename, t, G=Network; compressed=false)
Read a network from filename
in the format t
. Returns a network of type G
(or the corresponding directed/undirected type if needed). Compressed files can eventually be read.
Supported formats are :gml, :dot, :graphml, :gexf, :net, :gt
. When possible, graph, edge, and vertex properties will be read as well.
If no format is provided, it will be inferred from the filename
.
readnetwork(s::Symbol, G=Network)
Read a network identified by s
from Erdos' datasets collection (e.g. s=:karate
). They are stored in the gt
binary format in the datasets
directory of the package. For a list of available graph refer to the documentation.
Erdos.writegraph
— Methodwritegraph(file, g)
writegraph(file, g, t; compress=false)
Save a graph g
to file
in the format t
.
Eventually the resulting file can be compressed in the gzip format.
Currently supported formats are :gml, :graphml, :gexf, :dot, :net, :gt
.
If no format is provided, it will be inferred from file
along with compression.
Erdos.writenetwork
— Methodwritenetwork(file, g)
writenetwork(file, g, t; compress=false)
Save a network g
to file
in the format t
.
Eventually the resulting file can be compressed in the gzip format.
Currently supported formats are :gml, :graphml, :gexf, :dot, :net, :gt
. When possible, graph, edge, and vertex properties will be written as well.
If no format is provided, it will be inferred from file
along with compression.