11

I'm trying to come up with a reasonable algorithm for this problem:

Let's say we have bunch of locations. We know the distances between each pair of locations. Each location also has a point. The goal is to maximize the sum of the points while travelling from a starting location to a destination location without exceeding a given amount of distance.

Here is a simple example: Starting location: C , Destination: B, Given amount of distance: 45 enter image description here

Solution: C-A-B route with 9 points

I'm just curious if there is some kind of dynamic algorithm for this type of problem. What the would be the best, or rather easiest approach for that problem?

Any help is greatly appreciated.

Edit: You are not allowed to visit the same location many times.

Ali
  • 56,466
  • 29
  • 168
  • 265
Hzyf
  • 1,007
  • 2
  • 12
  • 21
  • Wouldn't CDA with 14 points be better? – happydave Apr 27 '14 at 03:42
  • Sorry, I just meant the path C->D->A. I was referring to the original post mentioning that C->A->B was optimal. Not related to your comment – happydave Apr 27 '14 at 03:44
  • @Hzyf Do you have any constraint on the value of amount? – Nikunj Banka Apr 27 '14 at 03:50
  • @NikunjBanka No there is not any constraint. – Hzyf Apr 27 '14 at 03:51
  • Is there a constraint on the points? – Niklas B. Apr 27 '14 at 04:06
  • @NiklasB. Yes points are in a range. Thanks for reminding me. – Hzyf Apr 27 '14 at 04:11
  • 1
    Please add the number of nodes, the number of edges and the bound on the valid point range *in actual numbers* – Niklas B. Apr 27 '14 at 04:18
  • @NiklasB. I don't have the actual data yet. It may vary depending on complexity of the algorithm. – Hzyf Apr 27 '14 at 04:31
  • It's easy to show your problem is NP-hard, because we can reduce Hamiltonian path to it. So there is probably no polynomial solution, unless you missed to add some more crucial information to the question. Are you looking for an exponential time solution? – Niklas B. Apr 27 '14 at 05:30
  • @NiklasB What might be that kind of crucial information? I don't think exponential time solution would be suitable. – Hzyf Apr 27 '14 at 05:40
  • @Hzyf I don't know, but the fact that you can't visit each node twice makes this more or less unsolvable – Niklas B. Apr 27 '14 at 05:47
  • @NiklasB. There might be some heuristic or dynamic programming approachs like TSP solving algorithms? – Hzyf Apr 27 '14 at 17:47
  • @Hzyf Algorithms for TSP are exponential. Of course if you only want a good approximation, it's a different story, but that has more or less nothing to do with your original question – Niklas B. Apr 27 '14 at 17:52
  • You can also always use the "big guns" like SAT or integer linear programming solvers, but they obviously can't solve all instances efficiently – Niklas B. Apr 27 '14 at 17:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51547/discussion-between-hzyf-and-niklas-b) – Hzyf Apr 27 '14 at 18:52

1 Answers1

7

EDIT: Under the newly added restriction that every node can be visited only once, the problem is most definitely NP-hard via reduction to Hamilton path: For a general undirected, unweighted graph, set all edge weights to zero and every vertex weight to 1. Then the maximum reachable score is n iif there is a Hamilton path in the original graph.

So it might be a good idea to look into integer linear programming solvers for instance families that are not constructed specifically to be hard.

The solution below assumes that a vertex can be visited more than once and makes use of the fact that node weights are bounded by a constant.


Let p(x) be the point value for vertex x and w(x,y) be the distance weight of the edge {x,y} or w(x,y) = ∞ if x and y are not adjacent.

If we are allowed to visit a vertex multiple times and if we can assume that p(x) <= C for some constant C, we might get away with the following recurrence: Let f(x,y,P) be the minimum distance we need to get from x to y while collecting P points. We have

f(x,y,P) = ∞ for all P < 0

f(x,x,p(x)) = 0 for all x

f(x,y,P) = MIN(z, w(x, z) + f(z, y, P - p(x)))

We can compute f using dynamic programming. Now we just need to find the largest P such that

f(start, end, P) <= distance upper bound

This P is the solution.

The complexity of this algorithm with a naive implementation is O(n^4 * C). If the graph is sparse, we can get O(n^2 * m * C) by using adjacency lists for the MIN aggregation.

Community
  • 1
  • 1
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
  • This looks like a reasonable dynamic programming approach, under the assumption that you are allowed to visit the same location many times, scoring points each time. I think the problem becomes much harder if you can only collecting points from each location once. – mcdowella Apr 27 '14 at 04:34
  • @mcdowella Yes, if the path has to be [simple](http://en.wikipedia.org/wiki/Path_(graph_theory)) this problem is most definitely NP-hard (I'm pretty sure it already is, if neither node nor edge weights are polynomially bounded) – Niklas B. Apr 27 '14 at 04:35
  • 1
    I missed to specify it. You are not allowed to visit the same location many times. – Hzyf Apr 27 '14 at 04:43
  • @Hzyf In that case the problem is NP-hard and there is no hope for an efficient algorithm. If we could solve it fast, we could use the algoritmh to solve Hamiltonion path in general graphs – Niklas B. Apr 27 '14 at 05:31
  • `can only be visited twice`? Don't you mean, "can't be visited twice"? – user3386109 Apr 27 '14 at 06:07
  • @user3386109 yes, I guess my brain switched between "can only be visited once" and "cannot visited twice" in the middle of the sentence. Gotta get some sleep. – Niklas B. Apr 27 '14 at 06:11