5
$\begingroup$

If I have a Graph, how can I count how many disconnected pieces it has? Mathematica must know this since it is able to separate them out when it displays the graph, but I can't find any function which gives the number.

$\endgroup$
1
$\begingroup$

The usual term is connected components.

You can use Length@WeaklyConnectedComponents[graph]. This will work on directed graphs as well, assuming that you are looking for the components of the underlying undirected graph.

IGraph/M also has a fast function for measuring the sizes of connected components without actually returning them: IGWeaklyConnectedComponentSizes.

$\endgroup$
6
$\begingroup$

Shortly after posting this I found that I can use

Length@ConnectedGraphComponents

to get the desired number.

$\endgroup$

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.