Tutorial :count of distinct acyclic paths from A[a,b] to A[c,d]?



Question:

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.


Solution:1

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.


Solution:2

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.


Solution: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.


Solution:4

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.


Solution:5

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?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »