5

I need to know whether the following problem has been studied before:

Let's have a directed acyclic graph G. The graph is connected, it has exactly one source vertex (no incomming nodes) and exactly one sink vertex (no outgoing nodes).

Each vertex has been assigned a nonnegative price in dollars and also a color.

The goal is to find a walk from the start to the sink which maximizes the sum of prices of visited edges.

The catch is, that the price is received only when a vertex of particular color is visited for the first time. For example when we wisit red vertex with price $1, then blue one with $2 and then red with $30 the total price is $3.

The approximate size of my particular problem: 50000 vertices, 3000 colors, typical walk length from start to sink about 200 edges.

             ------>[B red $1]---                   ---->[E red $1]----
            /                    \                 /                   \
[A black $0]                      ==>[D black $0]==                     ==>[G black $0]
            \                    /                 \                   /
             ---->[C green $2]---                   ->[F green $1000]--
danatel
  • 4,844
  • 11
  • 48
  • 62
  • 1
    It looks like a variation of the [TSP problem](http://en.wikipedia.org/wiki/Travelling_salesman_problem)? – CRM Dec 15 '11 at 04:31
  • 1
    What I love about this problem is that there is very likely to be some conditions where you would want to not hit a color because going to it would require you take a lower value on another color and give you a net loss. – corsiKa Dec 15 '11 at 04:35
  • @bacchus: I don't think so: TSP must go over all vertices. I must choose just few of them. – danatel Dec 15 '11 at 11:40

3 Answers3

1

This is a nice variant of the shortest path problem that considers dynamic graphs, i.e. graphs that change over time (as you get farther from the source, edge weights change). Dijkstra's algorithm will solve it since the weights of edges are calculated as you go, so the fact that edge weights are different based on where you've been doesn't matter. Here's wikipedia's description with considerations for the the color variation problem in bold:

Let the node at which we are starting be called the initial node. Let the distance of node Y be the distance from the initial node to Y. Dijkstra's algorithm will assign some initial distance values and will try to improve them step by step.

  1. Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes.

  2. Mark all nodes unvisited. Set the initial node as current. Create a set of the unvisited nodes called the unvisited set consisting of all the nodes except the initial node.

  3. For the current node, consider all of its unvisited neighbors and calculate their tentative distances. This now depends upon the chosen path up to this point. For example, if the current node A is marked with a distance of 6, and the edge connecting it with a neighbor B has length 2 based on the colored weight, then the distance to B through the path so far will be 6+2=8. If this distance is less than the previously recorded distance, then overwrite that distance. Even though a neighbor has been examined, it is not marked as visited at this time, and it remains in the unvisited set.

  4. When we are done considering all of the neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never be checked again; its distance recorded now is final and minimal.

  5. If the destination node has been marked visited, then stop. The algorithm has finished.

  6. Set the unvisited node marked with the smallest tentative distance as the next "current node" and go back to step 3.

Nannicini and Liberti have a nice survey on Shortest paths on dynamic graphs.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • May be I didnt' get your point, but I think this will not work. Imagine there are two alternate walks at the beginning of the traversal - the first one over green $1 vertex and the second one over red $2 vertex. Then, near the end there are again two alternatives green $1000 and red $1. If you choose the red one in first place ($2 > $1) you can no longer use the valuable green one . – danatel Dec 15 '11 at 11:38
  • @danatel The point that that you want to treat the weights as dynamic, meaning that the weights depend on the path up to that point. This idea is compatible the way Dijkstra's algorithm works, because you take one step at a time. – PengOne Dec 15 '11 at 18:10
  • please show me how your algorithm can find the best walk over the example I added to the question. Walking from A to G. I think that the corrent solution is A-B-D-F-G for $1001 but Dijkstra algorithm visits A (0), B(1), C(2), D(2). Now, F lost its value so F(2), E(3) and G(3). What did I do wrong? – danatel Dec 15 '11 at 20:13
1

If I understand correctly the problem, it can be solved in O(E) time as follows. Let's start by formally defining its solution.

Given a DAG G = (V, E), let FS(i) be the forward star of a vertex i:

FS(i) = {(x, y) in E: x = i}

let C the be set of colors of vertices visited so far and let p[i] and c[i] be respectively the price and color of a vertex i; now define the reward r for going from vertex i to vertex j belonging to FS(i) as

r[i, j] = p[j] if c[j] does not belong to C

otherwise r[i, j] = 0 (if c[j] belongs to C)

We are now ready to define our subproblems as follows:

D(i, j, C) = max on k belonging to FS(i) { r[i, k] + D(k, j, C union {c[k]}) }

with D(i, i, C) = 0 as termination condition (required when we reach the sink node)

The initial problem to be solved is therefore D(s, t, {c[s]}) where s and t are respectively the source and sink vertices:

D(s, t, {c[s]}) = max on k belonging to FS(s) { r[s, k] + D(k, t, {c[s]} union {c[k]}) }

To solve the problem, you store the DAG using adjacency lists corresponding to the forward stars of the vertices. Then starting from the source vertex, you determine the cost associated to all of the paths taking into account the color constraint. Since you are basically exploring the forward stars of vertices and never go back, the overall complexity is O(E) using an hash table to represent the set, so that insertion of a color and membership testing require O(1) time on average. What I called union is actually an insert operation (results are the same since every time you do the union of a singleton set).

Massimo Cafaro
  • 25,429
  • 15
  • 79
  • 93
  • This is one of the methods I tried. I think that the problem is that the color constraint is not defined by the set of all colors but by the power set of all their possible combinations (2^3000 in my case). For example we have to remember the cost on condition that red has been used, green has been used, both red and green have been used and so on. Each of the costs is different. This array of costs need to be updated in each step. The size of the cost array grows with i! as we walk the DAG. I used this with pruning and it worked for some cases. – danatel Dec 16 '11 at 19:52
  • @danatel: you may want to edit/update your question to reflect this information related to colors. Moreover, I still need additional info to fully understand your problem. In your example, what is the difference if you discover a red vertex given that a) you already visited a red vertex or b) you already visited two red and green vertices ? I thought that in both cases the reward associated to the newly discovered red vertex was zero. – Massimo Cafaro Dec 16 '11 at 20:34
  • So, please clearly explain what you mean by cost on condition that red has been used etc. What is the relationship between this cost and the reward associated to a newly discovered vertex ? – Massimo Cafaro Dec 16 '11 at 20:43
  • Soory, I "used" cost instead of "reward." There is difference in your example (R or 2R+G visiting R). But you do not know in advance what you will discover near the end. So you must rememeber reward for R, G and RG because near there may be a choice between 4 walk: cheap black, more rewarding green, similar red and very good walk over booth R and G. So you can walk the DAG just once, but you must store rewards obtained so far in a hash table indexed by bitmap of 3000 bits (i called this an array). After each visited node you update the . When you visit R – danatel Dec 17 '11 at 06:41
  • After each visited node you update the hash table, R after R+G and R after R+R+R+G share the same cell in the hash table - maximum reward is stored. – danatel Dec 17 '11 at 06:52
  • There is a typo in my comment "There is difference in your example (R or 2R+G visiting R)" should be "There is no difference in your example (R or 2R+G visiting R)" - sorry for the mess - comments cannot be edited for more than 5 minutes. – danatel Dec 17 '11 at 06:56
