-
Notifications
You must be signed in to change notification settings - Fork 76
/
numConnComp.m
executable file
·35 lines (28 loc) · 934 Bytes
/
numConnComp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
% Calculate the number of connected components using the eigenvalues
% of the Laplacian - counting the number of zeros
%
% INPUTS: adjacency matrix, nxn
% OUTPUTs: positive integer - number of connected components
%
% Other routines used: graphSpectrum.m
% Last updated: September 22, 2012
function nc = numConnComp(adj)
s = graphSpectrum(adj);
nc = numel(find(s < 10^(-5))); % zero eigenvalues are sometimes close to zeros numerically
%!test
%!shared T
%! T = load_test_graphs();
%!assert(numConnComp(T{5}{2}),2)
%!test
%! randint = randi(51);
%! Adj=zeros(randint*30);
%! for x=1:randint
%! adj=randomGraph(30,0.5);
%! Adj(30*(x-1)+1:30*x,30*(x-1)+1:30*x)=adj;
%! end
%! assert(numConnComp(Adj),randint)
%!demo
%! numConnComp([0 0 0; 0 0 0; 0 0 0])
%! numConnComp([0 1 1; 1 0 1; 1 1 0])
%! adj = [0 1 1 0 0 0; 1 0 1 0 0 0; 1 1 0 0 0 0; 0 0 0 0 1 1; 0 0 0 1 0 1; 0 0 0 1 1 0];
%! numConnComp(adj)