Implementing the shortest path generation