How to find out isolated subgraph?

6 vues (au cours des 30 derniers jours)
Mouli Sarkar
Mouli Sarkar le 17 Août 2021
Commenté : Walter Roberson le 18 Août 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)

Réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by