1
我有一個圖,我想找到所有從節點「01」訪問的節點,所以我使用以下命令:如何突出顯示有向圖的所有路徑?
B=[1 1 1 0 ; 0 1 1 0; 0 1 0 1; 0 1 0 1; 0 0 0 0 ; 0 0 1 0; 1 1 1 0; 0 1 0 0];
nNodeCol = size(B,2); % one node for each column of B
nNodeLine = size(B,1)/2; % one node for every two lines of B
% First the column nodes, then the line nodes:
nodeNames = [cellstr(strcat('O',num2str((1:size(B,2))'))) ; cellstr(strcat('S',num2str((1:size(B,1)/2)')))];
% 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 column node to a line 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 line node to a column node is added for all the 1 in the second line of the node in the matrix
% Creation of the graph:
G = digraph(adj,nodeNames);
h = plot(G);
v = dfsearch(G,'O1');
highlight(h,v,'nodeColor','r');
highlight(h,v,'edgecolor','r');
單元陣列v
包含以下內容:
'O1'
'S1'
'O2'
'S2'
'O4'
'S4'
'O3'
正如您所看到的,節點高光是正確的。但是,某些邊緣未以紅色顯示,如連接O1-S4
和S1-O3
。我怎樣才能顯示所有的邊緣?
感謝您的快速答覆 – StamDad