Difference between Divide and Conquer Algo and Dynamic Programming

后端 未结 9 1924
一个人的身影
一个人的身影 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: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

提交回复
热议问题