Polynomial-time algorithm for a traveling salesman in a grid

I read that the classic traveling salesman problem (TSP) is NP-Hard. And there are some approximation algorithms, as well as a specific algorithm that runs in O (N ^ 2 * 2 ^ N) time. But AFAIK, this is for TSP in general.

So my question is, is there a better algorithm (preferred polynomial moment) for solving TSP in the grid M x N?

For example, let's say that there is a 3x4 grid and there are different costs for moving from one cell to each of 2 neighboring (lower and right) cells. Therefore, I want to find the minimum cost for visiting all cells, starting from cell (0, 0) and returning to cell (0, 0).

EDIT: Just to clarify the situation, I'm sure it's not a Euclidean TSP. For simplicity, consider the following example. The rectangle is divided into M rows and N columns. The seller is in cell 0, 0 (upper left cell). He should visit all cells and return to his initial cell (0, 0). But it can only move from one cell to each of four neighboring cells (top, left, bottom, right). And the cost of one cell in any of its neighboring cells may not be the same.

Thank.

+4
source share
3 answers

The complexity of the TSP is due to the number of possible routes between two nodes that are exponential.

node ( node , , 4 ), - O (4 ^ (M * N)) M * N 4 .

, - NP-hard.

. , , , . , 3 ( 4). , O(3^(M*N)). , .

+1

, , TSP , NP- ( ). , , . O (poly (m, n) exp (min (m, n))) - TSP min (m, n) (.. mxn).

, .

+1

, , - , TSP, , - , O (3 ^ (m * n) * (m * n)), DP-

O(3^(m*n)*(m*n)):

int mindist = infinity;

void GridTSP(int x,int y,int visited[][],int dist,int total,int n,int m) {

    if(total==n*m) {

          if(dist<mindist)
              mindist = dist;
    }

    else if(visited[x][y]) {

         return;
   }

   visited[x][y] = true;

   for each neighbour (a,b) of (x,y)  :
          GridTSP(a,b,visited,dist+cost(x,y,a,b),total+1,n,m);


   visited[x][y] = false;
}


GridTSP(0,0,new int[3][4],0,0,3,4);
0
source

Source: https://habr.com/ru/post/1532260/


All Articles