File Exchange

image thumbnail

Find Network Components

version (4.14 KB) by Daniel Larremore
Find network components, sizes, and lists of member nodes.


Updated 24 Apr 2014

View License

Given an undirected network represented by an adjacency matrix, we may wish to find that network's
- largest component
- number of components
- list of which nodes are in which component together.
This code computes these rapidly (linear in the number of nodes) and accurately. The code is commented so that if you wish to modify it, you may do so.

1. Network adjacency matrix.

1. Number of network components
2. Sizes of network components (sorted, descending)
3. Membership list of each component (sorted by component size, descending).

Code includes a couple examples of usage at the top. I have tested this in R2014a, but it uses nothing fancy, and I believe it to be backward compatible for many years.

Cite As

Daniel Larremore (2020). Find Network Components (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

Hi, can we implement using adjacency list or sparse matrix as input ? As i am getting some errors while doing this.. Thanks!

Hong Cai


Hi, I have been using this package for months now and it is very useful to implement. However, I am writing a paper but I cannot tell what kind of algorithms you are using? BFS or DFS or others? Also, how exactly can I cite this package? Thanks!

good work


Great work!

Hi, you might also want to take a look at Tim Davis' function find_components:
find_components uses the builtin function dmperm to find connected components.

Iro, suggestion appreciated. The code removes diagonal elements already, but I have edited the example anyway.


Hi, it seems to work for a given adjacency matrix, but you might want to edit the example of creating a random one because it is possible that it generates ones in the diagonal.




Fixed typos (clarity), removed possibility of self-edges in the example (clarity), changed instances of "cluster" to "component" in comments and variable names (clarity and correctness), and sorted outputs by component sizes (ease of use).

Fixed typo in description.

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux