# consider the following recurrence relation by expanding the relation using the recurrence tree method; find the following. what is the cost of the last level of the tree? what is the amount of work at the other levels?

### Mohammed

Guys, does anyone know the answer?

get consider the following recurrence relation by expanding the relation using the recurrence tree method; find the following. what is the cost of the last level of the tree? what is the amount of work at the other levels? from screen.

## Lecture 20: Recursion Trees and the Master Method

## Lecture 20: Recursion Trees and the Master Method

Lecture 20: Recursion Trees and the Master Method Recursion Trees

A recursion tree is useful for visualizing what happens when a recurrence is iterated. It diagrams the tree of recursive calls and the amount of work done at each call.

For instance, consider the recurrence

.

The recursion tree for this recurrence has the following form:

In this case, it is straightforward to sum across each row of the tree to obtain the total work done at a given level:

This a geometric series, thus in the limit the sum is . The depth of the tree in this case does not really matter; the amount of work at each level is decreasing so quickly that the total is only a constant factor more than the root.

Recursion trees can be useful for gaining intuition about the closed form of a recurrence, but they are not a proof (and in fact it is easy to get the wrong answer with a recursion tree, as is the case with any method that includes ''...'' kinds of reasoning). As we saw last time, a good way of establishing a closed form for a recurrence is to make an educated guess and then prove by induction that your guess is indeed a solution. Recurrence trees can be a good method of guessing.

Let's consider another example,

.

Expanding out the first few levels, the recurrence tree is:

Note that the tree here is not balanced: the longest path is the rightmost one, and its length is . Hence our guess for the closed form of this recurrence is .

## The Master Method

The master method is a cookbook method for solving recurrences. Although it cannot solve all recurrences, it is nevertheless very handy for dealing with many recurrences seen in practice. Suppose you have a recurrence of the form

,

where and are arbitrary constants and is some function of . This recurrence would arise in the analysis of a recursive algorithm that for large inputs of size breaks the input up into subproblems each of size , recursively solves the subproblems, then recombines the results. The work to split the problem into subproblems and recombine the results is .

We can visualize this as a recurrence tree, where the nodes in the tree have a branching factor of . The top node has work associated with it, the next level has work associated with each of nodes, the next level has work associated with each of nodes, and so on. At the leaves are the base case corresponding to some . The tree has levels, so the total number of leaves is = .

The total time taken is just the sum of the time taken at each level. The time taken at the -th level is , and the total time is the sum of this quantity as ranges from 0 to , plus the time taken at the leaves, which is constant for each leaf times the number of leaves, or . Thus

.

What this sum looks like depends on how the asymptotic growth of compares to the asymptotic growth of the number of leaves. There are three cases:

Case 1: () is O(log − ε). Since the leaves grow faster than , asymptotically all of the work is done at the leaves, so () is Θ(nlog ).

Case 2: () is Θ(log). The leaves grow at the same rate as , so the same order of work is done at every level of the tree. The tree has O(log n) levels, times the work done on one level, yielding () is Θ(nlog log ).

Case 3: () is Ω(log + ε). In this case grows faster than the number of leaves, which means that asymptotically the total amount of work is dominated by the work done at the root node. For the upper bound, we also need an extra smoothness condition on in this case, namely that (/) ≤ () for some constant and large . In this case () is Θ(()).

As mentioned, the master method does not always apply. For example, the second example considered above, where the subproblem sizes are unequal, is not covered by the master method.

Let's look at a few examples where the master method does apply.

**Example 1**Consider the recurrence

.

For this recurrence, there are subproblems, each dividing the input by , and the work done on each call is . Thus log is , and is for , and Case 1 applies. Thus is .

**Example 2**Consider the recurrence

.

For this recurrence, there are again subproblems, each dividing the input by , but now the work done on each call is . Again log is , and is thus , so Case 2 applies. Thus is . Note that increasing the work on each recursive call from linear to quadratic has increased the overall asymptotic running time only by a logarithmic factor.

**Example 3**Consider the recurrence

.

For this recurrence, there are again subproblems, each dividing the input by , but now the work done on each call is . Again log is , and is thus for . Moreover, for , so Case 3 applies. Thus is .

### Example: Yet Another Sorting Algorithm

The following function sorts the first two-thirds of a list, then the second two-thirds, then the first two-thirds again:

