Difference between Brute Force and Dynamic Programming
- It gives a solution to a problem by using the most straightforward method. However, it is typically not a very optimal solution or one that is flexible for future changes, but it gets the job done.
- The proverbial brute force programming example is trying all optimal solutions for reaching the final answer.
- Brute force programming tests every possible routing combination; whereas other mathematical algorithms obtain the results more quickly when the number of test cases is large.
- Brute force techniques are not generally used in the industrial field because they are not optimal in terms of space and time and slow downs the overall product or software.
- The dynamic programming approach is similar to divide and conquer in breaking down the problem into smaller and yet smaller possible sub-problems. In simple words, dynamic programming is an optimization over simple recursion.
- But unlike divide and conquer, these sub-problems are not solved independently.
- Rather, the results of these smaller sub-problems are remembered and used for similarity or overlapping.
Different ways of using Dynamic Programming:
- Top-Down: Start solving the given problem by breaking it down. If we see that the problem has been solved already, then just return the saved answer. If it has not been solved, solve it and save the answer. This approach is also known as Memoization.
- Bottom-Up: Analyze the problem and see the order in which the sub-problems are solved and start solving from the trivial subproblem, up to the given problem. In this process, it is guaranteed that the subproblems are solved before solving the problem. This approach is also known as Tabulation.
Difference between Brute Force and Dynamic Programming: The difference between these two approaches is mentioned clearly in the following table.
|Parameters of Comparison||Brute Force||Dynamic Programming|
|Methodology||It finds all the possible outcomes of a given problem.||It also finds all the possible outcomes, but avoids recomputation by storing|
solutions of the subproblems.
|Time Complexity||It could be anything, sometimes even in exponential terms.||It helps us optimize the brute force approach, sometimes exponential terms are improved to polynomial terms(ex. factorial program).|
|Iterations||The number of iterations is more||The number of iterations is less(in terms of n)|
|Efficiency||It is less efficient||It is more efficient|
|Storage||Generally requires no extra space for storing results of sub-problems.||It requires extra space for storing the solutions to the sub-problems, which could be further used when required.|