13

I'm writing a sokoban solver for fun and practice, it uses a simple algorithm (something like BFS with a bit of difference).

now i want to estimate its running time ( O and omega). but need to know how to calculate count of acyclic paths from a vertex to another in a network. actually I want an expression that calculates count of valid paths, between two vertices of a m*n matrix of vertices.

a valid path:

  • visits each vertex 0 or one times.
  • have no circuits

for example this is a valid path:

alt text http://megapic.ir/images/f1hgyp5yxcu8887kfvkr.png

but this is not:

alt text http://megapic.ir/images/wnnif13ir5gaqwvnwk9d.png

What is needed is a method to find count of all acyclic paths between the two vertices a and b.

comments on solving methods and tricks are welcomed.

sorush-r
  • 10,490
  • 17
  • 89
  • 173
  • 3
    The number of possible paths will be a lot bigger than the number of paths considered by a BFS so I don't see how it'd help. A BFS repeatedly combines similar paths together which reduces the complexity. The complexity of a BFS is O(|V|+|E|). – fgb Mar 23 '10 at 15:14
  • Do you want a list of all the paths or just the number of paths? If you want the number of paths, would you settle for an approximation? – user287792 Mar 23 '10 at 18:25
  • I don't want list of them. i want to calculate count of them without counting them. – sorush-r Mar 23 '10 at 20:04
  • no. but if you have an idea about it please share with us? – sorush-r Mar 24 '10 at 07:04
  • Just wondering, why is this community wiki instead of a normal question? – rettvest Mar 24 '10 at 10:34

5 Answers5

4

The general problem of counting the number of simple paths in a graph is #P complete. Some #P-complete problems have fully polynomial randomized approximation schemes, and some don't, but you claim not to be interested in an approximation. Perhaps there's a way to take advantage of the grid structure, as there is for computing the Tutte polynomial, but I don't have any ideas for how to do this.

user287792
  • 1,581
  • 9
  • 12
  • I'm pretty sure that count of paths are finite. I'm not sure but i think there must be a recursive sequence of the count, also it would be extremely hard to estimate it without actually evaluating. – sorush-r Mar 24 '10 at 22:37
  • Actually it might be quite a lot easier to approximate than to compute exactly. Edit the question if you change your mind... – user287792 Mar 25 '10 at 22:05
  • 1
    "it would be extremely hard to estimate it without actually evaluating" -> yes, exactly what I think. you could look up theory for planar graphs to get an approx formular: http://www.cs.brown.edu/sites/jgaa/accepted/2007/RobertsKroese2007.11.1.pdf – Karussell Mar 28 '10 at 11:17
4

Not a solution but maybe you can think this idea a bit further. The problem is that you'll need to calculate also the longest possible path to get all paths. The longest path problem is NP complete for general graphs, so it will get a very long time even for relative small graphs (8x8 and greater).

Imagine the start-vertex is in the top, left corner and the end vertex is in the lower, right corner of the matrix.

  • For a 1x2 matrix there is only 1 possible path
  • 2x2 matrix => 2***1** paths => 2
  • 3x2 matrix => 2***2** paths => 4
  • 3x3 matrix => 2***4** + 2*2 paths => 12
  • 3x4 matrix => 2***12** + 12 + 2 paths => 38

Everytime I combined the results from the previous calculation for the current number of paths. It could be that there is a close formular for such a planar graph, maybe there is even a lot of theory for that, but I am too stupid for that ...

You can use the following Java (sorry, I am not a c++ expert :-/) snippet to calculate possible paths for larger matrices:

public static void main(String[] args) {
    new Main(3, 2).start();
}
int xSize;
int ySize;
boolean visited[][];

public Main(int maxX, int maxY) {
    xSize = maxX;
    ySize = maxY;
    visited = new boolean[xSize][ySize];
}

public void start() {
    // path starts in the top left corner
    int paths = nextCell(0, 0);
    System.out.println(paths);
}

public int nextCell(int x, int y) {
    // path should end in the lower right corner
    if (x == xSize - 1 && y == ySize - 1)
        return 1;

    if (x < 0 || y < 0 || x >= xSize || y >= ySize || visited[x][y]) {
        return 0;
    }

    visited[x][y] = true;
    int c = 0;
    c += nextCell(x + 1, y);
    c += nextCell(x - 1, y);
    c += nextCell(x, y + 1);
    c += nextCell(x, y - 1);
    visited[x][y] = false;
    return c;
}

