2

I tried searching a function for matlab that gives the average neighbor degree of a graph.

There is a function for the same in python in network-X package. So i was wondering if there's a similar function in matlab.

***********Edit****************

I cannot convert it to an adjacency matrix.. this will occupy too much of space actually.

What i have is the following edge list(Actually this is just a test matrix.. the actual one is pretty large ) as in there's an edge between node 2 to node 1 and so on.. and yes this is an un-directed graph

2 1
3 1
4 1
5 1
1 2
3 2
4 2
1 3
2 3
5 3
1 4
2 4
5 4
1 5
3 5
4 5

Now, what i need is a function that will compute the average neighbor degree(mean neighbor degree) for this graph.

SeasonalShot
  • 2,357
  • 3
  • 31
  • 49
  • How are you representing the graph? As an adjacency matrix or adjacency list? You have provided very little information here. – rayryeng Sep 07 '14 at 05:46

1 Answers1

3

Even for large edge list, you can use Matlab to create an adjacency matrix that fits into memory using sparse matrix:

el = [2 1; 3 1; ... ]; %// edge list, I put only a tiny sample here...
n = max( el(:) ); %// number of nodes in the graph
A = sparse( el(:,1), el(:,2), 1, n, n ); % //sparse adjacency matrix

The neighbor degree of each node is the number of neighbors

nd = sum( A, 2 ); %// degree of each node

To compute the average neighbor degree, one can construct another sparse matrix with the neighbor degree stored in each entry

ndM = sparse( el(:,1), el(:,2), nd( el(:,2) ), n, n ); 

The average neighbor degree can now be computed from the new matrix

av = full( sum( ndM, 2 ) ./ nd );
Shai
  • 111,146
  • 38
  • 238
  • 371
  • @Shai Actually, i could get the mean degree of the network. What i wanted to find is the mean degree neighbor of the network. Sorry, but it still doesn't answer my query . – SeasonalShot Sep 07 '14 at 06:28
  • @Shai I tried running this in the above matrix , but right after i execute ndM i am getting this error "Error using sparse Index exceeds matrix dimensions.". DO you happen to know the bug? – SeasonalShot Sep 07 '14 at 14:34
  • @Shai The average neighbour degree of the graph in the matrix example i had given it should come around to be 3.23(it should be a 1X1 vector). But, using your code the solution i am getting is a NX1 matrix. ( the mean neighbor degree(mnd) is equal to the 1/n(mnd of vertex 1+ mnd of vertex 2+...mnd of verex n) ) – SeasonalShot Sep 07 '14 at 15:12
  • according to [this doc](https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.assortativity.average_neighbor_degree.html#networkx.algorithms.assortativity.average_neighbor_degree) mnd should be an n-vector and not a scalar. Why do you expect to get a 1x1 scalar? – Shai Sep 07 '14 at 15:28
  • @Shai okay i meant the average of the sum of all such mnd. But, even so, with that 16 x 2 example matrix above, the mnd vector i should be getting is [3;3.33;3.33;3.33;3.33](found this my manual computation, which is correct), however after using your code, the output i am getting is [2.4;2;2;2;2]. – SeasonalShot Sep 07 '14 at 15:48
  • 1
    Wonderful :) It so works! I couldn't figure out ndM = sparse( el(:,1), el(:,2), nd( el(:,2) ), n, n ); and av = full( sum( ndM, 2 ) ./ nd ); Could you point me to the resource? – SeasonalShot Sep 07 '14 at 16:17
  • 2
    @BoyLittUp I suggest that you look at `full(ndM)` and `full(nd)` computed for the small toy example, this should be quite clear. – Shai Sep 07 '14 at 16:19
  • @Shai in the code, ndM = sparse( el(:,1), el(:,2), nd( el(:,2) ), n, n ); I have doubt in the code fragment "nd( el(:,2)" . I understand that this value corresponds to the vector el(:,1) and el(:,2). Could you please point out what exactly does nd( el(:,2) does? – SeasonalShot Sep 12 '14 at 21:01
  • 1
    @BoyLittUp `nd( el(:,2) )` creates a vector with the degree (`nd`) of all the neighboring nodes (stored in `el(:,2)`). – Shai Sep 14 '14 at 05:15