如何在一个路径中合并多路径以创建图形

时间:2017-04-17 13:31:37

标签: matlab networking

我有这个多路径:

[4876,12282,19222]
[4876,12282,19240]
[4876,12282,19254]
[4876,12282,19681]
[4876,12282,20222]
[4876,12282,20347]
[4876,12282,2401,159]
[4876,12282,2401,14174]
[4876,12282,2401,18407]
[4876,12282,14691,7865]
[4876,12282,7318,10314]
[4876,12282,17452,12152]
[4876,12282,14917,794]
[4876,12282,2401,9463]
[4876,12282,2401,18576]
[4876,12282,14691,7865,9496]
[4876,12282,14691,3722]
[4876,12282,14691,4216]

我想将它们合并在一条路径中,并保持边缘事件顺序以创建图形。我是图形和网络算法领域的新手。任何帮助将受到高度赞赏

1 个答案:

答案 0 :(得分:0)

我使用sparse创建了一个邻接矩阵:

mp{1} = [4876,12282,19222];
mp{2} = [4876,12282,19240];
mp{3} = [4876,12282,19254];
mp{4} = [4876,12282,19681];
mp{5} = [4876,12282,20222];
mp{6} = [4876,12282,20347];
mp{7} = [4876,12282,2401,159];
mp{8} = [4876,12282,2401,14174];
mp{9} = [4876,12282,2401,18407];
mp{10} = [4876,12282,14691,7865];
mp{11} = [4876,12282,7318,10314];
mp{12} = [4876,12282,17452,12152];
mp{13} = [4876,12282,14917,794];
mp{14} = [4876,12282,2401,9463];
mp{15} = [4876,12282,2401,18576];
mp{16} = [4876,12282,14691,7865,9496];
mp{17} = [4876,12282,14691,3722];
mp{18} = [4876,12282,14691,4216];
% get all connections
idx1 = cell2mat(cellfun(@(x) x(1:end-1),mp,'UniformOutput',0));
idx2 = cell2mat(cellfun(@(x) x(2:end),mp,'UniformOutput',0));
% remove duplicates
idxs = unique([idx1(:),idx2(:)],'rows');
% get max element index
maxIdx = max(idxs(:));
% create adjacency matrix and undirected graph
A = sparse( idxs(:,1),idxs(:,2),ones(size(idxs,1),1),maxIdx,maxIdx );
G = graph(A | A');