Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.     } } 
like image 539
ABHISHEK WALTER Avatar asked Oct 31 '14 08:10

ABHISHEK WALTER


People also ask

Is backtracking always recursive?

iterative version of grid A* is backtracking without any recursion ... it is simple O(n) loop without any stack ...

What is the difference between backtracking and dynamic programming?

What are the differences between dynamic programming and backtracking? Dynamic programming emphasizes on overlapping subproblems, while backtracking focus on all or some solutions. Dynamic programming relies on the principle of optimality, while backtracking uses a brute force approach.

What is recursive backtracking?

backtracking recursion: ○ We can generate all possible solutions to a problem or count the total number of possible. solutions to a problem. ○ We can find one specific solution to a problem or prove that one exists. ○ We can find the best possible solution to a given problem.

What is the difference between recursion and iteration?

Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false.


2 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:

Tree with bad nodes and one good node

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

like image 51
javier_domenech Avatar answered Oct 02 '22 17:10

javier_domenech


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

like image 33
dasLort Avatar answered Oct 02 '22 17:10

dasLort