5

I have read that the classic travelling salesman problem (TSP) is NP-Hard. And there are some approximation algorithms and also a specific algorithm running in O(N^2 * 2^N) time. But AFAIK, these are for TSP in a general graph.

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

For example, say there's a grid of 3x4 and there are different costs of travelling from one cell to each of the 2 adjacent (bottom and right) cells. So I want to find the minimum cost to visit all the cells, starting from cell (0, 0) and returning to cell (0, 0).

EDIT: Just to clear things up, I'm pretty sure this not an Euclidean TSP. For simplicity, think of the below example. A rectangle is divided in to M rows and N columns. The salesman is at cell 0, 0 (the top-left cell). He has to visit all the cells and still come back to his starting cell (0, 0). But he can only travel from one cell to each of its 4 adjacent cells (top, left, bottom, right). And the cost from one cell to any one of its adjacent cells may not be the same.

Thanks.

Roshnal
  • 1,284
  • 3
  • 16
  • 39

4 Answers4

0

With unit grid weights and no holes, there is no known polynomial-time algorithm but no known NP-hardness proof either.

With holes, it's NP-hard. ("Hamilton paths in grid graphs"; Itai, Papadimitriou, and Szwarcfiter; 1982.)

With no holes but arbitrary grid weights, there is a trivial reduction from the previous case, where we take an arbitrary grid and fill in the holes with edges so heavy that each one weighs more than the original edges combined, so this case is NP-hard as well. Since this is a special case of planar graph TSP, there's a linear-time approximation scheme due to my advisor, Philip Klein, but it's rather complicated. One of the pieces is a O(poly(m, n) exp(min(m, n)))-time dynamic program for solving TSP exactly in a graph of branchwidth min(m, n) (i.e., an m x n grid).

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
0

It is a very old post, but it seems that all answers are not accurate, so I will try to clear things up (mainly for future readers).

It is true (AFAIK) that there is no known polynomial solution to TSP when input is restricted to be a grid.

From here, to say the problem in NP-Hard - this jump is not justify and might be incorrect.

The fact that the degrees of nodes is greater than 2, makes the naive algorithm indeed to run in an exponential time complexity. As @Eyal Schneider mentioned, this doesn't prove it is NP-Hard.

Proving TSP in NP-Hard (from the method I know), is done using the fact that Hamiltonian-Path is NP-Hard. On grids graphs, Hamiltonian-Path is in P, hence the same method to prove it is NP-Hard won't work.

I provide here as well an example why the degree of each node is not necessarily a good indication if the problem is hard.

Take the TSP on the 2 layers grid graphs, all grid graphs of size 2 * n for some n. In this problem (which is easier the the general case of TSP on grids) the degree of all nodes (except for corner) is 3, which by most answers here means the naive algorithm time complexity will still be exponential with n (because there are O(n) nodes with degree greater than 2).

In fact, for 2 * n grids there are exactly 2 Hamiltonian paths (same circle in the opposite direction). Hence there exists a polynomial algorithm to solve TSP on 2 * n grids - check the 2 circles and return True if the minimum of them is lower than k (Given input of graph G and maximum value k).

For summary - there is no proof that TSP on grids is NP-Hard (AFAIK). There is also no any known polynomial algorithm to solve TSP on grids (AFAIK). As I see it, TSP on grids might be in NP-intermediate, but this class is a bit tricky, and it is quiet impossible to prove something is in NP-intermediate (it will be equivalent to prove P is not equal to NP).

Best,

Shahar

