How to find out isolated subgraph?

6 次查看(过去 30 天)
A large graph can has multiple isolated subgraph(which is not connected with the main graph). How can I count total number of isolated subgraph? And how can I find out total number of nodes and edges in each isolated subgraph? I have a directed signed graph.
This is the code I am using:
s = [1 1 1 2 2 2 8 8 8 8];
t = [2 3 4 5 6 7 9 10 11 12];
weight= [1 -1 1 -1 1 -1 -1 1 -1 1];
G = digraph(s,t,weight);
plot(G,'Layout','force','EdgeLabel',G.Edges.Weight)
weak_bins1 = conncomp(G,'Type','weak')
component= max(weak_bins1)

回答(1 个)

Christine Tobler
Christine Tobler 2021-8-17
The component output in your code is the number of components.
You can use the second output of conncomp to get a vector containing the number of nodes in each of the components.
To get the number of edges, you can either use the subgraph command for a specific component and then call numedges on the resulting graph. Or you can use the groupsummary function to sum up the out-degree of all nodes that are part of each component.
>> [weak_bins1, compNumNodes] = conncomp(G,'Type','weak')
weak_bins1 =
1 1 1 1 1 1 1 2 2 2 2 2
compNumNodes =
7 5
>> numedges(subgraph(G, find(weak_bins1 == 1)))
ans =
6
>> numedges(subgraph(G, find(weak_bins1 == 2)))
ans =
4
>> compNumEdges = groupsummary(outdegree(G), weak_bins1', 'sum')
compNumEdges =
6
4
  2 个评论
Mouli Sarkar
Mouli Sarkar 2021-8-18
numedges(subgraph(G, find(weak_bins1 == 1)))
Can this logic be more genelarized?
Walter Roberson
Walter Roberson 2021-8-18
uwb = reshape(unique(weak_bins1), 1, []);
for wbn = uwb
numedges(subgraph(G, find(weak_bins1 == wbn)))
end
but the groupsummary() that Christine shows is calculating the values for you. About the only thing to add might be
[compNumEdges, wbg] = groupsummary(outdegree(G), weak_bins1', 'sum')
[wbg, compNumEdges]
to give an array showing the weak_bin number before the count.

请先登录,再进行评论。

类别

Help CenterFile 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