will call the shortest path first and tail recurse the other
moved partitioning to its own function