BUG: fix logic in shortest_path.  Closes gh-3466.

(backport of eeee194)