Difference between backtracking and recursion?

限于喜欢 提交于 2019-12-02 14:44:06
vivoconunxino

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.

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

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

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.https://leetcode.com/problems/reverse-linked-list/discuss/386764/Java-recursive

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!