Solving sAllPairs(4) Recursively - 14 Calls

  • Thread starter Thread starter gfd43tg
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around understanding the recursive function sAllPairs and its behavior when called with the argument 4. Participants explore the output of the function as well as the number of recursive calls made during its execution. The conversation includes aspects of recursion, tree traversal, and counting function calls.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants calculate the output of sAllPairs(4) as 14 and discuss how to count the recursive calls made during its execution.
  • One participant expresses confusion about how to traverse the recursion tree, particularly regarding the order of processing nodes.
  • Another participant suggests starting the analysis from sAllPairs(0) and building up to sAllPairs(4), questioning how many calls each function generates.
  • Some participants argue about the correct way to count recursive calls, with differing interpretations of whether to include calls that return immediately due to base conditions.
  • There is a discussion about whether to include integers in the tree representation or to focus solely on function calls.
  • A participant proposes using print statements to visualize the function calls and better understand the recursion process.
  • Concerns are raised about the potential for traversing the same node multiple times in the recursion tree.

Areas of Agreement / Disagreement

Participants generally agree on the output of sAllPairs(4) being 14, but there is no consensus on the exact number of recursive calls made, with different interpretations leading to varying counts. The discussion remains unresolved regarding the proper method for counting calls and the traversal of the recursion tree.

Contextual Notes

Participants express uncertainty about the counting of recursive calls, particularly in relation to the base case and how to handle calls that do not lead to further recursion. There is also ambiguity in the representation of the recursion tree, with some suggesting a call stack might be a more appropriate model.

gfd43tg
Gold Member
Messages
949
Reaction score
48

Homework Statement


Suppose you are given the recursion function sAllPairs, given as
Code:
function output = sAllPairs(num)
if num > 0
    output = num + sAllPairs(num-1) + sAllPairs(num-2);
else
    output = 0;
end

a) what is the output of the command sAllPairs(4)?

b) How many recursive calls are made when issuing the command sAllPairs(4)?


Homework Equations





The Attempt at a Solution


I was able to calculate on paper and confirm that the answer is 14 for part (a), but I am confused once again about how to traverse the tree. I know for pre-order traversal you process the root, then the subtree from left to right. However, after I process the root, I get to the left subtree, and it's sibling is a root to another subtree. So when that happens, I am uncertain how to traverse through this thing.
ImageUploadedByPhysics Forums1407569013.095024.jpg
 
Physics news on Phys.org
I would start from num=0:
How many function calls does sAllPairs(0) produce?
Based on that result, how many function calls does sAllPairs(1) produce?
[...]
Based on that result, how many function calls does sAllPairs(4) produce?
 
Is my tree correct? I have doubts that the root would be sAllPairs(0)
 
##\bullet## So for num = 0, 0 + sAllPairs(-1) + sAllPairs(-2) = 0, it would make 0 recursive calls.

##\bullet## num = 1, you get 1 + sAllpairs(0) + sAllPairs(-1). Thus, 1+0+0. Does that mean it took no recursive calls, since it already knows that the last two are equal to zero due to the else condition?

##\bullet## Then for num = 2, 2 + sAllPairs(1) + sAllPairs(0), so 2+1+0. So it makes 1 recursive call

##\bullet## num = 3, 3 + sAllPairs(2) + sAllPairs(1), so 3+3+1 = 7. It makes 2 recursive calls

##\bullet## Finally, num = 4, 4+ sAllPairs(3) + sAllPairs(2), 4+7+3 = 14. It makes 2 recursive calls

for a total of 2+2+1 = 5 calls. I don''t know if this is the proper way to count calls though. If I am supposed to count any time I see sAllPairs for a num, then it would be 2 + 2 + 2 + 2 + 2 = 10, I just don't understand how one should count.
 
Last edited:
Maylis said:
##\bullet## So for num = 0, 0 + sAllPairs(-1) + sAllPairs(-2) = 0, it would make 0 recursive calls.
Be careful, you don't even make this calculation.

