Shorter path algorithm