Shaq
  • 303
  • 1
  • 10
  • Nope, it's known to be NP-hard with variable weights, the variant considered in the question. – David Eisenstat Feb 07 '23 at 13:04
  • Can you please give a reference to support your argument? btw, I didn't say it is not NP-Hard, just that non of the other answers proved it is. Saying the degree is greater than 2 then the naive algorithm is taking an exponential time is a wrong answer. – Shaq Feb 08 '23 at 07:48
  • No, I cannot find a reference (which is not surprising since it's an undergraduate exercise to prove it using the hardness of general grid TSP as a starting point). – David Eisenstat Feb 08 '23 at 12:19
-2

The complexity of TSP is due the number of possible routes between two nodes which is exponential.

A grid has a fixed node degree (every node has e.g. 4 neighbors), but the number of possible routes between two traveled nodes is still O(4^(M*N)) for an M*N grid with 4-neighbourship.

Algorithms may run faster on a grid, but the problem is still NP-hard.

EDIT: You could omit the paths where a vertex is visited and then the path goes right back to the last vertex since these paths are unnecessarily long. This means that there are always 3 possible neighbors to choose from (instead of 4). Thus the complexity can be expressed as O(3^(M*N)). This also corresponds to the brute force approach described in Vikrams answer.

helb
  • 7,609
  • 8
  • 36
  • 58
  • Yeah, but how can you say its O(4^(M*N)) exactly? I thought N was taken as the number of vertices, which is M*N itself. – Roshnal Mar 18 '14 at 07:36
  • That would be the M and N from your Grid, hence there are M*N vertices. What do you mean by "exactly"? The complexity is on that order of magnitude. – helb Mar 18 '14 at 07:50
  • Let's the grid is MxN. Then let V = M*N (the number of vertices). So the running time will be O(4^V)? But shouldn't it be O(V^2 * 2^V)? Sorry if I'm missing something... – Roshnal Mar 18 '14 at 07:56
  • 1
    @IceArdor That is not how big-oh notation works. The test for big-oh equivalency would be lim sup V->∞ (V^2 * 2^V)/(2^V) < ∞, which is not the case as that ratio is V^2 which goes to ∞ in the limit. – A. Webb Mar 18 '14 at 19:40
  • 1
    -1 This does not prove or give a reference that Traveling Salesman on a Grid is NP-hard. In fact, it is even an open problem whether TSP is NP-hard in the more general solid grid graphs. See also the answer by Shaq. – cero Feb 07 '23 at 12:36
  • (only open for unit weights, not the variant considered in the question) – David Eisenstat Feb 07 '23 at 13:08
-3

The problem you are solving is still np hard as it gets reduced to TSP if you convert grid into graph and forget the you were ever solving it for the grid but you can solve this one using simple brute force in exponential time of O(3^(m*n)*(m*n)) which is preferable than the complex DP solution

Brute force solution to your problem in 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);
Vikram Bhat
  • 6,106
  • 3
  • 20
  • 19
  • 7
    The fact you can reduce this problem to standard TSP doesn't mean it is np-hard... You need to reduce from TSP to this problem. – Eyal Schneider Mar 18 '14 at 21:23
  • You say it is O(3^n*n). What is n? The number of vertices? – helb Mar 18 '14 at 22:10
  • @helb sorry n here is size of grid – Vikram Bhat Mar 19 '14 at 04:13
  • @EyalSchneider I meant that the problem is already a TSP the grid structure means nothing , if you forget that it is a grid you will get a graph on which you solve TSP. – Vikram Bhat Mar 19 '14 at 04:18
  • @EyalSchneider The only advantage that you have is you know that degree of each vertex is at max 4 which can be used to get simple brute force solution of O(3^(m*n)*(m*n)) which is improvement over the O(n!) on general graph. – Vikram Bhat Mar 19 '14 at 04:25
  • 1
    @VikramBhat: This is not how you prove np-completeness. The grid can be considered as a complete graph with weights - of course. But so is a ring topology - where you have a trivial linear time algorithm. Also - the number of paths being exponential (which you didn't prove) doesn't mean that **selecting** one path must be exponential. – Eyal Schneider Mar 19 '14 at 06:26
  • @EyalSchneider The grid structure only gives information about adjacency of the vertices nothing else so you mean to say that if given a iso-morphic graph of the graph formed by the grid suddenly changing the graph to grid will make TSP solving on it polynomial time. In ring topology you are not using a linear time algorithm but it turns out to be linear because there is only one choice per vertex so branching factor is 1 hence O(1^n*n) which is O(n) so do not mistake it as linear time algorithm. – Vikram Bhat Mar 19 '14 at 09:33
  • @VikramBhat: The branching factor by itself can't prove anything. Consider for example the shortest path problem on a weighted grid. The 'branching factor' is 4, but you still have polynomial algorithms to solve it. – Eyal Schneider Mar 19 '14 at 09:49
  • @EyalSchneider I was saying ring topology has no decision in it so it will be polynomial but that does not mean that special case makes your TSP solution polynomial time. Shortest path has optimal substructure which make it solveable in polynomial time using DP or greedy whereas TSP doesnt have it(not yet found). Moreover my point of discussion was that what more information does grid structure give than just adjacency of vertices which is as good as having a adjacency matrix for a graph on which TSP is np. – Vikram Bhat Mar 19 '14 at 10:00
  • @VikramBhat: That's exactly what I disagree on. Sometimes simplifications of structures convert NPC problems to polynomial, and sometimes they don't. The fact that the search space is exponential doesn't mean anything. If you think this variation of TSP is NP-hard (I suspect it is), you'll have to prove it with a proper reduction. – Eyal Schneider Mar 19 '14 at 10:05
  • @EyalSchneider I strongly feel in order to make grid structure of any use the weights of edges must also be with some pattern but for arbitrary weights i feel it will be np – Vikram Bhat Mar 19 '14 at 10:06
  • @EyalSchneider For example if each edge is 1 unit then there is O(n^3) algorithm but with exceptions as well – Vikram Bhat Mar 19 '14 at 10:09
  • @EyalSchneider here is a link https://parasol.tamu.edu/~amato/Courses/620/openProblems/csce620-openProblem-P54_TSPinSolidGridGraphs_OzgurGonen.pdf – Vikram Bhat Mar 19 '14 at 10:13
  • @VikramBhat: interesting... if the edges have all the same weight 1, then any legal traversal has the same cost=mxn. As I see it, there are two fixed patterns that can do the traversals. One for even m and one for odd m (assuming n is even. Remeber that one of them must be even, otherwise there's no solution). So, why is it N^3? – Eyal Schneider Mar 19 '14 at 10:30
  • Oh I see... The O(N^3) algorithm is for general solid grid graphs. If it's rectangular, there's a linear solution. – Eyal Schneider Mar 19 '14 at 10:34
  • @EyalSchneider thats the reason why i think OP's problem is np because the weights are arbitrary hence the grid structure is not of much use – Vikram Bhat Mar 19 '14 at 10:38