finds the best node to replace in the path