Algorithm to find the path between two vertices