即使对于大型边列表,您也可以使用 Matlab 创建适合内存的邻接矩阵sparse http://www.mathworks.com/help/matlab/ref/sparse.html矩阵:
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
每个节点的邻居度是邻居的数量
nd = sum( A, 2 ); %// degree of each node
为了计算平均邻近度,可以构造另一个稀疏矩阵,并将邻近度存储在每个条目中
ndM = sparse( el(:,1), el(:,2), nd( el(:,2) ), n, n );
现在可以根据新矩阵计算平均邻居度
av = full( sum( ndM, 2 ) ./ nd );