Backtracking algorithm gets stuck

JDLK7

I have this problem of a matrix (map) that starting from top-left corner, I want to find the less heavier path to bottom-right corner. It has the condition that it can only move right, down or right-down.

This is an example: matrix example

I need to solve the problem with backtracking, but I can't tell if I'm doing it well.

This code is able to solve matrix sizes up to 10x10, but when I try a 20x20 matrix, it gets stuck (or at least that's what I think after hours).

/*
 * i, j -> matrix iterators.
 * n, m -> matrix height and width
 * map  -> matrix
 * actualPath, bestPath -> vectors for representing the path later
 * actual -> actual path weight
 * best -> best path weight
 */

 int backtracking(int i, int j, const int &n, const int &m, 
             const vector<vector<int>> &map,
             vector<vector<int>> &actualPath,
             vector<vector<int>> &bestPath,
             int best) {

     recursiveCalls++;
     int actual = 0;

     //Bottom-right corner
     if(i == (n-1) && j == (m-1)) {
         return map[i][j];
     }
     //Last row, only right
     else if(i == (n-1)) {
         actual = map[i][j] +
                  backtracking(i, (j+1), n, m, map, actualPath, bestPath, best, profundidad);
     }
     //Last column, only down
     else if(j == (m-1)) {
         actual = map[i][j] +
                  backtracking((i+1), j, n, m, map, actualPath, bestPath, best, profundidad);
     }
     else {
         int downRight = backtracking((i+1), (j+1), n, m, map, actualPath, bestPath, best, profundidad);
         int right = backtracking(i, (j+1), n, m, map, actualPath, bestPath, best, profundidad);
         int down = backtracking((i+1), j, n, m, map, actualPath, bestPath, best, profundidad);

         actual = map[i][j] + minimo(downRight, right, down);
     }

     if(actual < best) {
         best = actual;
         bestPath = actualPath;
     }

     return best;
 }

Is it possible that it gets stuck because I don't use bounds? Or is it bad implemented? I don't know what I'm doing wrong. I think I understand this algorithm but I guess I don't know how to implement it for this problem...

Ayon Nahiyan

Although backtracking will give you the correct answer here. It is not the fastest solution in this case.

You are doing a lot duplicate work here, which are not necessary. Straightforward Backtracking is not useful in this case. Lets take a look at this example,

suppose the grid size is 10X10.

one of the trees of the backtrackting stared from (0,0) -> (0,1) 
another started from (0,0) -> (1,0)
and another started from (0,0) -> (1,1)

When the 1st traversal reaches point (5,5) it will keep finding all possible ways to go to (9,9). Now the 2nd traversal when that reaches (5,5) it will do the exact same work the first traversal did from this stage, and so will the 3rd traversal. So these duplicate steps are the places where you are exhausting your program and your code is taking way too long to execute. Your code is not stuck its just running for very long time. You can memoize the results easily to optimize the time here.

So if you can save the value that you found when you first reached a point (i,j) as save[i][j], then when some other traversal reaches this same point(i,j) it can decide not to traverse any further and use this save[i][j] for its own. This way you can make the code lot more faster.

This way it will become more of dynamic programming than backtracking, and even a grid of size 10000X10000 will take around few seconds to give you the results.

In this answer I only described how to find the value of the path towards min value, if you want to find the path thats also possible using the same DP solution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related