Home/Blog/Programming/Solving recurrence relations using geometric series
Home/Blog/Programming/Solving recurrence relations using geometric series

Solving recurrence relations using geometric series

14 min read
Apr 24, 2024
content
The importance of solving recurrences
Geometric series: A fundamental concept
Examples: Sum of the series with a specific common ratio rrr
Case 1: Recurrence with an increasing geometric series
Calculation for the number of levels
In general
Case 2: Recurrence with decreasing geometric series
Calculation for the number of levels
Case 3: Decreasing problem sizes by equal cost on each level
Calculation for the number of levels
Smart method: In action
Logarithm’s base disappears in Big O notation
Conclusion

Recurrence relations are fundamental in computer science and mathematics, especially when analyzing algorithms’ time complexity. They help us understand the performance of algorithms in terms of their input size. One common technique for solving recurrences is the Master Theorem, which provides a framework for classifying and solving recurrences. However, learners often find it challenging to memorize the three cases of the Master Theorem and apply them correctly. This blog explores an alternative approach to solving recurrences using geometric series and summation bounds, eliminating the need to memorize the Master Theorem.

The importance of solving recurrences#

Before we delve into our alternative approach, let’s discuss why solving recurrences is crucial for a computer scientist. Analyzing algorithms’ time complexity helps determine our code’s efficiency. This information is vital for designing and optimizing algorithms, making it a fundamental skill for computer scientists and programmers. Many algorithms for solving various problems involve recursive strategies, and thinking of them in terms of recursion-based solutions is very natural. For a computer scientist, analyzing the bounds of these algorithms will require solving recurrences, i.e., at each step of the recurrence, we need to accumulate the cost associated with each recursive call. Generally, we calculate the cost at each level and then accumulate all these costs. Well, look at this in a while.

Here’s the general recurrence that we’re going to solve:

This can be interpreted with the following recursion tree:

canvasAnimation-image
1 / 14

Notice: We’re only looking at the recurrence cases that are solved through the Master Theorem (i.e., they can be represented in some form of geometrical series).

Geometric series: A fundamental concept#

To understand how geometric series can simplify recurrence analysis, we first have to understand the concept of the geometric series. A geometric series is a sequence of numbers in which each term after the first is found by multiplying the previous term by a fixed, nonzero number called the common ratio. The formula for the sum of a geometric series is as follows:

Sn=ar0+ar1+ar2+ar3++arn2+arn1.=arn1+arn2+ar3+ar2+ar1+ar0.\begin{align} \nonumber S_n &= ar^0 + ar^1 + ar^2 + ar^3 + \ldots + ar^{n-2} + ar^{n-1}. \\ \nonumber &= ar^{n-1} + ar^{n-2} \ldots + ar^3 + ar^2 + ar^1 +ar^0. \\ \end{align}

The two series above have a closed summation captured by the following formula:

Sn=a(1rn)1r.\begin{align} \boxed{S_n = \frac{a(1-r^n)}{1-r}.} \end{align}

where,

  • SnS_n is the sum of the series with n terms.
  • aa is the first term.
  • rr is the common ratio.
  • nn is the number of terms.

There are two key insights from the geometric series formula:

  1. Increasing geometric series: An increasing geometric series is bounded by the last term.
  2. Decreasing geometric series: A decreasing geometric series is bounded by the first term.

You’re encouraged to look at the proofs of these two properties. These two properties form the basis of our new approach to solving recurrences.

Before we go into the details of solving recurrences, let’s look at some example series and establish bounds for the series using the two properties mentioned above.

Examples: Sum of the series with a specific common ratio rrr#

Let’s look at the following example:

S1=T+T/2+T/4++8+4+2+1.S2=1+2+4+8++T/4+T/2+T.\begin{align} \nonumber S_1 &= T + T / 2 + T/4 + \ldots + 8 + 4 + 2 + 1. \\ \nonumber S_2 &= 1+2+4+8+\ldots + T/4 + T/2 + T. \\ \end{align}

  • S1S_1 is an example of a decreasing geometric series where TT is any value bigger then 11, with a common ratio of 12\frac{1}{2}, and it’s bounded by O(T)O(T).

  • Similarly, S2S_2 is an example of an increasing geometric series with a common ratio of 2{2}, and it’s bounded by O(T)O(T).

