function A=buildlattice(N,K)
%Builds ring lattice with N nodes. K is the number of neighbours TO EACH SIDE (i.e. half the average degree).
%K must be less than (N-1)/2
%Usage: A=buildlattice(N,K)
A=sparse([],[],[],N,N,2*(N*K));
B=repmat(1,N,2*K);
d=[[-(N-1):-(N-K)] [1:K]];
A=spdiags(B,d,N,N);
A=A+A'; %could do it directed not symetric