Brute Force:
- 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 creating the most efficient and least costly route for visiting multiple test cases and returning home.
- 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 slow down the overall product or software.
Dynamic Programming:
- The dynamic programming approach is similar to divide and conquer in breaking down the problem into smaller and yet smaller possible sub-problems.
- 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 similar 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.
- 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.
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 finds only one outcome. |
Time Complexity | O(xy), where x is the number of char to be found and y is the size of the input. | O(x), where x is the number of unique subproblems |
Example | Selection sort | Floyd Warshell |
Iterations | The number of iterations is more | The number of iterations is less |
Efficiency | It is less efficient | It is more efficient |
Uses | When we have less amount of data | When we have a huge amount of data |