Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between back tracking and dynamic programming

I heard the only difference between dynamic programming and back tracking is DP allows overlapping of sub problems, e.g.

fib(n) = fib(n-1) + fib (n-2) 

Is it right? Are there any other differences?

Also, I would like know some common problems solved using these techniques.

like image 390
brett Avatar asked Aug 28 '10 23:08

brett


People also ask

Is backtracking used in dynamic programming?

In contrast, dynamic programming is used to solve the optimization problem, but backtracking is not used to solve the optimization problems.

Should I learn backtracking before dynamic programming?

To start with Dynamic programming you must first know how the backtracking/ recursion works exactly. You can also say that it is a pre-requisite of learning dp.

What is backtracking in programming?

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.

What is the difference between backtracking and recursion?

Difference between Recursion and Backtracking: In recursion, the function calls itself until it reaches a base case. In backtracking, we use recursion to explore all the possibilities until we get the best result for the problem.


1 Answers

There are two typical implementations of Dynamic Programming approach: bottom-to-top and top-to-bottom.

Top-to-bottom Dynamic Programming is nothing else than ordinary recursion, enhanced with memorizing the solutions for intermediate sub-problems. When a given sub-problem arises second (third, fourth...) time, it is not solved from scratch, but instead the previously memorized solution is used right away. This technique is known under the name memoization (no 'r' before 'i').

This is actually what your example with Fibonacci sequence is supposed to illustrate. Just use the recursive formula for Fibonacci sequence, but build the table of fib(i) values along the way, and you get a Top-to-bottom DP algorithm for this problem (so that, for example, if you need to calculate fib(5) second time, you get it from the table instead of calculating it again).

In Bottom-to-top Dynamic Programming the approach is also based on storing sub-solutions in memory, but they are solved in a different order (from smaller to bigger), and the resultant general structure of the algorithm is not recursive. LCS algorithm is a classic Bottom-to-top DP example.

Bottom-to-top DP algorithms are usually more efficient, but they are generally harder (and sometimes impossible) to build, since it is not always easy to predict which primitive sub-problems you are going to need to solve the whole original problem, and which path you have to take from small sub-problems to get to the final solution in the most efficient way.

like image 186
AnT Avatar answered Oct 07 '22 13:10

AnT