Skip to content
Advertisement

Algorithm for Connected Components of Graph

I am looking for the most efficient algorithm in order to find both the number of connected components in a network, and the number of nodes for each connected component.

Example:

Given the following inputs:

JavaScript

I would receive the following output:

JavaScript

This is what I have so far:

JavaScript

I have found a way to iterate through the nodes assuming that all connections are adjacently placed in the graph_from list, but this, of course, will not hold up for all cases.

EDIT: I am looking for a way to do this without importing a module

Advertisement

Answer

Using the summarized process answered above, I was able to formulate the following which after testing looks to check out.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement