Divide and Conquer

Three Steps

  • Divide the problem into multiple subproblems in smaller size.

  • Conquer the subproblems recursively; set a straight forward way for base case.

  • Combine the solutions to the subproblems into the solution for the original problem.

Master Theorem

  • recurrence relation: T(n)=aT(nb)+f(n)T(n)=a T\left({\frac{n}{b}}\right)+f(n)

  • a>1a>1the number of subproblems in the recursion

  • b>1b>1the factor by which the subproblem size is reduced in each recursive call

  • andffis asymptotically positive (positive for sufficiently largenn)

  • base case: T(c)T(c)is a constant whenccis a constant

  • compare nlogban^{\log_{b}{a}}with f(n)f(n)and this will lead to three regimes (>, =, <)

    • in recursion tree, compare root with leaves and see which part dominates

  • more reference: CLRS book page 94

Example Code: Merge Sort

Last updated

Was this helpful?