Divide and Conquer
Divide and conquer
Divide and conquer breaks the original problem into k subproblems and solves them separately. If they remain too large, split them into k smaller subproblems again. Continue recursively until the problems are small enough to solve easily.
Combine the solutions to small problems into solutions to larger ones, working from the bottom up until the original problem is solved.
Four conditions make divide and conquer suitable:
- The problem becomes easy to solve once its size is sufficiently small.
- The problem can be divided into smaller problems of the same kind: it has the required substructure.
- Solutions to the smaller problems can be combined into a solution to the original problem.
- The subproblems are independent, without shared subproblems. Otherwise, divide and conquer may repeatedly solve the same problems. It can still be used, but dynamic programming is often preferable.
Experience suggests keeping subproblems approximately equal in size. Dividing a problem into k balanced parts is effective; this balancing strategy almost always works better than unequal partitions.
KNOWLEDGE QUIZ
Binary search
A bag contains 16 coins, exactly one of which is counterfeit and lighter than the genuine coins. Find it using an instrument that compares two groups of coins, reporting whether their weights are equal and which group is lighter.
Split the 16 coins equally into A and B. Weigh A against B. If A is lighter, solve the subproblem in A; otherwise, solve the subproblem in B.
Each iteration of binary search halves the remaining array. In the worst case there are iterations, with work inside each iteration. The total worst-case time complexity is therefore .
Find the counterfeit: retain one half
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Trace 32 coins
Number 32 coins from 1 to 32, with exactly one lighter coin. Assume coin 22 is counterfeit so you can check the trace. Split candidates equally by number and retain the lighter side.
- Record both groups, the balance result, and retained candidates each round.
- Give the number of weighings and explain its relation to candidate count.