In C language when a structure is referring or pointing to the structure of the same type, then it is called ‘self referential structure’. Example of a structure- Example of a self referential structure- Here, *link is a pointer to the structure of type ‘manu’. Hence, it is a self…
Category: Infinity Fitness
Everything you need to know about building muscles and maintaining a great overall health
Dynamic Memory Allocation: When we need to change the size of data structure at runtime, then we use dynamic memory allocation. C language provides some functions for dynamic memory allocation. There are 4 library functions in C language that are defined under the ‘stdlib.h’ header file to facilitate dynamic memory…
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)
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.…
Do you Know? In how many ways, you can express your algorithm? There are two ways to express your algorithms- 1. Iterative Algorithms 2. Recursive Algorithms Recursive algorithms can be converted into iterative algorithms and iterative algorithms can be converted into recursive algorithms, both have same in power. We will…
Do you know? How can you analyze the performance of an algorithm? Performance Analysis: Performance analysis of an algorithm is the process of calculating time and space required by that algorithm. There are three types of analysis- 1. Worst Case: In the worst case, an algorithm performs the maximum number…
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…
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…
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,…