Question

Does anyone know how can I write a programming graph-algorithm (C++ code would be great) that finds the Kth shortest path for a given set of nodes and edges in a cyclic graph?

For example, the shortest path (that could be found by Dijkstra or Bellman Ford) is considered to be the 1th shortest path. Now the 2nd shortest path is the shortest one that comes after the 1st shortest path. Now I want the algorithm to find the Kth shortest path. you are given the number of nodes, edges and the set of edges, as the following:

number of nodes: 5
number of edges: 6
edges:
0 1
0 2
1 2
2 3
3 1
1 4
source node:0
destination node: 4

"Note that this graph contains a cycle" Thank you.

Was it helpful?

Solution

Use a uniform cost search algorithm. Where the Wikipedia says "return solution", don't quit and return but append the result to some list until that list contains k paths. The k'th element of the list (counting from 1) will be the k'th shortest path.

Don't keep a "closed"/"explored" set or this algorithm won't work properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top