Calculate the Depth of the recursive algorithm

In summary, the conversation discusses a recursive algorithm for calculating the sum of elements in an array and the task of finding the depth of recursion for a specific array with elements that are an expression of 2. The individual seeking help is advised to run through the algorithm by hand and use the resulting pattern to determine the general rule for depth.
  • #1
tsgiannis
1
0

Homework Statement


Hello to everybody
I have this homework and my main issue is that is cannot find any bibliography to read that does what my homework requires.
The problem goes like
We have this recursive algorithm that calculates the sum of an array elements from array element k to array element n

procedure sum(A, left, right)
if left ? right then
if left = right then return(A
);
else return(0);
mid := [(left + right) / 2];
x := sum(A, left, mid);
y := sum(A, mid+1, right);
return(x + y);
The problem goes like this
If the array A has n elements that are an expression of 2 (like 8 = 2^3) find the depth of recursion for the call of the procedre sum(A,1,n)

I don't ask for the solution but just a helping hand to find some relative material.

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
I've never seen that language before, so I don't know what is going on in your code at all. If I were you, I'd probably run through the algorithm by hand for n = 1, 2, 4 and by then, a pattern will probably develop. Then, use that pattern to derive the general rule for depth.
 

What is a recursive algorithm?

A recursive algorithm is a type of algorithm that calls itself repeatedly until a certain condition is met. It is commonly used in programming to solve problems that can be broken down into smaller, simpler versions of the same problem.

What is the purpose of calculating the depth of a recursive algorithm?

The depth of a recursive algorithm tells us how many times the algorithm calls itself before reaching the base case. This information can help us understand the efficiency and complexity of the algorithm, and can aid in debugging and optimizing the algorithm.

How do you calculate the depth of a recursive algorithm?

The depth of a recursive algorithm can be calculated by counting the number of times the algorithm calls itself. This can be done manually by tracing the execution of the algorithm, or by using a debugger in a programming environment.

What is the difference between depth and height in a recursive algorithm?

The depth of a recursive algorithm is the number of times it calls itself, while the height is the maximum depth of any branch in the algorithm. In other words, the height is the longest path from the root of the recursive calls to the base case.

What factors can affect the depth of a recursive algorithm?

The depth of a recursive algorithm can be affected by the input size, the structure of the problem, and the implementation of the algorithm. A more complex problem or a poorly optimized implementation can result in a higher depth for the algorithm.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
11
Views
900
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
942
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
Back
Top