matlab将邻接矩阵转换为邻接列表

时间:2016-08-19 09:03:27

标签: algorithm matlab matrix

我有一个大的稀疏邻接矩阵,其中包含 10M节点,我正在使用MATLAB进行处理。我想尽可能有效地将矩阵转换为邻接列表。作为一个示例邻接矩阵来说明这一点:

adj =
     1     0     1
     0     0     1
     0     1     1

输出是:

ans =
     0     0     2
     1     2
     2     1     2

我想尽可能高效地完成它,有没有有效的方法呢?

1 个答案:

答案 0 :(得分:0)

结果需要是向量的单元数组,因为连接到每个节点的节点数会有所不同。这是一种方法:

[ii, jj] = find(adj); % row and col indices of connections
y = accumarray(ii, jj-1 , [], @(x){sort(x.')}); % get all nodes connected to each node,
    % sorted. Subtract 1 for 0-based indexing

这给出了

>> celldisp(y)
y{1} =
     0     2
y{2} =
     2
y{3} =
     1     2
相关问题