Let’s explore some further examples.

Example: Let’s explore the following example and see if it’s bound using the geometric series facts presented above.

S(n)=1+2+4+8+16+...+n+2×n+4×n+...+n×n.S(n) = 1+2+4+8+16+...+n+2 \times n + 4 \times n + ... + n\times n.

This is an increasing geometric series that’s bounded by the last term, i.e., S(n)O(n2)S(n) \approx O(n^2).

Based on the geometric series, most recurrences where the recurrence relationship expands by geometric sizes can be easily bounded asymptotically using the properties mentioned above. We’ll study them in greater detail with the help of three cases in the remainder of this blog.

Case 1: Recurrence with an increasing geometric series#

In the first case, when we expand the recurrence and sum up all the costs associated at each level, it emerges as an increasing geometric series from the first to the last level. In such a case, the asymptotic bound is just the cost of the last level.

Example: Now let us look at this bound in the form of recurrences:

Recursion tree
Recursion tree

If we open the recurrence above mathematically, it will be something like this (for reference, look at the recurrence tree above):

T(n)=4T(n/2)+O(1),=4(4T(n/4)+1)+O(1),=4(4(4T(n/8)+1)+1)+1,=4(4(4(...(1))...+1)+1)+1,=4levels+4levels1+4levels2+...+43+42+4+1.\begin{align} \nonumber T(n) &= 4 T(n/2) + O(1), \\ \nonumber &= 4 ( 4 T(n/4) + 1 ) + O(1), \\ \nonumber &= 4 ( 4 (4 T(n/8) + 1) + 1 ) + 1, \\ \nonumber &= 4(4(4(...(1))...+1)+1)+1, \\ &= 4^{levels} + 4^{levels-1} + 4^{levels-2} + ... + 4^3 + 4^2 + 4 + 1. \\ \end{align}

If we rewrite the last equation in increasing level order, i.e., level 1’s cost first, then the second, then the third, and so on, then the rearranged equation will be as follow:

T(n)=1+4+42+43+...+4levels2+4levels1+4levels.\begin{align} T(n) &= 1 + 4 + 4^2 + 4^3 + ... + 4^{levels-2} + 4^{levels-1} + 4^{levels}. \\ \end{align}

This last equation clearly depicts an increasing geometric series that is bounded by the last term. This results in the asymptotic complexity of O(4levels)O(4^{levels}).

Calculation for the number of levels#

As we go down the recursion tree, the size of nn decreases by the common ratio of 12\frac{1}{2} therefore, the total number of levels can be seen as the size of the sequence:

n,n2,n4,n8,,n2k1.n, \frac{n}{2}, \frac{n}{4}, \frac{n}{8}, \ldots, \frac{n}{2^k}\approx 1.

Here kk is the number of levels, which can be calculated by solving for kk for the last term (which is 1, the base case):

n2k=1    n=2k    log2n=log22k.\frac{n}{2^k} = 1 \implies n = 2^k \implies \log_2 n = log_2 2^k.

k=log2n.k = log_2 n.

Therefore, the number of levels is equal to log2n\log_2 n. So,

4levels    4log2n    nlog24=n2    (alogbc=clogba).4^{levels} \implies 4^{\log_2 n} \implies n^{\log_2 4} = n^2 \space \space \space \space \because (a^{\log_b c} = c^{\log_b a}).

Therefore, the asymptotic complexity of the recurrence is O(n2)O(n^2).

In general#

In case the recurrence yields an increasing geometric series, the recurrence will be bounded above the last level, i.e.,

T(n)=aT(n/b)+nc.T(n) = a T(n/b) + n^c.

T(n)T(n) will be bounded by O(alogbn)    O(nlogba)O(a^{\log_b n}) \implies O(n^{log_b a}).

Note: It’s interesting that the cost equals the number of nodes at the last level.

Case 2: Recurrence with decreasing geometric series#

In the second case, when we expand the recurrence and sum up all the costs associated with each level, it emerges as a decreasing geometric series when we look from the first level to the last level. In such a case, the asymptotic bound is just the cost at the first level.

Example: Let’s look at another recurrence:

Let’s look at the recurrence tree of the above-mentioned recurrence:

Case 2: recursion tree
Case 2: recursion tree

