Divide and conquer vs dynamic programming in short
Divide and conquer and dynamic programming are both ways to solve a large problem by working on smaller ones. They differ in how those smaller problems relate to each other. In divide and conquer, the pieces are independent: each one is solved once, and its answer is combined with the others. In dynamic programming, the pieces overlap, so the same smaller answer is needed again and again. It is worth saving the result the first time it is computed.
How divide and conquer works
A divide-and-conquer algorithm usually follows three steps:
- Divide the problem into smaller subproblems of the same kind.
- Conquer each subproblem. This is often done by applying the same method recursively, until the pieces are small enough to solve directly.
- Combine the answers to the subproblems into an answer for the whole problem.
Merge sort is the standard example. It splits a list into two halves, sorts each half the same way, and then merges the two sorted halves into one sorted list. The two halves never overlap, so no sorting work is repeated. A standard merge sort is also stable, which means equal items keep their original order. Checking which sorting algorithms are stable shows how that differs across methods.
The running time follows from the structure. Each split halves the input, and merging takes time roughly proportional to the length of the list. For that reason, merge sort runs in about n log n time, which is the usual bound taught for it. Quicksort, binary search, Strassen’s matrix multiplication and the fast Fourier transform are also built on this pattern.
Quicksort is often described as an in-place algorithm, since it rearranges items inside the array. A typical merge sort needs extra space for the merge step. Asking what is the most efficient sorting algorithm has no single answer, because the choice depends on the data and the memory available.
Recursive algorithm examples follow this pattern closely. Once you can spot the split and combine steps, the idea carries over to new problems.
How dynamic programming works
Dynamic programming is useful when a problem has two properties:
- Optimal substructure: an optimal answer to the whole problem can be built from optimal answers to smaller parts.
- Overlapping subproblems: the same smaller parts come up many times while solving the problem.
Rather than recomputing those parts, a dynamic programming solution stores each answer and looks it up when it is needed again. There are two common styles:
- Memoization (top-down): write the recursive solution, and save each result the first time it is computed.
- Tabulation (bottom-up): fill a table starting from the smallest subproblems, and build up to the full problem, usually without recursion.
Fibonacci numbers show the difference clearly. A plain recursive version computes the same smaller values over and over, and its running time grows exponentially as the input grows. If each Fibonacci number is stored once it is computed, the running time drops to roughly linear, since each value is computed one time.
Other classic dynamic programming problems include the longest common subsequence, edit distance, the 0/1 knapsack problem, and the coin change problem for finding the fewest coins. Brute force algorithm examples often try every possible answer, which can take far too long. Dynamic programming avoids much of that work by reusing answers to the parts it has already solved.
Key differences
| Divide and conquer | Dynamic programming | |
|---|---|---|
| Subproblems | Independent, and do not overlap | Overlapping, and repeated |
| Reuse of answers | Not needed | Stored in a memo or table |
| Typical shape | Split, solve, then combine | Recurrence, often filled as a table |
| Typical examples | Merge sort, binary search, quicksort | Fibonacci numbers, knapsack, edit distance |
| Common analysis | Recurrence relations, such as the master theorem | Number of distinct subproblems times the work for each |
How to choose between them
Start by asking whether the subproblems overlap.
- If each piece is unique, divide and conquer is usually the right shape. Sorting the two halves of an array is an example, since the halves never share work.
- If the same piece appears again, dynamic programming is usually the better fit. Counting the paths through a grid is an example. Each cell’s count depends on the cells above and to the left, and those counts get reused.
Treat this as a guide, not a proof. Overlap is necessary for dynamic programming to save work, but the problem must also have optimal substructure before a dynamic programming solution is correct.
Common misconceptions
- Recursion alone does not make an algorithm divide and conquer. A function that shrinks a problem by one step, such as computing a factorial, is recursive. It does not split the work into pieces and combine their answers.
- Dynamic programming is not always faster in practice. The table or memo can use a lot of memory, and some problems have so many distinct subproblems that the approach is still slow.
- Dynamic programming is not the only way to solve optimization problems. For some problems, a simpler greedy approach gives the right answer, and it is usually faster when it works.
Summary
Divide and conquer splits a problem into independent parts and combines their answers. Dynamic programming splits a problem into overlapping parts and saves each answer so it is never computed twice. If the pieces are independent, divide and conquer is the natural choice. If the same pieces keep coming back, dynamic programming is worth considering.