Search code examples
rgraphigraph

R count number of subgraphs/components in a graph


Does anyone know any R packages or methods to count the number of subgraph in a graph? I know igraph can handle some measurements of the graph, but I don't find related functions to count the number of subgraphs in a graph. For example, the number of subgraphs in the below graph should be 4.

enter image description here

Many thanks!


Solution

  • Most directly, you can use igraph::count_components:

    count_components(ig)
    # [1] 4
    

    (Using Maurits's nicely shared sample data)

    Component is a better term than subgraph for what you're looking for - a subgraph can be any subset of a graph, connected or not. Per wikipedia:

    a component of an undirected graph is a connected subgraph that is not part of any larger connected subgraph. The components of any graph partition its vertices into disjoint sets, and are the induced subgraphs of those sets.