9

I have read in Wikipedia and have also Googled it,

but I cannot figure out what "Backtracking Algorithm" means.

I saw this solution from "Cracking the Code Interviews"

and wonder why is this a backtracking algorithm?

enter image description here

Josef E.
  • 2,179
  • 4
  • 15
  • 30
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
  • 3
    this code cracks the interview because it violates Java code style – Display Name Jun 23 '14 at 18:06
  • 4
    Because at first you check drawers in the first room, but it's not found, so you **backtrack** out of the first room to check the drawers in the next room. It's also called **Trial & Error**. – Khaled.K Jun 24 '14 at 08:30

6 Answers6

28

Backtracking is a form of recursion, at times.

This boolean based algorithm is being faced with a choice, then making that choice and then being presented with a new set of choices after that initial choice.


Conceptually, you start at the root of a tree; the tree probably has some good leaves and some bad leaves, though it may be that the leaves are all good or all bad. You want to get to a good leaf. At each node, beginning with the root, you choose one of its children to move to, and you keep this up until you get to a leaf.(See image below)

enter image description here

Explanation of Example:

  1. Starting at Root, your options are A and B. You choose A.
  2. At A, your options are C and D. You choose C.
  3. C is bad. Go back to A.
  4. At A, you have already tried C, and it failed. Try D.
  5. D is bad. Go back to A.
  6. At A, you have no options left to try. Go back to Root.
  7. At Root, you have already tried A. Try B.
  8. At B, your options are E and F. Try E.
  9. E is good. Congratulations!

Source: upenn.edu

Josef E.
  • 2,179
  • 4
  • 15
  • 30
12

"Backtracking" is a term that occurs in enumerating algorithms.

You built a "solution" (that is a structure where every variable is assigned a value).

It is however possible that during construction, you realize that the solution is not successful (does not satisfy certain constraints), then you backtrack: you undo certain assignments of values to variables in order to reassign them.


Example:

Based on your example you want to construct a path in a 2D grid. So you start generating paths from (0,0). For instance:

(0,0)
(0,0) (1,0) go right
(0,0) (1,0) (1,1) go up
(0,0) (1,0) (1,1) (0,1) go left
(0,0) (1,0) (1,1) (0,1) (0,0) go down
Oops, visiting a cell a second time, this is not a path anymore
Backtrack: remove the last cell from the path
(0,0) (1,0) (1,1) (0,1)
(0,0) (1,0) (1,1) (0,1) (1,1) go right
Oops, visiting a cell a second time, this is not a path anymore
Backtrack: remove the last cell from the path
....
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • is this only for algorithms looking for one good result only? – Elad Benda2 Jun 24 '14 at 09:16
  • 1
    No. Sometimes you are interested in all solutions, or the "best" five (if you can compare two solutions). But from the moment you sometimes need to "destroy" a part of what you have constructed earlier in order to reassign variables, it is called a backtracking algorithm. – Willem Van Onsem Jun 24 '14 at 11:56
1

From Wikipedia:

Backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c ("backtracks") as soon as it determines that c cannot possibly be completed to a valid solution.

Backtracking is easily implemented as a recursive algorithm. You look for the solution of a problem of size n by looking for solutions of size n - 1 and so on. If the smaller solution doesn't work you discard it.

That's basically what the code above is doing: it returns true in the base case, otherwise it 'tries' the right path or the left path discarding the solution that doesn't work.

Since the code above it's recursive, it might not be clear where the "backtracking" comes into play, but what the algorithm actually does is building a solution from a partial one, where the smallest possible solution is handled at line 5 in your example. A non recursive version of the algorithm would have to start from the smallest solution and build from there.

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94
1

I cannot figure out what "backtracking algorithm" means.

An algorithm is "back-tracking" when it tries a solution, and on failure, returns to a simpler solution as the basis for new attempts.

In this implementation,

current_path.remove(p)

goes back along the path when the current path does not succeed so that a caller can try a different variant of the path that led to current_path.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

Indeed, the word "back" in the term "backtracking" could sometimes be confusing when a backtracking solution "keeps going forward" as in my solution of the classic N queens problem:

/**
 * Given *starting* row, try all columns.
 * Recurse into subsequent rows if can put.
 * When reached last row (stopper), increment count if put successfully.
 * 
 * By recursing into all rows (of a given single column), an entire placement is tried.
 * Backtracking is the avoidance of recursion as soon as "cannot put"...  
 *    (eliminating current col's placement and proceeding to the next col).
 */
int countQueenPlacements(int row) {     // queen# is also queen's row (y axis)
    int count = 0;
    for (int col=1; col<=N; col++) {    // try all columns for each row
        if (canPutQueen(col, row)) {
            putQueen(col, row);
            count += (row == N) ? print(board, ++solutionNum) : countQueenPlacements(row+1);
        }
    }
    return count;   
}

Note that my comment defines Backtracking as the avoidance of recursion as soon as "cannot put" -- but this is not entirely full. Backtracking in this solution could also mean that once a proper placement is found, the recursion stack unwinds (or backtracks).

WebViewer
  • 761
  • 7
  • 21
0

Backtracking basically means trying all possible options. It's usually the naive, inefficient solutions to problems.

In your example solution, that's exactly what's going on - you simply try out all possible paths, recursively: You try each possible direction; if you found a successful path - good. if not - backtrack and try another direction.

Jenian
  • 552
  • 1
  • 5
  • 18
  • No, it does not. If you ask prolog, which usually uses backtracking, to solve x=2, y=x then it will not try all values of x or all values of y and return only for the value 2. – Pete Kirkham Jun 24 '14 at 21:53
  • @Jenian is right: "Backtracking is an algorithmic technique where the goal is to get all solutions to a problem using the brute force approach. It consists of building a set of all the solutions incrementally. Since a problem would have constraints, the solutions that fail to satisfy them will be removed." – WebViewer Nov 06 '22 at 13:42