If we open the recurrence above mathematically, it will be something like this (for reference, look at the recurrence tree above):

T(n)=2T(n/2)+n2=2(2T(n/4)+(n2)2)+n2=2(2(2T(n/8)+(n4)2)+(n2)2)+n2=2(2(2(...(1))...+(n4)2)+(n2)2)+n2=2levels+2levels1+2levels2+...+n28+n24+n22+n2.\begin{align} \nonumber T(n) &= 2 T(n/2) + n^2 \\ \nonumber &= 2 ( 2 T(n/4) + (\frac{n}{2})^2 ) + n^2 \\ \nonumber &= 2 ( 2 (2 T(n/8) + (\frac{n}{4})^2) + (\frac{n}{2})^2 ) + n^2 \\ \nonumber &= 2(2(2(...(1))...+(\frac{n}{4})^2)+(\frac{n}{2})^2)+n^2 \\ &= 2^{levels} + 2^{levels-1} + 2^{levels-2} + ... + \frac{n^2}{8} + \frac{n^2}{4} + \frac{n^2}{2} + n^2. \\ \end{align}

Calculation for the number of levels#

As we go down the recursion tree, the size of nn decreases by a factor of 22. Therefore, the total number of levels is equal to log2n\log_2 n. Therefore,

2levels    2log2n    nlog22=n    (alogbc=clogba).2^{levels} \implies 2^{\log_2 n} \implies n^{\log_2 2} = n \space \space \space \space \because (a^{\log_b c} = c^{\log_b a}).

If we rewrite the last equation in increasing level order, i.e., level 1’s cost first, then the 2nd, then the third, and so on, then the rearranged equation will be:

n2+n22+n24+n28+...+2n+n.n^2 + \frac{n^2}{2} + \frac{n^2}{4} + \frac{n^2}{8} + ... + 2n + n.

This is just a decreasing geometric series ending on nn, Therefore, the total bound will be O(n2)O(n^2), bounded by the first term.

Case 3: Decreasing problem sizes by equal cost on each level#

In the third case, as we expand the recurrence, the cost remains the same on each level. Consequently, the geometric reduction in size constrains the recurrence to halt after a number of levels corresponding to logbn\log_b n, where bb is the geometric common ratio with which each recurrence diminishes in size by calling to its next level. In such a case, the asymptotic bound is just the cost at the first level (or any level) multiplied by the number of levels.

Example: Let’s look at another recurrence:

Let’s look at the recurrence tree of the recurrence above:

Case 3: recursion tree
Case 3: recursion tree

If we open the recurrence above mathematically, it will be something like this (for reference, look at the recurrence tree above):

T(n)=3T(n/3)+n=3(3T(n/9)+n/3)+n=3(3(3T(n/27)+n/9)+n/3)+n=3(3(3(...(1))...+n/9)+n/3)+n=3levels+3×3levels1+32×3levels2+...+27×n27+9×n9+3×n3+n×1.\begin{align} \nonumber T(n) &= 3T(n/3) + n \\ \nonumber &= 3 ( 3T(n/9) + n/3 ) + n \\ \nonumber &= 3 ( 3(3 T(n/27) + n/9) + n/3 ) + n \\ \nonumber &= 3(3(3(...(1))...+n/9)+n/3)+n \\ &= 3^{levels} + 3 \times 3^{levels-1} + 3^2 \times 3^{levels-2} + ... + 27\times \frac{n}{27} + 9\times \frac{n}{9} + 3\times \frac{n}{3} + n \times 1. \\ \end{align}

If we rearrange the last equation such that level 1 is the first, level 2 is the second, and so on, the equation becomes:

T(n)=n×1+3×n3+9×n9++32×3levels2+3×3levels1+1×3levels.\begin{align} \nonumber T(n) &= n\times 1 + 3\times \frac{n}{3} + 9\times \frac{n}{9} +\cdots + 3^2 \times 3^{levels-2} + 3 \times 3^{levels-1} + 1\times 3^{levels}. \end{align}

T(n)=n+n+n++n.\boxed{ \begin{align} \nonumber T(n) &= n+n+n+\cdots+n.\end{align}}

Calculation for the number of levels#

