Connected graph given adjacency matrix
77 views (last 30 days)
imperial1991 on 29 May 2012
Hi all, I'm working on a research project on graphical models involving a large dimension (large number of nodes). I'm just wondering, is there an existing efficient algorithm to determine whether the graph is connected or not given its adjacency matrix? I've tried looking but it seems that the existing ones are brute force algorithms which are costly. Does MATLAB have a built-in function? When I checked, it seems that none exists as of now.
Help would be greatly appreciated!
Wolfgang Schwanghart on 29 May 2012
You can use the function dmperm to see if a graph consists of one or several connected components. E.g. see the example here http://blogs.mathworks.com/steve/2007/03/20/connected-component-labeling-part-3/
More Answers (3)
Christine Tobler on 22 Dec 2016
Edited: Christine Tobler on 22 Nov 2019
I realize this is an old question, but since it's still getting visits, I have a small addition. As of R2015b, the new graph and digraph classes have a method for computing connected components. To check whether a graph is connected based on its adjacency matrix A, use
g = digraph(A);
bins = conncomp(g, 'Type', 'weak');
isConnected = all(bins == 1);
The vector bins gives the bin number for each node of A. If a graph is connected, all nodes will be in one bin, which is checked using all(bins == 1). This is not necessarily faster than dmperm, but easier to read.
Hon Wah Yeung on 31 Jan 2021
This will work if you can at least load the matrix (meaning the matrix is not larger than the max possible array for Matlab) to Matlab.
function [idx,Group] = CheckConnected(E)