Solving Occurrences
The objective of solving occurrences is to find out what the
Induction
Example
Recursion Tree
Although you can use the substitution method to provide a succinct proof that a solution to a recurrence is correct, you might have trouble coming up with a good guess. In a recursion tree, each node represents the cost of a single sub-problem somewhere in the set of recursive function invocations. We sum the costs within each level of the tree to obtain a set of per-level costs, and then we sum all the per-level costs to determine the total cost of all levels of the recursion. [1]
We focus on formulas such as:
or
Where
- Inside the occurrences
we have diminishing functions (i.e. division or subtractions) are constants.
0 - Some notation first
| Notation | Meaning | Notes |
|---|---|---|
| Count of internal nodes for | ||
| Count of leaf nodes for | ||
| Some level | ||
| Count of nodes at a level | It can be written differently | |
| Weight of a level | Sum of the weights/complexity of the nodes of a level | |
| Dimension of a subproblem at level | If not unique, use the one to achieve the longest path from root | |
| Contribution of one call at a level | If not unique, find the maximum cost of a level | |
| Total count of levels | Can be replaced with | |
| Cost of the nodes at a level | Only if tree is balanced and the terms are unique |
Do not get confused with the formulas above, we change variables based on the case.
1 - Construct a tree with levels
Start by drawing a tree
- Draw the root as
(remember that can be seen as everything that is not some occurrence ) - Ex:
and , would be is the root
- Ex:
- Draw the branches
- Depending on what the constants
and are, we need to draw sub-branches for each internal node - Obviously we need to apply the functions
and respectively. - ex:
and would result in 2 sub-nodes - ex:
and would result in 3 sub-nodes - We would have 5 branches for each node, until we reach the case
- ex:
- Depending on what the constants
2 - Create a table for the analysis
We want to create a table for further analysis:
- The number of the level,
- Just enumerate the levels
- The total count of nodes for that level: How many nodes can we see at a level
? - Try to find a pattern and express it as a function of the level with the constant preceding the occurrence, i.e.
- if it is not possible we might need to look at the height later
- ex: 2 for binary trees (which are usually the case)
, ,
- Try to find a pattern and express it as a function of the level with the constant preceding the occurrence, i.e.
- Weight of the level, the sum of the weight of each node at a level
- Just sum the weight of the nodes and express it a function of the level multiplied by
, - This is important since we discover how fast the tree goes to base case.
- Just sum the weight of the nodes and express it a function of the level multiplied by
| Level | Total Nodes at level | Weight of level |
|---|---|---|
| 0 | sum the weights on the same level | |
| 1 | ||
| 2 | ||
| 3 | ||
| ... | ... | ... |
3 - Analyze the balance of the tree
By now we should have noticed some aspects of our tree.
- Look at the table: does the weight of a level change?
- Yes, the weight diminishes so we need to sum all the weights of the nodes to find our
(Case 2) - No, we can identify the weight of one level and the number of levels to get our answer (Case 1).
- Yes, the weight diminishes so we need to sum all the weights of the nodes to find our
- Identify a diminishing factor
: does the weight of a sub-problem change homogeneously? - Yes, the weight of a node is
at some level (Case 2). - No, we need to guess the maximum cost of a level (Case 3)
- Yes, the weight of a node is
- Find the height
: when is the tree going to reach its leaves? - If there's a diminishing factor, then we can set
and find the count of levels (Case 2) - Else we need to find the longest path from the root to the leaves (Case 3)
- If there's a diminishing factor, then we can set
4 - Guess the case
| Case | Critical points | Formula |
|---|---|---|
| 1 - Balanced Tree, with non-changing weight | ||
| 2 - Balanced Tree, with homogeneously decreasing weight | ||
| 3 - Unbalanced Tree, with heterogeneously decreasing weight |
And the follow the instructions
5.1 - Case 1
- Find the diminishing factor
- Set it to 1 and find
- Find the number of levels
, since the levels start from
- Find the weight of a level
- Sum the single contributions to the total complexity, for a level
5.2 - Case 2
- Find the diminishing factor
- Set it to 1 and find
- Find the number of leaves
Remember that, for geometric series:
and when the summation is infinite and
Which we can use since we allow ourselves for a small amount of sloppiness
5.3 - Case 3
- Find the maximum cost of a level
- Find the length of the longest path from the root , which is given by the branch which diminishes the slowest among all the other ones.
Example
Iteration Method
Example
Substitution Method
Example
Master Theorem
The master theorem is a powerful way to solve the occurrences of divide et impera algorithms:
Where:
and are not recursive. and
can be expressed as summation of the time needed to solve the sub-problems which is equal to
Theorem 4.1 - Master Theorem
Let
where we interpret
- If
, for some constant , then - If
, then - If
, for some constant , and if for some constant and all sufficiently large , then
Explaining the conditions
We need to express the
Plus, the following conditions must stand true:
, is a constant expressing the number of occurrences , the dimension of the sub-problem is constant for n sufficiently large
If the conditions are met, then we can add the following notation:
Through the theorem we asymptotically compare
| Case | Condition | Asymptotic Notation | Procedure | Solution |
|---|---|---|---|---|
| 1 | Find | |||
| 2 | None needed | |||
| 3 | Find the only |
Master Theorem Demonstration
We know that for a divide-et-impera algorithm, we can rewrite its complexity as:
And if the conditions mentioned above are met, we can focus on comparing
But why do we want to do this?
This is what the demonstration is for.
1 - Rewrite
Through the occurrences tree we try to rewrite
And we want to focus on finding out:
, the number of sub-problems of dimension with , the number of nodes at a level , the dimensionality of the sub-problems at a level , the contribution of one call at a level to the total complexity , the complexity of all nodes at a level
We can define the total complexity as the sum of the complexity of all levels:
The summation lacks a boundary condition. We need to find it.
- When does the summation stop?
- When the tree reaches its leaves
- Then we need to set
since when sub-problem reaches the size of .
We assume that
Since
and the property of inversion (prop 2)
changes its power
and becomes
We conclude that
Now, if we want to compute the total recursive calls/nodes, we can use a geometric series:
by the property of the geometric series:
we obtain
We conclude that
This precisely why, through the master theorem we compare
- The number of recursive calls
- And the time used for splitting and merging
We want to check which one of the terms is asymptotically dictating the complexity of the algorithm
2 - Find the right case
Remember that we found the following:
2.1 - Case 1
Hypothesis
2.2 - Case 2
Hypothesis
2.3 - Case 3
Hypothesis
Example
Credits
- [1] Introduction to Algorithms, Third Edition
- Publisher: The MIT Press
- Authors: Cormen, Thomas H. and Leiserson, Charles E. and Rivest, Ronald L. and Stein, Clifford