Skip to content

Solving Occurrences

The objective of solving occurrences is to find out what the Tworst(n) is for a certain algorithm.

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:

T(n)={1 if n=1T(n)=aT(n/b)+cf(n) if n>1

or

T(n)={1 if n=1T(n)=aT(n/b)+jT(n/k)+cf(n) if n>1

Where

  • Inside the occurrences T() we have diminishing functions (i.e. division or subtractions)
  • a,b,c,j,kR are constants.

0 - Some notation first

NotationMeaningNotes
T(n)Tworst(n), the total complexity
iT(n)Count of internal nodes for T(n)
fT(n)Count of leaf nodes for T(n)
iSome level i of the tree
af(i)Count of nodes at a level iIt can be written differently
wiWeight of a level iSum of the weights/complexity of the nodes of a level i
n/biDimension of a subproblem at level iIf not unique, use the one to achieve the longest path from root
cf(n/bi)Contribution of one call at a level i to T(n)If not unique, find the maximum cost of a level O(Max(wi))
n/bi=1i=logb(n)Total count of levelsCan be replaced with O( longest path from root )
ag(i)cf(n/bi)Cost of the nodes at a level iOnly 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 [02] levels

Start by drawing a tree

  • Draw the root as cf(n) (remember that f(n) can be seen as everything that is not some occurrence kT(j(n)))
    • Ex: f(n)=n2 and c=3, would be 3(n2) is the root
  • Draw the branches
    • Depending on what the constants a and b are, we need to draw a+b sub-branches for each internal node
    • Obviously we need to apply the functions g(n) and h(n) respectively.
      • ex: a=2 and g(n)=n/2 would result in 2 sub-nodes c(f(g(n)))=3((n/2)2)
      • ex: a=3 and h(n)=n/4 would result in 3 sub-nodes c(f(h(n)))=3((n/4)2)
      • We would have 5 branches for each node, until we reach the case n=1
c++//CaseT(n)=g(n)+h(n)+n2//f(n)=n2//Onlya+b=1+1subnodes[n]2//Level0/ [g(n)]2[h(n)]2//Level1/ / [g(g(n))]2[h(g(n))]2[g(h(n))]2[h(h(n))]2//Level2T(1)T(1)...T(1)//Bottomlevel

2 - Create a table for the analysis

We want to create a table for further analysis:

  • The number of the level, i
    • Just enumerate the levels
  • The total count of nodes for that level: How many nodes can we see at a level i?
    • Try to find a pattern and express it as a function of the level with the constant preceding the occurrence, i.e. ai
    • if it is not possible we might need to look at the height later
    • ex: 2 for binary trees (which are usually the case) 20=1, 21=2, 22=4,
  • Weight of the level, the sum of the weight of each node at a level i
    • Just sum the weight of the nodes and express it a function of the level multiplied by f(n), h(i)f(n)
    • This is important since we discover how fast the tree goes to base case.
Level iTotal Nodes at level iWeight of level i
0aisum 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 T(n) (Case 2)
    • No, we can identify the weight of one level and the number of levels to get our answer (Case 1).
  • Identify a diminishing factor n/bi: does the weight of a sub-problem change homogeneously?
    • Yes, the weight of a node is n/bi at some level i (Case 2).
    • No, we need to guess the maximum cost of a level (Case 3)
  • Find the height i: when is the tree going to reach its leaves?
    • If there's a diminishing factor, then we can set n/bi=1 and find the count of levels i (Case 2)
    • Else we need to find the longest path from the root to the leaves (Case 3)

4 - Guess the case

CaseCritical pointsFormula
1 - Balanced Tree, with non-changing weightf(n/bi) is the same, wi does not changeT(n)=aT(n/b)+cf(n)
2 - Balanced Tree, with homogeneously decreasing weightf(n/bi) is the same, wi decreasesT(n)=aT(n/b)+cf(n)
3 - Unbalanced Tree, with heterogeneously decreasing weightf(n/bi) is NOT the same, wi decreasesT(n)=aT(n/b)+jT(nk)+cf(n)

And the follow the instructions

{Case 1T(n)=k=0iwi=wiiCase 2T(n)=k=0i1akcf(n/bk)+Θ(aiT(1))Case 3T(n)=O(Max{Cost of a level}Length longest path)

5.1 - Case 1

  1. Find the diminishing factor
    1. n/bi
  2. Set it to 1 and find i
    1. n/bi=1i=logb(n)
  3. Find the number of levels
    1. i+1, since the levels start from 0,1,,logb(n)
  4. Find the weight of a level wi
    1. Sum the single contributions to the total complexity, for a level
T(n)=k=1i+1wi=wii+1

5.2 - Case 2

  1. Find the diminishing factor
    1. n/bi
  2. Set it to 1 and find i
    1. n/bi=1i=logb(n)
  3. Find the number of leaves
    1. ai
T(n)= Cost of internal nodes + Cost of leaves =k=0i1akcf(n/bk)+Θ(aiT(1))

Remember that, for geometric series:

j=0ixi=xi+11x1

and when the summation is infinite and |x|<1, we have the infinite decreasing geometric series:

j=0infxj=11x

Which we can use since we allow ourselves for a small amount of sloppiness

5.3 - Case 3

  1. Find the maximum cost of a level
  2. 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.
T(n)=O(Max(Cost of a level)Length longest path)

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:

T(n)=Tsplit(n)+Tmerge(n)+Tsolve(n)

Where:

  • Tsplit(n) and Tmerge(n) are not recursive.
    • f(n)=Tsplit(n)+Tmerge(n) and f(n)0
  • Tsolve(n) can be expressed as summation of the time needed to solve the sub-problems
    • T(ni) for i=1,,k which is equal to af(n/b)

Theorem 4.1 - Master Theorem

Let a1 and b>1 be constants, let f(n) be a function, and let T(n) be defined on the non-negative integers by the recurrence (nN)

T(n)=aT(n/b)+f(n)

where we interpret n/b to mean either n/b or n/b. Then T(n) has the following asymptotic bounds:

  • If f(n)=O(nlogbaϵ), for some constant ϵ>0, then T(n)=Θ(nlogba)
  • If f(n)=Θ(nlogba), then T(n)=Θ(nlogbalog(n))
  • If f(n)=Ω(nlogba+ϵ), for some constant ϵ>0, and if af(n/b)cf(n) for some constant c<1 and all sufficiently large n, then T(n)=Θ(f(n))

Explaining the conditions

We need to express the T(n) of the algorithm we want to analyze through the following form:

T(n)=aT(n/b)+f(n)

Plus, the following conditions must stand true:

  • a1, is a constant expressing the number of occurrences
  • n/b, the dimension of the sub-problem is constant
  • b>1
  • f(n)0 for n sufficiently large

If the conditions are met, then we can add the following notation:

  • d=logb(a)
  • g(n)=nd=nlogb(a)

Through the theorem we asymptotically compare g(n) and f(n) to discover T(n). For this we identify the right case:

CaseConditionAsymptotic NotationProcedureSolution
1f(n)ndf(n)=O(ndϵ) with ϵ>0Find ϵT(n)=Θ(nd)
2f(n)ndf(n)=Θ(nd)None neededT(n)=Θ(ndlog(n))
3f(n)ndf(n)=Ω(nd+ϵ) with ϵ>0Find the only ϵ, then find the c such that c<1 for n suffic. large af(n/b)cf(n)T(n)=Θ(f(n))

Master Theorem Demonstration

We know that for a divide-et-impera algorithm, we can rewrite its complexity as:

T(n)=Tsplit(n)+Tmerge(n)+Tsolve(n)=f(n)+Tsolve(n)=f(n)+aT(n/b)

And if the conditions mentioned above are met, we can focus on comparing f(n) with g(n)=nd.

But why do we want to do this?

This is what the demonstration is for.

1 - Rewrite T(n)

Through the occurrences tree we try to rewrite T(n) in an explicit way, that is non-recursive.

mastertheoremdem

And we want to focus on finding out:

  • a1, the number of sub-problems of dimension n/b with b>1
  • ai, the number of nodes at a level i
  • n/bi, the dimensionality of the sub-problems at a level i
  • f(n/bi) with i0, the contribution of one call at a level i to the total complexity
  • aif(n/bi), the complexity of all nodes at a level i

We can define the total complexity as the sum of the complexity of all levels:

T(n)=Total complexity of all levels=Tlvl 1+Tlvl 2++Tlvl i=i=0aif(n/bi)

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 n/bi=1 since when sub-problem reaches the size of 1T(1).
n/bi=1bi=nlogb(n)=i

We assume that n is a power of b, else we would need to use the ceil integer value of i

iN, represents the levels of the tree. We can now explicitly write T(n) in a non recursive way:

T(n)=i=0logb(n)aif(n/bi)

Since ai is the number of nodes at a level i, the number of leaves is exactly alogb(n) which, by the property of the logarithms for the change of the base (prop 1),

loga(b)=logc(b)1logc(a)

and the property of inversion (prop 2)

loga(b)=1logb(a)

changes its power

logb(n)prop1loga(n)1loga(b)prop2loga(n)logb(a)

and becomes

alogb(n)=aloga(n)logb(a)=(aloga(n))logb(a)defnlogb(a)defnd

We conclude that

ai=nd is the number of leaves 

Now, if we want to compute the total recursive calls/nodes, we can use a geometric series:

i=0logb(n)ai

by the property of the geometric series:

i=0kqi=qk+11q1

we obtain

i=0logb(n)aipropalogb(n)+11a1=alogb(n)a1a1=nda1a1Θ(nd)

We conclude that nd is not just the number of leaves, but is the factor that dictates the asymptotic growth of the total complexity, since it is also the total count of nodes/recursive calls.

This precisely why, through the master theorem we compare

  • The number of recursive calls nd=g(n)
  • And the time used for splitting and merging Tsplit + merge=f(n)

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:

T(n)=i=0logb(n)aif(n/bi)

2.1 - Case 1

Hypothesis

2.2 - Case 2

Hypothesis

2.3 - Case 3

Hypothesis

Example

Credits