I have to find a min path from a source and destination where source and destination are the same node and I want a minimum fixed number of nodes in the path. I thought to implement a Dijkstra algorithm (in Java) with the variant that k nodes are included into the minimum path. (k is a minimum number of nodes to cover). It's correct? If yes, any suggestion for the implementation? Thanks in advance
It's a good idea. Remember to set distance to source to INF instead of 0 at the beginning for correct result.
EDIT
A simple solution is to start from u, go to all adjacent vertices and recur for adjacent vertices with k as k-1, source as adjacent vertex and destination as v. Following is C++ implementation of this simple solution. GeeksForGeeks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With