Recursion
Recursive
A recursive algorithm or function is the one that calls itself to solve a smaller version of its task. Terminating or base conditions are required.
Divide & Conquer
- Divide: Split a problem into smaller sub-problems
- Conquer: Solve a subset of smaller sub-problems recursively
- Combine: Combine the solutions to solve the initial problem
Examples:
Analysis
Recurrence relation
A mathematical equation that defines the overall cost of solving a problem in terms of the cost of solving its smaller sub-problems. It provides a way to express the time complexity of recursive algorithms.
3 common methods are used to solve a recurrence relation.
Substitution method
The form of the solution is guessed and verified by induction. Then the constants are solved for.
Iteration method
Aka. recursion-tree method. The recurrence is expanded step-by-step to identify patterns and dervice a solution. Good for generating guesses for substituion method but can be unreliable.
Master theorem
For decreasing functions
Applies to below form:
Where and .
Here are the cases:
- If then
- If then
- If then
For dividing functions
Applies to recurrences of the below form:
Where , and .
Here are the cases:
- If then .
- If and then .
- If and then .
- If and then .
- If and then .
- If and then .