I have a graph data file infile.txt
.
3 2 0 1 1 2
3 2 0 1 2 1
3 2 1 0 1 2
3 3 0 1 1 0 1 2
3 3 0 1 1 0 2 1
3 4 0 1 1 0 1 2 2 1
In this file, each row corresponds to a digraph in the following way:
- The first number is the number of vertices in this digraph;
- The second number is the number of arcs in this digraph;
- Remaining numbers represent arc in pairs.
For example, the last row in the given file corresponds to a digraph with 3 vertices and 4 arcs, and arcs are $(0,1)$,$(1,0)$,$(1,2)$,$(2,1)$.
I want to import it into Mathematica and construct a list of graphs accordingly. And my solution is:
mylist=Import["/path/to/infile.txt","Table"];
mylist=Drop[#,2]&/@mylist;
glist=Graph[DirectedEdge@@@Partition[#,2]]&/@mylist
My method is OK with small files. However, it is extremely slow when the input file is large. But I need to deal with large files a lot. So my questions are
How to input such large files efficiently?ReadList
seems to be good option. But how to read the file row by row?What is best importing format for constructing a list of graphs, number, string or something else?
Are there other ways to construct the list of graph efficiently from a list given above?
Update: Following are the running times for a file with 531441 rows.
AbsoluteTiming[mylist=Import["infile.txt","Table"];]
{20.3156, Null}
And the method suggested by @RunnyKine gives
AbsoluteTiming[mylist=ReadList["infile.txt", Number, RecordLists -> True];]
{4.45186, Null}
That's really a big improvement.
The input file (around 35MB) is available from this link.