採用された回答

Michael Croucher
Michael Croucher 2020 年 9 月 28 日
編集済み: Michael Croucher 2020 年 9 月 28 日

0 投票

One way to proceed would be to use concomp.
Take this example graph
s = [1 2 2 3 3 3 4 5 5 5 8 8];
t = [2 3 4 1 4 5 5 3 6 7 9 10];
G = graph(s,t);
plot(G,'Layout','layered')
>> components=conncomp(G)
ans =
1 1 1 1 1 1 1 2 2 2
We can see that there are two connected components. To see it nodes 4 and 7 are connected (for example), just see if they are members of the same component.
components(4)==components(7)
ans =
logical
1

2 件のコメント

Hari
Hari 2020 年 10 月 4 日
This did work for me. Thanks
Christine Tobler
Christine Tobler 2020 年 10 月 5 日
Note this works well for an undirected graph, but for directed graphs it would be more complicated: In that case, you'd want to look into digraph/transclosure to get connection between every pair of nodes.

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

その他の回答 (1 件)

Christine Tobler
Christine Tobler 2020 年 9 月 28 日

2 投票

Compute a path between the nodes, then check if the result is empty (this is returned by shortestpath if no path exists):
path = shortestpath(G, firstNode, secondNode)
pathExists = ~isempty(path);

2 件のコメント

Hari
Hari 2020 年 10 月 4 日
Will this be computationally intensive if I need to check paths between every node pair (say there 100 nodes)?
Bruno Luong
Bruno Luong 2020 年 10 月 4 日
Read Michael's answer that suggests using concomp

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

カテゴリ

ヘルプ センター および File Exchange で Graph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by