let rec sort3 (a : 'a list) : 'a list =

## DAA Recursion Tree Method

DAA Recursion Tree Method with daa tutorial, introduction, Algorithm, Asymptotic Analysis, Control Structure, Recurrence, Master Method, Recursion Tree Method, Sorting Algorithm, Bubble Sort, Selection Sort, Insertion Sort, Binary Search, Merge Sort, Counting Sort, etc.

## Recursion Tree Method

1. Recursion Tree Method is a pictorial representation of an iteration method which is in the form of a tree where at each level nodes are expanded.

2. In general, we consider the second term in recurrence as root.

3. It is useful when the divide & Conquer algorithm is used.

4. It is sometimes difficult to come up with a good guess. In Recursion tree, each root and child represents the cost of a single subproblem.

5. We sum the costs within each of the levels of the tree to obtain a set of pre-level costs and then sum all pre-level costs to determine the total cost of all levels of the recursion.

6. A Recursion Tree is best used to generate a good guess, which can be verified by the Substitution Method.

**Example 1**

Consider T (n) = 2T + n2

We have to obtain the asymptotic bound using recursion tree method.

**Solution:**The Recursion tree for the above recurrence is

**Example 2:**Consider the following recurrence

T (n) = 4T +n

Obtain the asymptotic bound using recursion tree method.

**Solution:**The recursion trees for the above recurrence

**Example 3:**Consider the following recurrence

Obtain the asymptotic bound using recursion tree method.

**Solution:**The given Recurrence has the following recursion tree

When we add the values across the levels of the recursion trees, we get a value of n for every level. The longest path from the root to leaf is

← Prev Next →

## How to solve time complexity Recurrence Relations using Recursion Tree method?

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## How to solve time complexity Recurrence Relations using Recursion Tree method?

Difficulty Level : Medium

Last Updated : 24 Nov, 2021

Read Courses Discuss Practice Video

The Recursion Tree Method is a way of solving recurrence relations. In this method, a recurrence relation is converted into recursive trees. Each node represents the cost incurred at various levels of recursion. To find the total cost, costs of all levels are summed up.

**Steps to solve recurrence relation using recursion tree method:**

Draw a recursive tree for given recurrence relation

Calculate** **the cost at each level and count the total no of levels in the recursion tree.

Count the total number of nodes in the last level and calculate the cost of the last level

Sum up the cost of all the levels in the recursive tree

**Let us see how to solve these recurrence relations with the help of some examples:**

**Question 1:**T(n) = 2T(n/2) + c

**Solution:**

**Step 1:**Draw a recursive tree

Recursion Tree

**Step 2:**Calculate the work done or cost at each level and count total no of levels in recursion tree

Recursive Tree with each level cost

**Count the total number of levels –**

Choose the longest path from root node to leaf node

n/20 -→ n/21 -→ n/22 -→ ……… -→ n/2k

Size of problem at last level = n/2k

At last level size of problem becomes 1

n/2k = 1 2k = n

**k = log2(n)**

**Total no of levels in recursive tree = k +1 = log2(n) + 1**

**Step 3:**Count total number of nodes in the last level and calculate cost of last level

No. of nodes at level 0 = 20 = 1

No. of nodes at level 1 = 21 = 2

………………………………………………………

No. of nodes at level log2(n) = 2log2(n) = nlog2(2) = n

Cost of sub problems at level log2(n) (last level) = nxT(1) = nx1 = n

**Step 4:**Sum up the cost all the levels in recursive tree

T(n) = c + 2c + 4c + —- + (no. of levels-1) times + last level cost

= c + 2c + 4c + —- + log2(n) times + Θ(n)

= c(1 + 2 + 4 + —- + log2(n) times) + Θ(n)

1 + 2 + 4 + —– + log2(n) times –> 20 + 21 + 22 + —– + log2(n) times –> Geometric Progression(G.P.)

= c(n) + Θ(n) Thus,

**Question 2: T(n) = T(n/10) + T(9n/10) + n**

**Solution:**

**Step 1:**Draw a recursive tree

Recursive Tree

**Step 2:**Calculate the work done or cost at each level and count total no of levels in recursion tree

Recursion Tree with each level cost

**Count the total number of levels –**

Choose the longest path from root node to leaf node

(9/10)0n –> (9/10)1n –> (9/10)2n –> ……… –> (9/10)kn

Size of problem at last level = (9/10)kn

At last level size of problem becomes 1

(9/10)kn = 1 (9/10)k = 1/n

**k = log10/9(n)**

**Total no of levels in recursive tree = k +1 = log10/9(n) + 1**

**Step 3:**Count total number of nodes in the last level and calculate cost of last level

No. of nodes at level 0 = 20 = 1

No. of nodes at level 1 = 21 = 2

………………………………………………………

No. of nodes at level log10/9(n) = 2log10/9(n) = nlog10/9(2)

Cost of sub problems at level log2(n) (last level) = nlog10/9(2) x T(1) = nlog10/9(2) x 1 = nlog10/9(2)

**Step 4:**Sum up the cost all the levels in recursive tree

T(n) = n + n + n + —- + (no. of levels – 1) times + last level cost

= n + n + n + —- + log10/9(n) times + Θ(nlog10/9(2))

= nlog10/9(n) + Θ(nlog10/9(2))

Thus, Recommended

Solve DSA problems on GfG Practice.

Solve Problems

Guys, does anyone know the answer?