Implement nearest neighbor graph