function c = comps(a)
% function c = comps(a)
%
% given adjacency matrix of a graph a,
% make a vector c giving a number of each component

n = length(a);

c = zeros(n,1);

k = 1;

q = zeros(n,1);
qptr = 1;
qlen = 0;

for i = 1:n,
    if (c(i) == 0),
        
        %keyboard
        
        c(i) = k;
        
        qlen = qlen + 1;
        q(qlen) = i;
        while (qptr <= qlen)
           j = q(qptr);
           
           nbrs = find(a(:,j));
           for nbr = nbrs';
               if (c(nbr) == 0),
                   qlen = qlen + 1;
                   q(qlen) = nbr;
                   c(nbr) = k;
               end
           end
           qptr = qptr + 1;
        end
          
        k = k + 1;
    end
end

