在MATLAB中生成随机加权邻接矩阵

Roy*_*yeh 2 random matlab sparse-matrix adjacency-matrix

我想在MATLAB中创建一个随机邻接矩阵,使得权重的总和等于边数.最后找到拉普拉斯矩阵

L = diag(sum(A)) - A
Run Code Online (Sandbox Code Playgroud)

然后绘制图形.有没有办法这样做?提前致谢.

Sha*_*hai 6

无向图的邻接矩阵简单地是方形对称矩阵.
如果你对权重的节点程度没有约束,那么就像我建议的那样

n ; % number of nodes in the graph
density = 1e-3; % a rough estimate of the amount of edges       
A = sprand( n, n, density ); % generate adjacency matrix at random
% normalize weights to sum to num of edges
A = tril( A, -1 );    
A = spfun( @(x) x./nnz(A), A );    
% make it symmetric (for undirected graph)
A = A + A.';
Run Code Online (Sandbox Code Playgroud)

我在这段代码中使用过:

  • sprand 生成随机稀疏矩阵.
  • spfun 帮助规范边缘权重.
  • tril 只提取一半的矩阵.