##\bullet## num = 1, you get 1 + sAllpairs(0) + sAllPairs(-1). Thus, 1+0+0. Does that mean it took no recursive calls, since it already knows that the last two are equal to zero due to the else condition?
You do call those sAllpairs functions here, so you get two calls.
Hmm, the list should start with -1 instead of 0, but -1 gives the same result as 0.

The numerical answer here does not matter, count the function calls instead of the function results. Calling a function that makes another 2 function calls gives 3 calls, for example.
 
My bad for the num = 0 case, I knew better than that!

But I am still confused for num = 1:4

They should all make 2 calls, thus a total of 8 calls, no?

num = 0, 0 calls
num = 1, 2 calls
num = 2, 2 calls
num = 3, 2 calls
num = 4, 2 calls
 
Maylis said:
They should all make 2 calls, thus a total of 8 calls, no?
Why?
num = 0, 0 calls
num = 1, 2 calls
Okay.
num = 2:
The calculation is "output = num + sAllPairs(num-1) + sAllPairs(num-2);"
Plugging in num we get:
"output = num + sAllPairs(1) + sAllPairs(0);"
So we 1[/color] call sAllPairs(1) which then makes 2[/color] more calls, and we 1[/color] call sAllPairs(0)which then makes 0[/color] more calls, for a total of 1+2+1+0=4[/color] recursive calls.

Can you work out 3 and 4?
 
You can get the computer to check your counting. Print out the value of "num" each time you call the function. Put the print statement right at the start of the function before it has done anything. Then figure out what happened from the output you get.

This is a good strategy for making sure you understand any new concept in computer programming. The computer is an ideal teacher. It never makes mistakes, and it never loses patience. Knowing how to "ask the computer the right sort of questions to find out what is going on" is a valuable skill to learn.
 
ImageUploadedByPhysics Forums1407622436.891085.jpg


I calculate 14 calls for num=4, and 8 calls for num=3
 
  • #10
ImageUploadedByPhysics Forums1407623530.644436.jpg


I wrote up the tree and if you count all the children for a given SAP(num), I found 8 and 14 for SAP(3) and SAP(4), respectively. Now, my question is, how is it traversing this tree?

The rule for pre-order traversal is process root, then subtree from left to right. So I start at SAP(4), then go to 4. Where I'm stuck is understanding whether it will go to 3, or will it go to SAP(3).

I am checking how the code works and I see it goes to 3, but I am confused as to why. If the rule is process root, then process subtree left to right, well isn't SAP a root, hence should come before 3 which is a child of SAP(3)??
 
Last edited:
  • #11
Maylis said:
I calculate 14 calls for num=4, and 8 calls for num=3
I agree.
You get the same number just by counting all function nodes in your tree apart from the top one.
 
  • #12
ImageUploadedByPhysics Forums1407624144.330818.jpg


I ran the function and got this as the traversal. I think where I am tripping up is some confusion over where it will go. So it will go from the root and get all the left subtrees that it can, which goes all the way down to 1, then it will go to SAP(0), SAP(-1), then finally connect with another root at SAP(1). But then after SAP(1), it goes up to SAP(2), instead of 1. I could see that if it didn't process that root it wouldn't ever have an opportunity to come back to it, but I guess it seems inconsistent that it would process that rooti nstead of the left subtree.
 
  • #13
It's a bit confusing that you include the integers, they are not function calls.
Instead of 4->3->2->..., it should start with SAP(4)->SAP(3)->SAP(2)->... which is always the call at the left side of the addition process.
 
  • #14
So in a tree, only function calls should be present??
 
  • #15
ImageUploadedByPhysics Forums1407626065.343959.jpg


So can the tree traverse the same node twice? I am looking at the bottom left of the tree, specifically at the bottom SAP(0). It can either go back up to SAP(2), then over to SAP(1), or SAP(0) -> SAP(1) which I wrote, or SAP(0) to SAP(0). Which way would it go?
 
  • #16
You can have the program count the number of calls, just declare a static counts, such as:

Code:
static int callcount;  /* initialiized to zero */

Do not explicity initialize the count, as it would get reset on each call.

Then for the first line of the recursive function increment the count:

Code:
    callcount ++;

Another issue here is that your trying to create a tree which has a mix of local values and either calls or returned values. A call stack would be a better diagram.
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K