Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dijkstra variant with k nodes?

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

like image 574
Denise Avatar asked Oct 30 '22 01:10

Denise


1 Answers

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

like image 81
xenteros Avatar answered Nov 15 '22 07:11

xenteros