2

I currently work on a problem where I want to try to find an algorithm which does the following: Given a square grid graph G and start node S and an end node E, where E and S in G, find a path P from S to E with maximum value and |P| <= k. If it makes it easier, one can possibly make G a DAG.

The grid cells are either 0 or 1.

As an example:

S--o--o--o
|  :  |  |
o--o..o..o
:  |  :  |
o--o--E--o
|  :  |  |
o--o--o--o

S := "Starting State" 
E := "Ending State" 
- := "Edge value is 1" 
. := "Edge value is 0"

Solution with k = 5 (from what I see)

S  o  o  o
|         
o--o  o  o
   |     
o  o--E  o

o  o  o  o

S and E lie arbitrarily, so one cannot assume just down and right movement, but I can transform the graph to a DAG with some loss to optimality I assume.

Edge value is a cost, G is a grid graph where every node is connected to its four neighbours.

First of all, is this problem already known in literature? I did not find anything about it. Is it in NP or does someone has an idea for a fast algorithm? I asked the search engine of my choice, and somebody asked something maybe related to it on StackOverflow , but their problem description does not match 100%, since their goal is last row, where mine is a distinct node.

Community
  • 1
  • 1
jcklie
  • 4,054
  • 3
  • 24
  • 42

1 Answers1

1

Aight, a warning first: I've thought this up on the spur of the moment. I seem to remember reading about something like it before, but I can't remember where, so while it seems correct I can't be sure of it. If I spot a flaw later, I'll come back and edit this post and notify you.

Let L(k, v) be the value of the path of length at most k from S to some node v, and suppose v has predecessors {u1, u2, ... um}. Since G is a DAG, it must be that

L(k, v) = max { L(k-1, u1) + w(u1, v), L(k-1, u2) + w(u2, v), ..., L(k-1, um) + w(um, v) }

where w(u,v) is the weight of the edge from u to v.

To put this to use, what we're going to do is find the highest value path of length < R to every node within a radius of R of S. That then gives us enough information to calculate the highest-value path of length < R+1 to every node within a radius R+1 of S. So:

  1. First, throw away any node that is more than distance k from S, as it can't possibly be part of the optimum path. We have O(k^2) nodes remaining.
  2. Now initialize a collection L and set L[S] = 0. Leave all other entries undefined.
  3. Next, apply the L[v] rule to each node in the graph (ignore the k parameters)
    • If a predecessor u of a node v doesn't have a value for L[u] defined yet, ignore u when calculating L[v].
    • If no predecessor u of v has L[u] defined, leave L[v] undefined.
  4. Repeat Step 3 k-1 more times.
  5. If L[E] has a value, return it. Else there is no length k path from S to E.

This is O(k^3) time. You could probably speed it up for large graphs by only considering nodes both within distance 1 of S and distance k-1 of E during the first execution of Step 3, and only nodes within distance 2 of S and distance k-2 of E during the second execution, and so-on, but that'll still be cubic time.

Andy Jones
  • 4,723
  • 2
  • 19
  • 24