1
$\begingroup$

Problem:

I have a large undirected graph in the .ncol format, and want to get it in to Mathematica.

I understand that Mathematica doesn't natively support the import of .ncol format, but .ncol format is very simple...

node1 node2 0.1
node2 node3 0.2
node1 node5 0.3
node3 node5 0.9
node2 node5 0.5

For example, this .ncol file produces the following undirected graph...

enter image description here

Question: What's the best way to import a large, complete, undirected graph of 4268 nodes, and 9105778 edges in .ncol format? Is it possible to import this type of file even though it is not natively supported? Reading in line by line?

What I've Tried So Far

Thank you vikram reddy and user6014 for your suggestions.

I have implemented ReadList as such:

list = ReadList["/path/to/file.ncol", Word, RecordLists -> True, 
  WordSeparators -> {" "}]

Now, list is a nested list of the lines like:

{{"0000004", "0000002", "0.8516145163710000"}, {"0000007", "0000006", 
  "0.9169866102280000"}, {"0000011", "0000000", "0.8639846171470000"}...}

How do I add these elements to a graph object?

The .ncol File (snippet):

Here's a snippet of the .ncol graph I am actually trying to import:

0000001 0000000 0.4408587960080000
0000002 0000000 0.7405196230980000
0000002 0000001 0.5211728712080000
0000003 0000000 0.7376683700760000
0000003 0000001 0.4166055229790000
0000003 0000002 0.6032695565790000
0000004 0000000 0.7225082142750000
0000004 0000001 0.5197121010690000
0000004 0000002 0.8516145163710000
0000004 0000003 0.6050347164500000
0000005 0000000 0.3433514873340000
0000005 0000001 0.4807050687280000
0000005 0000002 0.3554792851080000
0000005 0000003 0.3306190830880000
0000005 0000004 0.3536941923200000
0000006 0000000 0.4033221771690000
0000006 0000001 0.6300091271690000
0000006 0000002 0.4136969934920000
0000006 0000003 0.4107973190560000
0000006 0000004 0.4259157902110000

Thank you!

$\endgroup$
  • 2
    $\begingroup$ I do not have your dataset. But why don't you try this? This is just to import the file.ReadList[inputfilename, Word, RecordLists -> True, WordSeparators -> {" "}] $\endgroup$ – no-one Jul 19 '16 at 14:22
  • $\begingroup$ Try what exactly? $\endgroup$ – jackskis Jul 19 '16 at 14:22
  • 1
    $\begingroup$ Use the code he mentioned, ReadList[yourFileName, Word, RecordLists -> True, WordSeparators -> {" "}], where your file name is the .ncol file. It should bring the data in as a list of strings, to which you can easily then likely modify it to meet your needs. $\endgroup$ – user6014 Jul 19 '16 at 15:13
  • $\begingroup$ OK. It works well, up to that point, but now I need to put all the values in a graph object. How do I do this? $\endgroup$ – jackskis Jul 19 '16 at 15:22
2
$\begingroup$

Rough mock up of how you could handle this, using Vikram's suggestion to import the data:

In[4]:= lis=ReadList["yourfileName.ncol", Word, RecordLists->True, WordSeparators->{" "}]

(*Out[4]= {{"0000001", "0000000", "0.4408587960080000"}, {"0000002", "0000000", "0.7405196230980000"}, {"0000002", "0000001", "0.5211728712080000"}, {"0000003", "0000000",   "0.7376683700760000"}, {"0000003", "0000001", "0.4166055229790000"}, {"0000003", "0000002",   "0.6032695565790000"}, {"0000004", "0000000", "0.7225082142750000"}, {"0000004", "0000001",   "0.5197121010690000"}, {"0000004", "0000002", "0.8516145163710000"}, {"0000004", "0000003",   "0.6050347164500000"}, {"0000005", "0000000", "0.3433514873340000"}, {"0000005", "0000001",   "0.4807050687280000"}, {"0000005", "0000002", "0.3554792851080000"}, {"0000005", "0000003",   "0.3306190830880000"}, {"0000005", "0000004", "0.3536941923200000"}, {"0000006", "0000000",   "0.4033221771690000"}, {"0000006", "0000001", "0.6300091271690000"}, {"0000006", "0000002",   "0.4136969934920000"}, {"0000006", "0000003", "0.4107973190560000"}, {"0000006", "0000004", "0.4259157902110000"}})*

Convert string to expressions:

exprlis = ToExpression /@ lis;

Your graph will likely take the form Graph[edges, EdgeWeight->weights]. To achieve this:

{edges, weights} = exprlis/. {a_, b_, c_}:>{a\[UndirectedEdge]b,c}//Transpose;

And then you can just input edges and weights into a Graph object:

Graph[edges, EdgeWeight -> weights]

completed graph

$\endgroup$
  • 2
    $\begingroup$ +1 - I made a couple of changes, hope you don't mind. One just to change "vertices" to "edges", and another to reflect that the OP was asking about an undirected graph. $\endgroup$ – Jason B. Jul 19 '16 at 18:15
  • $\begingroup$ Ah, I missed the undirected part. Appreciate the corrections! $\endgroup$ – user6014 Jul 19 '16 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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