5

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

Denise
  • 131
  • 10
  • A problem is that the start node is marked when you start. You have to unmark it – CloudPotato Jun 22 '16 at 09:48
  • yes! Any ideas about implementation for the k nodes? – Denise Jun 22 '16 at 09:54
  • This is at least as hard as solving the NP-hard Hamiltonian Cycle problem, since you could solve that problem simply by picking any vertex as the source/destination vertex, setting k=n, and then running your algorithm. – j_random_hacker Jun 22 '16 at 11:53
  • 1
    In a *path*, no vertex can appear more than once. Is that really what you want? If so, xenteros's answer is solving a different problem (it counts *walks*, where repeated vertices and edges are allowed), and you're out of luck as the problem is NP-hard as I explained above. If instead you allow repeated vertices and edges, then you want to count *walks* -- so please edit. (Or, maybe you want to count *trails*, where vertices but not edges can be repeated.) – j_random_hacker Jun 22 '16 at 12:09
  • The vertex can't repeat in the path because after I visited I mark it, instead the edges are allowed to repeated. So I think the xenteros's solution works if I make this variant in the algorithm. what do u think? – Denise Jun 22 '16 at 13:22
  • If you forbid repeated vertices, then you also forbid repeated edges, since if some edge (u, v) occurs twice then the vertex u (and v) must also appear twice! – j_random_hacker Jun 22 '16 at 14:10
  • This can also be solved using the Bellman-Ford algorithm. Just set the outer for loop to run up to `k` vertices instead of the `No. of vertices -1` times. That way you will get the shortest path between two paths having at most `k` edges. – thebenman Sep 13 '16 at 07:12

1 Answers1

2

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

xenteros
  • 15,586
  • 12
  • 56
  • 91
  • thank you for your reply.why set to INF? and how can i pass the k nodes to algorithm? – Denise Jun 22 '16 at 09:53
  • there is an implementation under the posted URL – xenteros Jun 22 '16 at 09:59
  • @Denise feel free to put uparrow and accepted answer next to my answer :) – xenteros Jun 22 '16 at 10:08
  • 1
    The linked algorithm considers *walks*, where vertices (and edges) are allowed to repeat. The OP asks for *paths*, where vertices (and, therefore, edges) are not allowed to repeat. (It may be that the OP is confused about what s/he wants, but -1 at least until you clarify which problem the linked algorithm solves.) – j_random_hacker Jun 22 '16 at 12:05
  • if you flag visited verticles and disallow using them, this will become appropriate algorithm – xenteros Jun 22 '16 at 12:21
  • 1
    Unfortunately the DP no longer works in polynomial time if you do this, because you need to record the *set* of visited vertices as part of the DP state, and there are O(2^n) possible such sets. The non-DP algorithm still works of course, but it's also exponential-time. – j_random_hacker Jun 22 '16 at 12:25