Difference between Divide and Conquer Algo and Dynamic Programming

后端 未结 9 1922
一个人的身影
一个人的身影 2020-12-12 08:41

What is the difference between Divide and Conquer Algorithms and Dynamic Programming Algorithms? How are the two terms different? I do not understand the difference between

相关标签:
9条回答
  • 2020-12-12 09:11

    I assume you have already read Wikipedia and other academic resources on this, so I won't recycle any of that information. I must also caveat that I am not a computer science expert by any means, but I'll share my two cents on my understanding of these topics...

    Dynamic Programming

    Breaks the problem down into discrete subproblems. The recursive algorithm for the Fibonacci sequence is an example of Dynamic Programming, because it solves for fib(n) by first solving for fib(n-1). In order to solve the original problem, it solves a different problem.

    Divide and Conquer

    These algorithms typically solve similar pieces of the problem, and then put them together at the end. Mergesort is a classic example of divide and conquer. The main difference between this example and the Fibonacci example is that in a mergesort, the division can (theoretically) be arbitrary, and no matter how you slice it up, you are still merging and sorting. The same amount of work has to be done to mergesort the array, no matter how you divide it up. Solving for fib(52) requires more steps than solving for fib(2).

    0 讨论(0)
  • 2020-12-12 09:20

    I think of Divide & Conquer as an recursive approach and Dynamic Programming as table filling.

    For example, Merge Sort is a Divide & Conquer algorithm, as in each step, you split the array into two halves, recursively call Merge Sort upon the two halves and then merge them.

    Knapsack is a Dynamic Programming algorithm as you are filling a table representing optimal solutions to subproblems of the overall knapsack. Each entry in the table corresponds to the maximum value you can carry in a bag of weight w given items 1-j.

    0 讨论(0)
  • 2020-12-12 09:20
    • Divide and Conquer
      • They broke into non-overlapping sub-problems
      • Example: factorial numbers i.e. fact(n) = n*fact(n-1)
    fact(5) = 5* fact(4) = 5 * (4 * fact(3))= 5 * 4 * (3 *fact(2))= 5 * 4 * 3 * 2 * (fact(1))
    

    As we can see above, no fact(x) is repeated so factorial has non overlapping problems.

    • Dynamic Programming
      • They Broke into overlapping sub-problems
      • Example: Fibonacci numbers i.e. fib(n) = fib(n-1) + fib(n-2)
    fib(5) = fib(4) + fib(3) = (fib(3)+fib(2)) + (fib(2)+fib(1))
    

    As we can see above, fib(4) and fib(3) both use fib(2). similarly so many fib(x) gets repeated. that's why Fibonacci has overlapping sub-problems.

    • As a result of the repetition of sub-problem in DP, we can keep such results in a table and save computation effort. this is called as memoization
    0 讨论(0)
  • 2020-12-12 09:22

    Divide and Conquer

    Divide and Conquer works by dividing the problem into sub-problems, conquer each sub-problem recursively and combine these solutions.

    Dynamic Programming

    Dynamic Programming is a technique for solving problems with overlapping subproblems. Each sub-problem is solved only once and the result of each sub-problem is stored in a table ( generally implemented as an array or a hash table) for future references. These sub-solutions may be used to obtain the original solution and the technique of storing the sub-problem solutions is known as memoization.

    You may think of DP = recursion + re-use

    A classic example to understand the difference would be to see both these approaches towards obtaining the nth fibonacci number. Check this material from MIT.


    Divide and Conquer approach Divide and Conquer approach

    Dynamic Programming Approach enter image description here

    0 讨论(0)
  • 2020-12-12 09:23

    The other difference between divide and conquer and dynamic programming could be:

    Divide and conquer:

    1. Does more work on the sub-problems and hence has more time consumption.
    2. In divide and conquer the sub-problems are independent of each other.

    Dynamic programming:

    1. Solves the sub-problems only once and then stores it in the table.
    2. In dynamic programming the sub-problem are not independent.
    0 讨论(0)
  • 2020-12-12 09:26

    Divide and Conquer

    • In this problem is solved in following three steps: 1. Divide - Dividing into number of sub-problems 2. Conquer - Conquering by solving sub-problems recursively 3. Combine - Combining sub-problem solutions to get original problem's solution
    • Recursive approach
    • Top Down technique
    • Example: Merge Sort

    Dynamic Programming

    • In this the problem is solved in following steps: 1. Defining structure of optimal solution 2. Defines value of optimal solutions repeatedly. 3. Obtaining values of optimal solution in bottom-up fashion 4. Getting final optimal solution from obtained values
    • Non-Recursive
    • Bottom Up Technique
    • Example: Strassen's Matrix Multiplication
    0 讨论(0)
提交回复
热议问题