Week 3: Divide and Conquer Flashcards
What is the Divide and Conquer Approach?
- Divide the problem into some subproblems, which are smaller instances of the same problem (D(n))
- Conquer the subproblems by solving them recursively, or if they are small enough just solve the subproblems normally (aT(n/b))
- Combine the solutions to the subproblems into the solution for the original problem (C(n))
T(n)=Θ(1) if n≤c
T(n) = D(n) + aT(n/b) + C(n) otherwise
What is Merge Sort?
Divide: Divide the n element sequence into two subsequces of n/2 elements each Θ(1)
Conquer: Sort the two subsequences recursively using merge sort 2T(n/2)
Combine: Merge the two sorted subsequences to produce the sorted answer Θ(n)
T(n)=Θ(1) if n=1
T(n) = Θ(1) + 2T(n/2) + Θ(n) otherwise
Merge Sort time complexity?
The merge step has a time complexity of O(n)
The sequence of size n will be divided log2(n) times
Overallm the time complexity of the merge sort algorithm is O(nlog2(n))
What is the Substitution method?
The substitution method is a method for solving recursive time complexity algorithms for T(n).
T(n) = {c n = 1
{2T(n/2) + cn n > 1
Base case: Show that the inequality holds for some n sufficiently small
Ind. Hyp: Assume that T(n) ≤ cn log n for c > 0 holds for all positive m < n, in particular for m = n/2
Step case: use the Ind. Hyp to replace part of the time complexity formula
How to make good guesses
Guessing a solution requires both experience and creativity:
- Heuristics can help become a good guesser.
- Recursion trees can help with this as well
Start with loose upper and lower bounds and allow them to converge
Algebraic manipulation can be used to make a recurrance simular to more familiar ones