Is it possible to import graphs generated by geng (a tool from the nauty suite) one by one, rather than all at once. If one could also specify not only the order but also the number of edges that would be great, but the main thing is to be able to get one at a time rather than dump them all in memory at once. Thanks


2 Answers 2


Here is a simple way to get part of the graphs using the format "GraphList".

Import["~/MMA/geng/simplegraphs/graph5.g6", {"GraphList", Range[10,15]}]


simple graphs from 10 to 15


I should point out that import by "GraphList" is much slower than that by direct method. In fact, you can get part of the graphs by splitting the file itself since the .g6 file is just the same as .txt(might be more efficient if the file is large).


To see geng's options, just rung geng -h on the command line.

You can use something like this to e.g. generate connected graphs (-c) on 5 vertices (5) with 6 to 7 edges (6:7):

geng = StartProcess[{"/opt/local/bin/geng", "-c", "5" , "6:7"}]

This does generate all graphs without stopping. But we can read them into Mathematica one by one instead of reading them all at once. Example:

While[(line = ReadLine[geng]) =!= EndOfFile,
  Print@ImportString[line, "Graph6"]

Simply use ReadLine to read the output line-by-line (i.e. graph-by-graph) until it returns EndOfFile (i.e. there is no more data).

  • $\begingroup$ Thanks, this helps somewhat ... However, the output of geng is still completely dumped into an output file, right? I guess there is no way of avoiding this? The problem is that even for small graphs, the output of such calls to geng generate an enormous amount of graphs ... $\endgroup$
    – EGME
    Dec 12, 2019 at 21:50
  • $\begingroup$ Yes, the output of geng is still stored somewhere. I do not know where (memory, disk, a combination of the two, etc.) On the upside: the output of geng is an extremely efficient encoding of the graphs. It will take up much less space than what Mathematica's Graph expressions would. So this should still be a considerable saving. $\endgroup$
    – Szabolcs
    Dec 12, 2019 at 22:01
  • $\begingroup$ It appears that geng cannot be installed on Windows machines? $\endgroup$
    – EGME
    Dec 14, 2019 at 14:20
  • $\begingroup$ @EGME It should be possible to install it on Windows, but the author does not provide binaries. That means you need to compile it yourself. Unfortunately, on Windows that tends to be much less convenient than on Unix-like systems. I don't use Windows much, so I can't really offer advice here, except that if I had to do it, I would try msys2 first, which provides a Unix-style environment in which the original compilation instructions should work. $\endgroup$
    – Szabolcs
    Dec 14, 2019 at 21:10
  • $\begingroup$ Thanks, I will try that ... I take it the source to compile is also on the website you linked? $\endgroup$
    – EGME
    Dec 14, 2019 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.