Difference Between Backtracking and Recursion?

Difference Between Backtracking and Recursion?

What is the difference between backtracking and recursion? How is this program working?

void generate_all(int n)
{
    if(n<1) printf("%s\n", ar);
    else{
            ar[n-1]='0';        //fix (n)th bit as '0'
            generate_all(n-1);  //generate all combinations for other n-1 positions.
            ar[n-1]='1';        //fix (n)th bit as '1'
            generate_all(n-1);  //generate all combinations for other n-1 positions.
    }
}
2

7 Answers

That question is like asking what's the difference between a car and a DeLorean.

In recursion function calls itself until reaches a base case.

In backtracking you use recursion in order to explore all the possibilities until you get the best result for the problem.

Can be a bit hard to understand, I attach some text from here:

"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.

Suppose you get to a bad leaf. You can backtrack to continue the search for a good leaf by revoking your most recent choice, and trying out the next option in that set of options. If you run out of options, revoke the choice that got you here, and try another choice at that node. If you end up at the root with no options left, there are no good leaves to be found."

This needs an example:

Your piece of code is simply recursion, as you never get back if the result doesn't fit your goal.

0

Recursion describes the calling of the same function that you are in. The typical example of a recursive function is the factorial, i.e. something like

int fact(int n) {
    int result;
    if(n==1) return 1;
    result = fact(n-1) * n;
    return result;
}

What you see here is that fact calls itself. This is what is called recursion. You always need a condition that makes recursion stop. Here it is if(n==1) combined with the fact that n will always decrease each time it is called (fact(n-1))

Backtracking is an algorithm that tries to find a solution given parameters. It builds candidates for the solution and abandons those which cannot fulfill the conditions. A typical example for a task to solve would be the Eight Queens Puzzle. Backtracking is also commonly used within Neuronal Networks.

The program you described uses recursion. Similar to the factorial function, it decreases the argument by 1 and ends if n<1 (because then it will print ar instead of doing the rest).

recursion - no values abandoned;

backtracking - abandon some solution candidates;

also note that backtracking will call itself more than once in the body of function, while it's not normally true for recursion

In my understanding, backtracking is an algorithm, like all the other algorithms, like BFS and DFS, but recursion and also iteration are methods, they are at a higher level than the algorithms, for example, to implement a DFS, you can use recursion, which is quite intuitive, but you can also use iteration with a stack, or you can also think recursion and iteration are just methods to support your algorithms.

Recursion -

  • The problem can be broken down into smaller problems of same type.
  • Base case is required

Backtracking -

  • General algorithmic technique that takes in all the possible combination to solve a computational problem.

Recursion is just like what you showed. If routine A calls A, or if A calls B and B calls A, that is recursion.

Backtrack is not an algorithm, it is a control structure. When using backtrack, a program appears to be able to run backward. This is useful when writing a program to play a game like chess, where you want to look ahead some number of moves. When the program wants to make a move, it picks a move, then switches to its mirror-image opponent program, which picks a move, and so on. If the initial program gets to a "good place" it wants to say "Yay" and carry out the first move it made. If either program gets to a "bad place" it wants to back out and try another move.

You could just think of this as searching a tree, but if the move choices are complicated it may be more intuitive to think of it as a program "backing up" to the last place it chose a move, choosing a different move, and running forward again.

OK, so if that idea has appeal, how do you do it? First, you need a kind of statement representing a choice, where a move is chosen, that you can "back up" to and revise your choice. Second, whenever you have a sequence of statements like A;B, you make A a function, and you pass to it a function capable of executing B. Something like A(lambda()B(...)). So when A is finished executing, before returning it calls its argument that executes B. If A wants to "fail" and start "running backward" it simply returns without calling the function that calls B. I know this is hard to follow. I've done it via macros in LISP, and it works well. To do it in a normal compiler language like C++ is incredibly hairy.

I have done something like it in C/C++ in a way that preserves the "prettiness" but doesn't actually run backwards. The idea is you're doing this to perform some kind of depth-first tree search. But you could do it instead as a series of "stabs" down from the root of the tree, following a different path each time you "stab". This might be objected to on performance grounds, but it doesn't actually cost that much more, since the bulk of the work happens in the leaves of the tree. If the tree is 3 layers deep and has a branching factor of 5 at each node, that means it has 5 + 25 + 125 or 155 nodes. But in a series of "stabs" from the root, it visits 125 * 3 = 375 nodes, a performance penalty of less than 3x, which can be quite tolerable unless performance is really a problem. (Remember that real backtrack can involve quite a bit of machinery, making lambdas and so on.)

Here's the basic code I did it with:

#define NLEVEL 20
int ia[NLEVEL];
int na[NLEVEL];
int iLevel = 0;
int choose(int n){
    if (ilevel >= ns){ na[ns]=n; ia[ns]=0; ns++; }
    return ia[ilevel++];
}
void step(){
    while (ns > 0){
        if (++ia[ns-1] >= na[ns-1]) ns--;
        else break;
    }
}
bool search(int iLevel){
    iLevel++;
    switch(choose(2)){
    break; case 0:;
        // see if 0 is a win. if not, fail by returning false
    break; case 1:
        // choose move 1 and recur
        return search(iLevel);
    }
    return true;
}
// this is the "top level" routine
void running(){
    ns = 0;
    // repeat for stabs into choice tree until success
    do {
        bool bSuccess = search(0);
        if (bSuccess){
            // Yay!
            break;
        }
        step(); // this advances the stack of choices
    } while(ns > 0); // stop when success or there are no more choices
}

I've used this code to build a home-grown theorem prover in place of the search routine.

Recursion is like a bottom-up process. You can solve the problem just by using the result of the sub-problem.

For example, reverse LinkedList using recursion is just appending a head node on the already reversed sublist.

Backtracking is still like a top-down process. Sometimes you can't solve the problem just by using the result of the sub-problem, you need to pass the information you already got to the sub-problems. The answer(s) to this problem will be computed at the lowest level, and then these answer(s) will be passed back to the problem with the information you got along the way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.