フィルターのクリア

Paths search in a graph

2 ビュー (過去 30 日間)
stam dadi
stam dadi 2017 年 6 月 24 日
編集済み: stam dadi 2017 年 6 月 26 日
I have the following code to generate a graph from a matrix :
B=randi([0 1], i*2,j);
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);
plot(G);
I want to know how can I search for a path between two nodes in G and show it ? and also how can i search for all the paths having a node n as a source ? thank you.

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 6 月 24 日
  1 件のコメント
stam dadi
stam dadi 2017 年 6 月 26 日
編集済み: stam dadi 2017 年 6 月 26 日
I found that , but the problem is when I use it get the paths using a character vector containing the nodes names as an input like this :
v=shortestpath(G,'O1','S2');
I get the following error :
Error using digraph/validateNodeID (line 503)
Graph does not contain a node named 'O1'.
Error in digraph/shortestpath (line 57)
src = validateNodeID(G, s);
Error in PGM (line 25)
v=shortestpath(G,'O1','S2');

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by