nearest neighbor thermodynamic algorithm