Exploring the Cost of a Recursion Tree

In summary: We could have used any other power of 2, but $2^i$ is typically the most convenient choice. In summary, the recursion tree for the given function has a cost per level of $\left( \frac{7}{8} \right)^{i-1}n$ and a leaf per level of $3^i$. To find the total cost, we add $3^D$ to the sum of costs of each level, where $D=\log_2 n$ is found from the base case $\frac{n}{2^D}=1$.
  • #1
evinda
Gold Member
MHB
3,836
0
Hi! (Wave)

According to my notes:

$$T(n)=T\left(\frac{n}{2} \right)+T\left(\frac{n}{4} \right)+T\left(\frac{n}{8} \right)+n$$

Th recursion tree is this:

https://www.physicsforums.com/attachments/3626

Cost per level $i \to \left( \frac{7}{8} \right)^{i-1}n, i \geq 1$

Leaf per level $i \to 3^i, i \geq 0$

$$\text{Total Cost=} 3^D O(1)+ \sum_{i=1}^D \left ( \frac{7}{8}\right)^{i-1} n$$

$$\frac{n}{2^D}=1 \Rightarrow D=\log_2 n$$

Could you explain me why we have to add $3^D$ at the sum of costs of the levels, in order to find the total cost? :confused:Also, why do we find $D$, from $\frac{n}{2^i}=1$ and not for example from $\frac{n}{4^i}=1$ or from $\frac{n}{8^i}=1$ ? (Worried)
 
Physics news on Phys.org
  • #2
The $3^D$ is added to the total cost because there are three leaf nodes for every level, so for every level that you go down in the recursion tree, you have three more leaf nodes to account for. The reason why we use $\frac{n}{2^i}=1$ and not something else is because that's the base case of the recursion, i.e. when we reach the leaves of the tree. This is the point where we stop recursing and start adding up the costs. We use $2^i$ because that's the number of leaf nodes we have at each level, i.e. $\frac{n}{2^i}=1$ tells us that we have reached a level with one leaf node.
 

FAQ: Exploring the Cost of a Recursion Tree

What is a recursion tree?

A recursion tree is a visual representation of the recursive calls made in a recursive algorithm. It starts with the initial call at the top of the tree and branches out to each subsequent recursive call until the base case is reached.

How is the cost of a recursion tree determined?

The cost of a recursion tree is determined by analyzing the number of levels in the tree and the number of nodes at each level. The cost can also be affected by the complexity of the recursive function and any additional operations performed within each recursive call.

What is the significance of exploring the cost of a recursion tree?

Exploring the cost of a recursion tree is important for understanding the efficiency and performance of a recursive algorithm. It allows us to analyze and compare different algorithms and make improvements to optimize their performance.

How can we calculate the time complexity of a recursion tree?

The time complexity of a recursion tree can be calculated by multiplying the number of levels in the tree by the number of nodes at each level. This can also be expressed as a mathematical function, such as O(2^n) for a binary recursion tree.

Can the cost of a recursion tree be reduced?

Yes, the cost of a recursion tree can be reduced by optimizing the recursive algorithm, such as eliminating unnecessary recursive calls or finding more efficient ways to solve the problem. Additionally, converting a recursive algorithm to an iterative one can also reduce the cost of the recursion tree.

Back
Top