A divide-and-conquer algorithm takes its input, makes several smaller-problems, solves those recursively, and then combines the sub-results into the result for the full original problem.
The canonical example: Mergesort.
Another example: Given n points in the plane, find the minimum distance between points. (See Rosen Section 6.3, Example 12. Comp360 deals with problems like this.)
[To think about: Compare to: Separate the points into thirds by x-coord; find the min distance in left-two-thirds, and in right-two-thirds. Does this work? What is base case? How to worry about points w/ same x-coord?]
What is the (worst-case) running-time of this algorithm, for input size n?
T(n) = ??
It's not clear what a closed-form answer is.
But we can easily find a recursive formulation:
T(n) = 2T(n/2) + 7n; T(2)=1.
[Draw out tree, assuming n = 16, say.
In blue, write the amount of work done at that node;
the total time for a node is that blue plus all the blue underneath.
Add up all the blue: observe that by rows-of-tree, it's 7n on each row.
There are log(n) rows, if n a perfect power of 2.
Thus, total 7n⋅log(n) + #leaves⋅1;
#leaves is 2height = 2(lg(n)) = n.
Total work: O(n log n).]
Exercise: show that this big-oh still holds for n not being a perfect power of 2. Not too hard, if we assume that more input never takes less time. Then just bound T(n) with T(nearest-power-of-2-bigger-than-n) (which is less than 2n).
Now, consider other divide-and-conquer algorithms (ignoring floor/ceiling):
Master th'm for recurrence relations arising from divide-and-conquer:
For f(n)=a⋅f(n/b)+c⋅nd,
Does this cover all types of recurrences? No; consider:
breaking an n-digit password.
Brute force:
T(n)= 10 T(n-1)
Clearly the closed-form answer is 10n.
What of Fibonacci?
{ fib(n-1)+fib(n-2) if n ≥ 2 fib(n) ≡ { 1 if n = 1 { 0 if n = 0[Consider: different initial conditions; running the process backwards.]
This technique generalizes:
Will not cover:
the nonhomogenous version, where the recurrence relation
includes a non-recursive term.
In general, this involves a lot of voodoo to find one solution.
However: Punchline: if you can find one sol'n h(n) to the recurrence, then every sol'n must be of the form g(n)+h(n), where g(n) is a sol'n to the corresponding homogenous form.
Example: the game Lights Out. Consider all moves that take an empty board back to an empty board; this is a homogeneous solution; there might be several such solutions. Now consider a game where there are some lights on; if you can find one way to turn them off, then all-possible-solutions must be your one particular-solution, joined with any single homogenous solution. (Or put differently, there aren't any other solutions which aren't just your original solution plus a homogenous solution.)
(Note that we haven't talked about how to find one solution. For Lights Out: Subtle hint: systems of boolean equations…)
When you cover ODE's in engineering-math classes, you'll use the exact same approaches as here, except that instead of f(n-k) you use the kth derivative f(k)(n) — it's just the analog analog.
[ian: This should take less than one session, letting me get ahead.] [an error occurred while processing this directive] [an error occurred while processing this directive]