我有两个类型的节点的大小(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'继续?
答案 0 :(得分:1)
您可以从邻接矩阵的nNodeCol
个副本(对应于O1 O2 ...)和虚拟元素创建块对角矩阵,因此新图形将具有nNodeCol*(nNodeCol+nNodeLine)+1
个节点。然后,您可以将第一个块的第一个元素,第二个块的第二个元素...连接到末尾虚拟节点。
从end元素开始搜索可以找到从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
是与每个子图相关的类别向量。