0001 function p = components(g)
0002 % components(g) --- find the components of the graph g
0003 % If g has n vertices, this returns a partition of the [n] based on the
0004 % components of g.
0005
0006 n = nv(g);
0007 indicator = zeros(n,1);
0008 c = 0;
0009
0010 while (nnz(indicator)<n)
0011 c = c+1;
0012 % find first zero entry in indicator
0013 i = find(indicator==0);
0014 i = i(1);
0015
0016 % get i's component
0017 ci = component(g,i);
0018
0019 indicator(ci) = c;
0020 end
0021 %p = indicator;
0022 p = ind2part(indicator);
0023
0024
0025
0026 function p = ind2part(ind)
0027 % convert indicator vector to a partition
0028
0029 np = max(ind);
0030 A = cell(np,1);
0031 for k=1:np
0032 A{k} = find(ind == k);
0033 end
0034 p = partition(A);