Gets the minimum length of a path from
r to some an end state
The implementation searches backwards from the end-tuple.
This approach was chosen because it is way more efficient if the first predicate given to
shortestDistances is small.
end argument must always be an end state.