我有一个大型稀疏邻接矩阵,其中包含大约 1000 万个节点,我正在使用 MATLAB 对其进行处理。我想尽可能高效地将矩阵转换为邻接表。作为示例邻接矩阵来说明这一点:
adj =
1 0 1
0 0 1
0 1 1
输出是:
ans =
0 0 2
1 2
2 1 2
我想尽可能高效地做到这一点,有什么有效的方法吗?
最佳答案
结果需要是向量元胞数组,因为连接到每个节点的节点数各不相同。这是一种方法:
[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
关于algorithm - matlab将邻接矩阵转换为邻接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39035074/