0

Interesting problem. It's NP-hard but doesn't bear resemblance to any of the classics of the approximation algorithms literature.

Proof by example of a reduction from the set cover instance {{1, 2, 3}, {2, 4}, {3, 4}, {4, 5}}. All edges are directed downward.

 s:$0
 | \
 |   \
 |     \
 |     1:$100
 |      |
!A:$1  2:$100
 |      |
 |     3:$100
 |     /
 |   /
 | /
 *:$0
 | \
 |   \
 |     \
 |     2:$100
!B:$1   |
 |     4:$100
 |     /
 |   /
 | /
 *:$0
 | \
 |   \
 |     \
 |     3:$100
!C:$1   |
 |     4:$100
 |     /
 |   /
 | /
 *:$0
 | \
 |   \
 |     \
 |     4:$100
!D:$1   |
 |     5:$100
 |     /
 |   /
 | /
 t:$0

I have no idea whether there's a good approximation algorithm (this reduction doesn't rule it out). The LP relaxation with a unit flow instead of a path might be good enough for branch and bound to work.

Per
  • 2,594
  • 12
  • 18
  • @Per: Thank you. I agree. I want to know, if this problem has a name, has been studied before and if someone invented an approximation or heuristic algorithm. – danatel Dec 15 '11 at 11:44
  • 1
    @DonAndre No, it's not, but I would have imagined that the set of people who (a) would appreciate a formal proof (b) can't figure out how it would go from the example is empty. – Per Dec 15 '11 at 13:07
  • @danatel The location-varying payout for collecting a color is a problem feature I've *never* seen before, and if there is a theory paper on this problem, it probably makes use of an LP that will be very, very difficult to solve efficiently enough at the scale you're interested in. Someone else will have to speak to the AI literature. – Per Dec 15 '11 at 13:45
  • 1
    Ah, the infamous proof by intimidation: Clearly, this must be clear to all! You have shown that when you turn a set cover problem into an awkward set cover problem it's still a set cover problem. You need to reduce the original problem to the set cover problem to show it has the same properties and not the other way round. This just means that some instances of the original problem may be set cover problems. – Andreas Dec 15 '11 at 17:19