5
$\begingroup$

Is there a specific command in Mathematica 9 or 10 to extract the largest component of a given network which has one large and many small components?

The ConnectedComponents or ConnectedGraphComponents commands seem to do different than what I want in the question.

$\endgroup$
10
$\begingroup$

You just need to wrap First around ConnectedComponents because the later sorts components by number of vertexes:

SeedRandom[1];
g = RandomGraph[{200, 130}];
HighlightGraph[g, Subgraph[g, First[ConnectedComponents[g]]]]

enter image description here

$\endgroup$
  • $\begingroup$ For directed graphs ConnectedComponents are not sorted by Length:. e.g. ConnectedComponents[ Graph[{1 -> 2, 2 -> 3, 3 -> 1, 3 -> 4, 4 -> 5, 3 -> 5, 6 -> 7}]] gives {{5}, {4}, {1, 2, 3}, {7}, {6}} . So we do need to sort explicitly, and Last@Sort@ConnectedComponents@g works for both directed and undirected graphs. (+1) $\endgroup$ – kglr May 6 '16 at 19:06
  • $\begingroup$ For directed, use WeaklyConnectedComponents. ConnectedComponents would find strongly connected ones and would not sort by size in this case. $\endgroup$ – Szabolcs May 16 '17 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.