Finding the shortest path on the graph