2

I want to calculate L = laplacian(G) from a graph dataset. I imported the dataset which contains two columns: FromNodeId and ToNodeId:

# Nodes: 3997962 Edges: 34681189
# FromNodeId    ToNodeId
0   1
0   2
0   31
0   73
0   80
0   113619
0   2468556
0   2823829
0   2823833
0   2846857
0   2947898
0   3011654
0   3701688
0   3849377
0   4036524
0   4036525
0   4036527
0   4036529
0   4036531
0   4036533
0   4036534
0   4036536
0   4036537
1   2
1   3
1   4
1   5
1   6
1   7
1   8
1   9
1   10
1   11

To do so, I need to find G first so I use G = graph(FromNodeId, FromNodeId). When I did that, I got this error:

>> G = graph(fromNodeId,toNodeId)
Error using matlab.internal.graph.MLGraph
Source must be a dense double array of node indices.

Error in matlab.internal.graph.constructFromEdgeList (line 125)
G = underlyingCtor(double(s), double(t), totalNodes);

Error in graph (line 264)
                matlab.internal.graph.constructFromEdgeList(...

I don't know why! Can I get a solution of that? Thank you.

Kristofer
  • 1,457
  • 2
  • 19
  • 27
  • 1
    Are both fromNodeID and toNodeID full double arrays? Try `FromNodeID=full(double(FromNodeID)); ToNodeID=full(double(ToNodeID));`. "Source must be a dense double array of node indices." sounds to me like you might be inputting either a sparse array or a single type. – Poelie Apr 01 '17 at 16:20
  • 1
    The problem is with the way the data is logged. The arrays are sparse, I've checked. – NKN Apr 01 '17 at 16:22
  • Possible duplicate of [Target must be a dense double array of node indices. How to Solve?](http://stackoverflow.com/questions/35075759/target-must-be-a-dense-double-array-of-node-indices-how-to-solve) – Poelie Apr 02 '17 at 17:29

1 Answers1

1

Turns out the problem lies in the fact that no zeros are allowed when using the Graph function in this manner. (See: Target must be a dense double array of node indices. How to Solve?)

I downloaded the dataset and ran it successfully with the following code. Note that this code uses a system command and is not compatible with all operating systems, but it should be simple enough to rewrite to whatever operating system you use. It also assumes the .txt file to be in the working directory.

% Removes first lines with comments in them; this system command was tested on Linux Ubuntu 14.04 and is probably not portable to Windows. 
% If this system command doesn't work, manually remove the first four lines from the text file.  
system('tail -n +5 com-lj.ungraph.txt > delimitedFile.txt'); 
% Read the newly created delimited file and add 1 to all nodes. 
edges=dlmread('delimitedFile.txt')+1;
% Build the graph
G=graph(edges(:,1),edges(:,2));

Assuming you've build your arrays similarly to how I did it, adding 1 to FromNodeIdFull and ToNodeIdFull should resolve your problem. In other words, the following code snippet should solve your problem; if it doesn't I advise you to rewrite based on the code presented above.

G=graph(FromNodeIdFull+1,ToNodeIdFull+1);

Leaving my old answer here, as deleting it may cause confusion for others reading both this answer and the comments to it. Note that the answer below did NOT resolve the issue.

Just putting the comments by myself and NKN into an answer:

The problem lies in the fact that the arrays are sparse but graph() seems to expect full arrays. The following should work:

FromNodeIdFull=full(double(FromNodeId));
ToNodeIdFull=full(double(ToNodeId));
G=graph(FromNodeIdFull,ToNodeIdFull);

Depending on whether your input arrays are already doubles or not you may be able to remove the double() from the first two lines.

Community
  • 1
  • 1
Poelie
  • 711
  • 6
  • 18
  • Thank you very much for your response. I used your solution but I still get the same error!! I don't know what is wrong with this dataset!! It is making me desperate – Kristofer Apr 01 '17 at 21:53
  • Please don't accept answers that did not resolve your problem; this may cause others to overlook your question. – Poelie Apr 02 '17 at 17:19