The Near-Infrared Spectrograph (NIRSpec) on the James Webb Space Telescope V. Optimal algorithms for planning multi-object spectroscopic observations