added a generalized shortest path function