How to find out isolated subgraph?

조회 수: 6 (최근 30일)
Mouli Sarkar
Mouli Sarkar 2021년 8월 17일
댓글: Walter Roberson 2021년 8월 18일
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