I am trying to implement Dijkstra\'s algorithm which can find the shortest path between the start node and the end node. Before reach the end node there are some \'must-pass
By finding shortest path between must include node and two(end and start) node. Form graph, then run shortest path algo(Dijkstra's algorithm). Start and end nodes will be same.
This is a generalisation of the travelling salesman problem. The TSP comes up as the case where all vertices are "must-pass".
Find shortest paths between each pair of must-pass vertices, from the source to each must-pass vertex, and from each must-pass vertex to the sink. Then use the famous O(n 2^n) dynamic programming algorithm for TSP to find the shortest path from source to sink meeting your constraints; here n will be two plus the number of must-pass vertices.
Unfortunately this problem is reduced to TSP so dont expect a polynomial solution but if no of intermediate node are small then you can do this reasonably fast like following :-
- try every sequence of nodes to visit possible.
- say you have s->a->b->c->d
- then evaluate min(s,d) + min(d,a) + min(c,d) using dijkstra
- the sequence that has minimum distance is your answer.
Time complexity : O(k!*ElogV)
where k is no of must pass nodes