ManOrInfinity Posts

June 10, 2021 / Infinity Fitness


Recursion Tree Method (Iteration Method):

Recursion tree method is a graphical representation of an iteration method, which is the form of a tree where each level nodes are expended. It is useful when the divide and conquer algorithm is used. In recursion tree, each root and child represent the cost of a single problem.

 

Example 1– Solve the following recurrence relation using recursion tree method (iteration method).

T(n) = 2T(n/2) + n

Explanation: 

In the above diagram, the recurrence relation divide into two sub problems. Understood? If not, then look at the recursive equation and diagram simultaneously. The diagram showing the actual meaning of the equation.

Again,

 

Now again,

 

T(n) = n + n + n + …. + (log2n times)

Why log2n times? Because the height of the tree is log2n.

T(n) = n*log2n

T(n) = O(nlog2n) 

 

Example 2– Solve the following recurrence relation using recursion tree method (iteration method).

T(n) = 2T(n/2) + c; n>1

T(n) = c;       n=1

Explanation: 

Similarly as previous question-

 

T(n) = c + 2c + 4c + 8c + … + (log2n times)

T(n) = c[ 1 + 2 + 4 + 8 + … + (log2n times)]

Simply solve the G.P. inside the brackets.

T(n) = c[2log2n – 1]     [2log2n = nlog22 = n]

T(n) = c[n – 1]

T(n) = O(n) 

 

Example 3– Solve the following recurrence relation using recursion tree method (iteration method).

T(n) = T(n/3) + T(2n/3) + n

Explanation: 

This question is somewhat different from above question. Why?

Because, in this question, the recurrence relation is divided into two different parts. In the above questions, the recurrence relation is divided into two equal parts.

Let’s see how to solve this one-

It is clear that the height of the tree is not balanced here. The height of the leftmost branch of the tree is log3n and the height of the rightmost branch of the tree is log3/2n

So, the longest path is rightmost one, and its length is log3/2n

Hence, 

T(n) = n + n + n + …. + (log3/2n times)

Why log3/2n times? Because the height of the rightmost tree is log3/2n.

T(n) = n*log3/2n

T(n) = O(nlog3/2n)

 

June 10, 2021 / Infinity Fitness

Recurrence Relation: A recurrence relation is a equation or inequality that express a function in terms of its value on a smaller input. How to solve these recurrences? any idea? So, there are three ways to solve the recurrence relations. 1. Substitution Method 2. Recursion Tree Method (Iteration Method) 3.…

June 9, 2021 / Infinity Fitness
June 9, 2021 / Infinity Fitness
June 9, 2021 / Infinity Fitness

If you lose your smartphone charger and you are a price-conscious person, most probably you will pick a duplicate charger or a fake charger. Fake chargers are cheap initially but they could be expensive over time. As much as this applies to the charger it also applies to the charging…

June 8, 2021 / Infinity Fitness

  1. What is recursion?  Any function(in your program) which calls itself is called a ‘recursive function’ and this process is known as ‘recursion‘. A recursive function solves a problem by calling a copy of itself, by working on a smaller distinct of the actual problem. The recursion step must…

June 7, 2021 / Infinity Fitness

1. What is C programming? or When was the C language invented? or When was the C language created? or When the C programming language was developed? or Where the C programming language was developed? C is a programming language that is used for general purpose. It is a procedural,…

June 4, 2021 / Infinity Fitness

When it comes to pagination, the first solution that comes to our mind is fetching all the documents form MongoDB and then create a Javascript function to run on that data to do pagination. But there is a better way, you can use limit, sort and skip mongo function to…

June 2, 2021 / Infinity Fitness

While building a website sometimes you might need to run some specific code before the route changes and after the route changes in your NextJS app. The best use case is setting loader variables. A Page loader is simply animation or gif that you show to user until your page…

May 22, 2021 / Infinity Fitness

When I first bought this Samsung Galaxy Note 10 in January 2020, I was under the impression that I have to upgrade post 1.5 years as this is usually my upgrade cycle of any phone. But, since Samsung announced 3 years updates, This started to look more promising. Now, a…