As we go down the recursion tree, the size of nn decreases by a factor of 33. Therefore, the total number of levels is equal to log3n\log_3 n. So, because every term in the series equation above is nn and there are O(logn)O(\log n) levels, the total time complexity bound will be O(nlogn)O(n \log n).

Smart method: In action#

Now, in such cases of recurrences, there’s is no need to perform the complicated conversion from recurrence to series. All we need to do is look at the first level expansion of the recursion tree and see which case it fit into:

  • If it’s an increasing geometric series, then the last terms bound it.

  • If it’s a decreasing geometric series, then the first terms bound it.

  • If it’s equal on all levels, then the total bound is:

Let’s look at some solved examples in the slides below.

canvasAnimation-image
1 / 3

In some cases, it’s harder to see, on paper, whether the two immediate levels follow an increasing, decreasing, or equal pattern. In such cases, looking at the last level helps a lot. Here, in the following examples, we’ve demonstrated how to solve such recurrences.

canvasAnimation-image
1 / 5

It’s important to remember the following property:

Logarithm’s base disappears in Big O notation#

The logarithm function has an interesting property that, makes the base of the log\log disappear in Big O notation:

logcn=logbnlogbc.\log_c n = \frac{\log_b n}{\log_b c}.

Notice, in the above equation that, logbc\log_b c is always just a constant. Therefore, in Big O notation:

logcn=logbnconstant    O(logcn)O(logbn).\log_c n = \frac{\log_b n}{constant} \implies O(\log_c n) \approx O(\log_b n).

This is the reason why computer scientists seldom write the base of the logarithm.

Conclusion#

In this blog, we explored an easy way of solving recurrences. Notably, there’s no requirement to memorize intricate formulas, such as the Master Theorem, for resolving these recurrences. What’s particularly advantageous is that understanding this method not only simplifies our approach to problem-solving but also serves as a reminder that geometric series feature prominently in algorithmic designs. Consequently, we can regard them as a fundamental element in algorithmic thinking, particularly for pruning and searching strategies. For more in-depth insights, feel free to delve into the details provided in the accompanying blog.

You can also check out these exciting Educative courses:

Grokking Coding Interview Patterns in C++

Cover
Grokking the Coding Interview Patterns in C++

With thousands of potential questions to account for, preparing for the coding interview can feel like an impossible challenge. Yet with a strategic approach, coding interview prep doesn’t have to take more than a few weeks. Stop drilling endless sets of practice problems, and prepare more efficiently by learning coding interview patterns. This course teaches you the underlying patterns behind common coding interview questions. By learning these essential patterns, you will be able to unpack and answer any problem the right way — just by assessing the problem statement. This approach was created by FAANG hiring managers to help you prepare for the typical rounds of interviews at major tech companies like Apple, Google, Meta, Microsoft, and Amazon. Before long, you will have the skills you need to unlock even the most challenging questions, grok the coding interview, and level up your career with confidence. This course is also available in JavaScript, Java, Python, C# and Go — with more coming soon!

85hrs
Intermediate
345 Challenges
346 Quizzes

Visual Introduction To Algorithms

Cover
A Visual Introduction to Algorithms

Learn introductory computer science algorithms, including searching, sorting, recursion, and graph theory through a combination of articles, visualizations, quizzes, and coding challenges. Implement Challenges in Java, Python, C++ or Javascript.

14hrs
Beginner
18 Challenges
2 Quizzes

Beginner to Advanced Computing and Logic Building

Cover
Beginner to Advanced Computing and Logic Building

This course focuses on building logic to solve simple to advanced coding problems. You will learn advanced computational techniques to solve real-world problems. The solutions for most problems are curated in a way that each solution is followed by a more efficient one, enabling you to devise the most efficient algorithm from the start. The course starts with the fundamentals of variables, loops, and arrays, and progresses to more advanced concepts such as creating games like Gomoku, Word Search, and Game of Life using the divide and conquer approach. The course also includes a built-in debugger to help you understand the code by setting breakpoints and analyzing variable values. Concepts will be demonstrated via animations and illustrations to engage your interest and give you hands-on experience in building interactive games. By the end of the course, you will be well-prepared to tackle coding challenges commonly found in FAANG interviews.

46hrs
Beginner
31 Challenges
44 Quizzes

Written By:
Sarfraz Raza
Join 2.5 million developers at
Explore the catalog

Free Resources