
figure(1);
clf;
colormap([1 0 0; 0 0 1]);  %just blue and red

%generate two random clusters of data
x1 = 15;  y1 = 20;  x2= 28; y2 =30;
N1 = 50;  N2 = 50; N = N1+N2;
A1 = 2*pi*rand(1,N1);  A2 =2*pi*rand(1,N2);
R1 = 10*sqrt(rand(1,N1));  R2 =10*sqrt(rand(1,N2));
x = [x1+R1.*cos(A1)  x2+R2.*cos(A2)];
y = [y1+R1.*sin(A1)  y2+R2.*sin(A2)];
subplot(3,1,1);
plot(x,y,'o');  axis([0 50 0 50]);
title('data points');


%adjacency matrix and plot graph 
td = 4.5;  %threshold distance
X = x'*ones(1,N) -  ones(N,1)*x;
Y = y'*ones(1,N) -  ones(N,1)*y;
A = (X.^2 + Y.^2 < td^2) - eye(N);
subplot(3,1,2);  plot(x,y,'o');  axis([0 50 0 50]);
for i=1:N, for j = i:N,
    if (A(i,j)~=0)  line([x(i) x(j)],[y(i) y(j)]); end;
end; end;
title('proximity graph');


%find graph laplacian and principal eigenvector
deg = sum(A);  %column vector of vertex degrees
L = A - diag(deg); 
[V,D] = eig(L);  %complete spectrum; N-1 th column has clustering eigenvector
ev = V(:,N-1);
subplot(3,1,3);
scatter(x,y,30,ev);
axis([0 50 0 50]);
title('second eigenvector components (blue positive, red negative)');






