function [A, xy, p] = clustereg(nv,csizes,inprob,outprob)
% CLUSTEREG : make sample graph for clustering
% 
% [A, xy, p] = clustereg(nv,nc,inprob,outprob)
%
% nv:      number of vertices
% nc:      number of equal-size clusters, or vector of cluster sizes
% inprob:  probability of a within-cluster edge
% outprob: probability of a between-cluster edge
%
% A  is the Laplacian matrix of the graph.
% xy is vertex coordinates around a circle.
% p  if present, is the inverse of a random permutation
%    that has been applied to A.
%
% John Gilbert, 31 March 2012

if nargin < 4
    outprob = 2/nv;
end;
if nargin < 3
    inprob = 10/nv;
end;
if length(csizes) == 1
    nc = csizes;
    csizes = floor(nv/nc) * ones(1,nc);
    k = mod(nv,nc);
    csizes(1:k) = csizes(1:k) + 1;
else
    assert(sum(csizes)==nv);
    nc = length(csizes);
end;
A = sprandsym(nv,outprob);
cstart = cumsum([1 csizes]);
for k = 1:nc
    I = cstart(k) : (cstart(k+1)-1);
    cs = csizes(k);
    A(I,I) = A(I,I) + sprandsym(cs,inprob - outprob);
end;
A = laplacian(A);
xy = [sin(2*pi*(0:nv-1)'/nv) cos(2*pi*(0:nv-1)'/nv)];
if nargout > 2
    pinv = randperm(nv);
    A = A(pinv,pinv);
    p = ones(1,nv);
    p(pinv) = 1:nv;
end;