2017-07-12 61 views
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' 

但我的問題是當我繪製結果時,並非顯示所有邊緣: enter image description here

正如您所看到的,節點高光是正確的。但是,某些邊緣未以紅色顯示,如連接O1-S4S1-O3。我怎樣才能顯示所有的邊緣?

回答

2

我能得到缺少的邊緣使用'edgetonew'選項dfsearch和修改來電顯示多達highlight如下:

v = dfsearch(G, 'O1', 'edgetonew'); 
h = plot(G); 
highlight(h, v(:, 1), v(:, 2), 'nodeColor', 'r', 'edgeColor', 'r'); 

enter image description here

注意,從O1直接路徑到S4沒有突出顯示,但通過S1O2的間接路徑是。如果你寧願突出的直接路徑,可以使用bfsearch的廣度優先,而非深度優先搜索:

v = bfsearch(G, 'O1', 'edgetonew'); 
h = plot(G); 
highlight(h, v(:, 1), v(:, 2), 'nodeColor', 'r', 'edgeColor', 'r'); 

enter image description here

+0

感謝您的快速答覆 – StamDad

相關問題