implementing nn descent algorithm