两点之间的路径

时间:2014-12-05 16:21:05

标签: matlab adjacency-matrix

我希望能够从以下代码构建任意两个节点之间的所有可能路径:

    function [ out ] = one_step( network )
%FINDPATHS This function calculates all the 1-step connections among the
%node

% It works by figuring out which nodes a particular node is connected to
% from the adjacency matrix provided
rows = size(network,1); % number of rows, i.e. number of nodes
path = []; % this matrix contains the nodes that are connected to at least another node
nodes = []; % this matrix contains the next hop nodes
connections = []; %this is the FINAL matrix, listing all the connections

for i = 1:rows % for every row...
    no_of_connections = numel(find(network(i,:))); % ...find number of possible nodes.
    for j = 1:no_of_connections % write the node as many times as the connections it can connect to
        path = [path; i];
    end
end

path = [path, zeros(size(path,1),1)]; % add second empty column
x = unique(path(:,1))'; % eliminate double elements from path

for i = x % for every node...
    subPath = path(path(:,1) == i,:); % ...extract correspondent section from 'path'... 
    nodes = find(network(i,:)); % ...identify all node pairs...
    subPath(:,2) = nodes'; %...update 'subPath' by transposing 'nodes'...
    connections = [connections; subPath]; % ...and update the FINAL matrix
end
out = connections;
end

结果是一个2列矩阵,其中第一列是输入,第二列是输出。 变量“网络”是映射相关网络的邻接矩阵。试试以下,我正在研究的那个:

n1 = [0 1 1 0 0 0 0;
0 0 0 1 0 1 0;
0 0 0 1 0 0 1;
0 0 0 0 1 0 0;
0 0 0 0 0 1 1;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0]; % adjacency matrix

请帮帮我。已经好几个月了,我无法理解它。谢谢

0 个答案:

没有答案