Ch4: Divide and Conquer Flashcards
For the substitution method of solving recurrences, what technique can you try if you hit a snag in the math?
Revise the guess by subtracting a lower-order term (this will be <= what you try to prove, therefore T(n) <= new guess <= desired guess implies T(n) = O(desired))
3 steps of divide and conquer
DIVIDE the problem into subproblems that are smaller instances of the same problem, CONQUER the subproblems by solving them recursively (or trivially), COMBINE the subproblem solutions into the solution for the original problem
Case when subproblems are large enough to solve recursively
Recursive case
Case when subproblems are small enough to no longer recurse
Base case
Recurrence
An equation or inequality that describes a function in terms of its value on smaller inputs
3 methods of solving recurrences
Substitution method, recursion-tree method, master method
When we state and solve recurrences, we often omit…
floors, ceilings, and boundary conditions (assuming the latter are O(1))
Steps for the substitution method
1) Guess the form of the solution (we do this sometimes w/ output of recursion-tree method!) 2) Use mathematical induction to find the constants and show that the solution works
In a recursion tree, each node represents…
the cost of a single subproblem somewhere in the set of recursive function calls
Using the recursion tree method, we get the total T(n) by…
Summing the cost at each level, then summing all the level costs to get the total cost T(n)
A recursion tree is best used to…
generate a good guess which you can then verify using the substitution method
You can use a recursion tree as a direct proof of a solution to a recurrence if…
you are very careful when drawing out the recursion tree and summing the costs
How do you determine the recursion tree height (h) in terms of n?
Create an equation describing the subproblem size at a level i in terms of n: n[i] = f(n, i). Then solve the equation for the level i when the subproblem size = 1 (at the leaves of the tree). This is the tree height h
What is the key trick to solving an “unbalanced” recursion tree - one where the subproblems do not have equal sizes?
Determine which subproblem size leads you down the longest simple path from the root node to a leaf (the biggest subproblem). Since the tree is not complete, this longest tree height is an upper bound we can use, since we know the actual total cost will be smaller than that of a complete tree