gpt4 book ai didi

matlab - 区分从不同节点开始的路径

转载 作者:行者123 更新时间:2023-12-02 07:02:30 28 4
gpt4 key购买 nike

我有以下图表 pf size (i,j) ,其中包含两种类型的节点: i 是类型 S 的节点数量,j 是类型 O 的节点数量

  i=input('i:');
j=input('j');

B=randi([0 1], i*2,j);
nNodeCol = size(B,2); % nodes of type O
nNodeLine = size(B,1)/2; % nodes of type S
% First the 'O' nodes, then the 'S' nodes:
nodeNames = [cellstr(strcat('O',num2str((1:size(B,2))'))) ; cellstr(strcat('S',num2str((1:size(B,1)/2)')))];
nodeNames{end+1} = 'X';
% Adjacency matrix adj, adj(i,j)=1 means there is an edge from node#i to node#j:
adj = zeros(nNodeCol+nNodeLine); % square matrix which size is the number of nodes
adj(1:nNodeCol, nNodeCol+1:end) = B(1:2:end,:)'; % edge from a 'O'node to 'S' node is added for all the 1 in the first line of the node in the matrix
adj(nNodeCol+1:end, 1:nNodeCol) = B(2:2:end,:); % edge from the 'S' node to 'O' node is added for all the 1 in the second line of the node in the matrix
adj(end+1,end+1) = 0; % add 1 row and 1 column to adj
adj(end, 1:nNodeCol) = 1; % only outgoing edges from X to O*
% Creation of the graph:
G = digraph(adj,nodeNames);
v = dfsearch(G,'X');

现在,这段代码将允许我同时从所有类型为“O”的节点开始获取dfsearch结果。我的问题如下:有什么方法可以区分结果,我的意思是区分“O1”的结果和“O2”的结果,然后继续?

最佳答案

您可以从邻接矩阵的 nNodeCol 副本(对应于 O1 O2 ...)和虚拟元素创建 block 对角矩阵,因此新图将具有 nNodeCol*( nNodeCol+nNodeLine)+1 节点。然后您可以将第一个 block 的第一个元素、第二个 block 的第二个元素...连接到末尾、虚拟节点。从末尾元素开始搜索,可以找到从O元素开始的所有子图。

n =nNodeCol+nNodeLine;

adj = zeros(n); %same as your code
adj(1:nNodeCol, nNodeCol+1:end) = B(1:2:end,:)'; %same as your code
adj(nNodeCol+1:end, 1:nNodeCol) = B(2:2:end,:); %same as your code

adj2= blkdiag(kron(eye(nNodeCol),adj),0); % block diagonal matrix of tha adjacency matrix plus a dummy element added to the end
adj2(end, 1:n+1:nNodeCol*n) = 1; % conncet the dummy element to O1, O2..
G = digraph(adj2);
v = dfsearch(G,nNodeCol*n+1); % start the seach from the end node
v = v(2:end); % exclude the dummy node from the result
categ= cumsum(ismember(v,1:n+1:nNodeCol*n)); % create categories so each subgraph can be distiguished
node_num = mod(v,n); % rescale node numbers to the original ones

categ 是与每个子图相关的类别向量。

关于matlab - 区分从不同节点开始的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45084208/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com