1

Requirements:

  1. There are multiple targets that you need to visit on a graph (does not matter what order or how many times you visit each point)

  2. You can start from the starting point, visit all the targets and come back to the base.

  3. You are allowed to visit each target multiple times.

Question:

1) What algorithm should I use to approach this?

2) My proposed approach

Let's say targets = [A, B, C]

  • I am thinking to use Dijkstra's algorithm to find the shortest path to any of the targets.
  • Once I reach the target, I use Dijstra's to find any of the remaining targets.
  • Once I have found all the targets, I will use Dijstra's to find the path back to the starting point.
  • This should give me the shortest path to find all the targets and back home
samol
  • 18,950
  • 32
  • 88
  • 127
  • 1
    If you can visit each target multiple times, it's obvious to visit the same target subsequentially, because the distance between a target and itself is 0, and you can count 3 visits to the same point as 1 visit. So what additional rule is there that makes this problem different from a travelling salesman problem? – GolezTrol Jul 08 '15 at 06:15
  • 1
    @GolezTrol you must visit all target nodes. – Adam Jul 08 '15 at 06:30
  • @GolezTrol Not sure your question. Once I have visited A. A is removed from the target list. While I might still traverse past it in the future, it is no longer a target – samol Jul 08 '15 at 06:37
  • 2
    I second the comment of GolezTrol; the permission to visit the same node multiple times does not make the problem any easier, alt least for nonnegative edge weights. To my understanding, the problem in discussion is exactly the traveling salesman problem (supposed that nonnegative edge weights are used). – Codor Jul 08 '15 at 06:38
  • @codor Ok my bad I have changed the problem – samol Jul 08 '15 at 06:46
  • @Codor is my solution flawed? – samol Jul 08 '15 at 06:47
  • @Codor although I believe that not allowing to visit the same node twice is what makes something that traveling salesman problem: http://cs.stackexchange.com/a/1761/34665 – samol Jul 08 '15 at 06:49
  • You haven't changed the problem. – Adam Jul 08 '15 at 06:49
  • Your solution is not valid for weighted directed graphs. – Adam Jul 08 '15 at 06:50
  • @Adam I meant I changed the part where I stated this is not traveling salesman problem – samol Jul 08 '15 at 06:51
  • Why is my solution not correct for weighted directed graph? The shortest between each target is the shortest overall? – samol Jul 08 '15 at 06:52
  • possible duplicate of [Non-cycle path to all nodes](http://stackoverflow.com/questions/2359345/non-cycle-path-to-all-nodes) – Lior Kogan Jul 08 '15 at 07:01
  • Here's why your solution doesn't work for weighted directed graphs. Imagine a graph `(A, B, w=1), (B, C, w=1), (S, A, w=13), (S, B, w=12), (S, C, w=11)`. source is vertex `S`, targets `A`, `B`, `C`. The optimum is `S -> A -> B -> C -> S`. Your solution will go `S -> C -> S -> A -> B -> C -> S`. – Adam Jul 08 '15 at 17:51
  • @adam I think you misunderstood my proposal. I wouldn't consider S until all the targets are exhausted. My proposal will give a solution of `S->C->B->A->S` which has total weight of 26 – samol Jul 10 '15 at 07:51
  • @samol you misunderstood what a directed graph is. There is no `C->B` without going through `S`. (I did forget to include the `(C, S)` edge which has to exist). – Adam Jul 10 '15 at 13:53

2 Answers2

0

I think this will be a good approximation :

Construct a Minimum Spanning Tree.

Do a pre-order traversal taking your source as the root.

Remove edges which are not necessary (removing which does not disconnect your source and targets).

say these are your nodes :

enter image description here

construct an MST :

enter image description here

if a is your source : do an pre-order traversal taking a as root.

enter image description here

remove the edges that does not disconnect your target and sources. (It's easy to do using union-find)

Community
  • 1
  • 1
Karthik
  • 4,950
  • 6
  • 35
  • 65
  • Could you please explain why my proposed solution is not correct? And this is the correct solution? – samol Jul 08 '15 at 06:54
  • you are right but MST is an approximation of the TSP, which is essentially what the OP is solving.. no mention of approximation – softwarenewbie7331 Jul 08 '15 at 06:56
  • @samol your solution is incorrect, because If you are trying to Find shortest path from Source(S) to a target (T), you might visit a 1000 nodes which are also targets, but according to your solution you will again find shortest paths for another 999 (S,T) combinations which is not necessary and constructing an MST using unino-find solves that problem for you. – Karthik Jul 08 '15 at 06:59
  • @karthik, I need to clarify my solution. What you described won't happen. I am trying to find the shortest path to any of the targets. When I see a target, I remove the target from the target list and restart Dijstra's again – samol Jul 08 '15 at 07:04
  • @samol so you will not have a target node in mind when you start your first Dijkstra? your solution works well for unweighted edges, but I still think that it won't work for weighted graph. – Karthik Jul 08 '15 at 07:06
  • I was thinking I will have a list of target nodes in mind when I start Dijkstra's instead of just one target node. (Does that work?) – samol Jul 08 '15 at 07:11
  • Btw, you graph looks a pre-order traversal instead of an in-order traversal. Am I correct? – samol Jul 08 '15 at 07:11
0

You are on the right track, however your problem reduces into the Travelling Salesman Problem (TSP).

Using Dijkstra as you mentioned, you can replace all paths between target nodes that do not contain any target nodes, with a single edge. The result is a graph with only target nodes... which leaves you with TSP.

As for the weighted directed edge thing in the comments, I think as long as edge costs are non-negative, Dijkstra is perfectly fine.