Summary: Divide and Conquer
Everything on one page, to review the chapter before moving on.
Key ideas
- Divide and conquer divides a problem into smaller subproblems of the same kind, solves them recursively and combines their solutions (The Divide and Conquer Scheme).
- The number of subproblems should be small, and the subproblems independent. When they repeat, dynamic programming is the right tool.
- With a single subproblem () the process is a reduction, often better written as a loop. Recursion uses stack memory proportional to its depth.
- Count the recursive calls that are executed, not the ones written (Analyzing Divide and Conquer).
- Discarding the subproblems that cannot contain the answer is what makes binary search and Quickselect fast (Majority, Mode and Median).
Solving the recurrences
With subproblems and a cost of dividing and combining in (Analyzing Divide and Conquer):
| Scheme | Condition | Complexity |
|---|---|---|
| Division () | ||
| Division () | ||
| Division () | ||
| Subtraction () | ||
| Subtraction () | , exponential |
At a glance
| Problem | Algorithm | Complexity | Lesson |
|---|---|---|---|
| Factorial | Subtraction, | Numbers and Searches | |
| Search in a sorted vector | Binary search | Numbers and Searches | |
| Fibonacci | Naive recursion | , exponential | Numbers and Searches |
| Fibonacci | Iterative / matrix power | / | Numbers and Searches |
| Greatest common divisor | Euclid's algorithm | Linear in the number of digits | Numbers and Searches |
| Sorting | Mergesort | Classic Problems | |
| Sorting | Quicksort | best, worst | Classic Problems |
| Towers of Hanoi | Subtraction, | Classic Problems | |
| Large integers | Karatsuba | Classic Problems | |
| Tennis tournament | Division, | Classic Problems | |
| Tromino puzzle ( = side) | Division, | Classic Problems | |
| Majority element, mode | Sorting first | Majority, Mode and Median | |
| Median | Quickselect | average, worst | Majority, Mode and Median |
When to use it
- The problem splits into independent subproblems of the same kind, and their solutions combine cheaply.
- Avoid it when the subproblems overlap (naive Fibonacci): use dynamic programming instead.
- Prefer division schemes () to subtraction schemes () with several subproblems, which are exponential.
Saved only in your browser
Questions, or something wrong?
Errors, suggestions and public questions open a form on GitHub, visible to everyone.