How to find out isolated subgraph?

12 views (last 30 days)
Mouli Sarkar
Mouli Sarkar on 17 Aug 2021
Commented: Walter Roberson on 18 Aug 2021
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)

Answers (1)

Christine Tobler
Christine Tobler on 17 Aug 2021
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 Comments
Mouli Sarkar
Mouli Sarkar on 18 Aug 2021
numedges(subgraph(G, find(weak_bins1 == 1)))
Can this logic be more genelarized?
Walter Roberson
Walter Roberson on 18 Aug 2021
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.

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!