=>

  • 4x4 => 184
  • 5x5 => 8512
  • 6x6 => 1262816
  • 7x7 (even this simple case takes a lot of time!) => 575780564

This means you could (only theoretically) compute all possible paths from any position of a MxM matrix to the lower, right corner and then use this matrix to quickly look up the number of paths. Dynamic programming (using previous calculated results) could speed up things a bit.

Karussell
  • 17,085
  • 16
  • 97
  • 197
  • I am glad that it helps and I hope that this makes sense and is nearly bugfree. Thanks for this interesting question :-) ! – Karussell Mar 28 '10 at 11:47
  • see the first reference (blum + hewitt: automata on a 2 dimensional tape) here: ftp://ftp.inf.fu-berlin.de/pub/reports/tr-b-97-08.ps.gz -> there they compute the number of possible cycles in a KxK grid. Maybe this is a nearly identical problem to yours -> simply connect the lower right vertex with the top left vertex ? – Karussell Mar 28 '10 at 12:50
  • A search on OEIS reveals that the first 19 values for NxN are known: http://research.att.com/~njas/sequences/index.html?q=184%2C8512%2C&language=english&go=Search – user287792 Mar 28 '10 at 15:40
  • 1
    (That it's only 19 suggests very strongly that there's no closed-form formula in the literature.) – user287792 Mar 28 '10 at 15:41
3

There is a similar but less general problem on project Euler: http://projecteuler.net/index.php?section=problems&id=237

I think some of the solutions described in the forum there can be extended to solve your general case. It's a pretty difficult problem though, especially for your general case.

To get access to their forums, you first need to solve the problem. I won't post the answer here, nor link to a certain site that lists the answer, a site that you can easily find on google by searching for something really obvious.

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • Dear IVlad, i think it's more general than problem you mean. because 3rd rule of problem 237 says: The tour visits each square exactly once. but here The tour visits each square less than twice. (1 or 0 times) and tour starts and ends between 2 arbitrary and distinct vertices. – sorush-r Mar 23 '10 at 17:40
2

This is an open question in Mathematics with direct application to chemistry and physics in using it to model polymer bonds. Some of the earliest work done on this was done during the Manhattan project (nuclear bomb WWII.)

It is better known as the Self Avoiding Walk problem.

I spent a summer at my university mathematics department researching a monte-carlo algorithm called the pivot algorithm to approximate the parameters of the asymptotic fit of the number of Self-Avoiding Walks of a given length n.

Please refer to Gordon Slade's excellent book titled "The Self Avoiding Walk" for extensive coverage of the types of techniques used to approach this problem thus far.

This is a very complex problem and I wonder what your motivation may be for considering it. Perhaps you can find a simpler model for what you want, because Self Avoiding Walks are not simple at all.

ldog
  • 11,707
  • 10
  • 54
  • 70
1

Would a matrix showing the edges work? Consider building a Matrix showing where the edges are,i.e. [a,b]=1 <=> a->b is an edge in the graph, 0 otherwise. Now, raise this Matrix to various powers to show how many ways exist to get between vertices using n steps and then sum them to get the result. This is just an idea of one way to solve the problem, there may be other ways to frame the problem.

I wonder if this belongs on MathOverflow, as another idea


True, that once you have a zero matrix you could stop exponentiating as in your case, there aren't many places to go after 3, but the paths from 1 to 3 would be the direct one and the one that goes through 2, so there are only a few matrices to add together before the all zero one is found.


I'd think there should be a way to compute a bound of say n^(n+1), where n is the number of vertices in the graph, that would indicate a stopping point as by that point, every vertex will have been visited once. I'm not sure how to get the cyclic paths out of the problem though, or can one assume that the graph is free of cycles?

Community
  • 1
  • 1
JB King
  • 11,860
  • 4
  • 38
  • 49
  • Consider 1 -> 2, 1 -> 3, 2 -> 3. Adjacency matrix: 0 1 1 | 0 0 1 | 0 0 0. This matrix's elements will all be zero eventually by repeated exponentiation. If you consider a symetric adjacency matrix (for an undirected graph), then how will you even know when to stop exponentiating? – IVlad Mar 23 '10 at 16:33
  • What happens if the matrix never becomes 0 though? How do you know when to stop? – IVlad Mar 23 '10 at 17:42
  • The method described will also count paths where some nodes are visited more than once, so at best this will be an upper bound on the number of paths. The matrix keeps no information about the visited nodes, just the number of ways to go from a to b. – rettvest Mar 23 '10 at 21:20