function A = graph_knn(sdm,nb)

% [A a] = graph(sdm,nb)
% form symmetric knn graph
% sdm - squared distance matrix
% nb - number of neigbors 

% number of points
n = size(sdm,1);

% sort distances
[xx,idx] = sort(sdm,'ascend');

% indexes of nb nearest neighbors
idxR = idx(2:nb+1,:);
idxC = ones(nb,1)*[1:n];
A = sparse(idxR(:),idxC(:),1,n,n);
A = A|A';


