7 7 votes Consider the following recursive function which is used by dynamic programming. Assume for every function call T(i) it checks the table first, if its value is already computed it retrieves the value from table. Otherwise it calls a recursive function call to compute its return value. Whenever a function T(i) computes for first time its return value is stored in the table to avoid the redundant function calls. The number of function calls that need the support of stack to complete the execution of the function T(12) are __________ . Programming in C + – Anirban Biswas 2.8k views answer comment Share Follow Print See all 4 Comments 4 4 Comments reply dd commented Dec 31, 2016 reply Follow flag post again ! photo not loading ! 0 0 replyShare Anirban Biswas commented Jan 1, 2017 reply Follow flag Edited post. 0 0 replyShare pC commented Jan 1, 2017 reply Follow flag is it 21 ? 2 2 replyShare Pranav Kant Gaur commented Jan 19, 2017 reply Follow flag Is 5 the answer? 0 0 replyShare Please log in or register to add a comment.
Best answer 9 9 votes It should be $\large\color{red}{21}$ dd answered Jan 1, 2017 • selected Jan 1, 2017 by pC dd comment Share Follow See all 20 Comments 20 20 Comments reply Kapil commented Jan 1, 2017 reply Follow flag @Debashish, This code allows storing the values in the table. Every call is not a fresh call. 1 1 replyShare dd commented Jan 1, 2017 reply Follow flag ok ! I got your point ..mark the redundancy in the above diagram. 1 1 replyShare Kapil commented Jan 1, 2017 reply Follow flag Yes, Redundant calls need to be excluded . 0 0 replyShare dd commented Jan 1, 2017 reply Follow flag I am asking if there is any redundancy please point out...I think mine is ok! ..that may be wrong! 0 0 replyShare mcjoshi commented Jan 1, 2017 reply Follow flag I too think no redundancy in above tree. 0 0 replyShare Kapil commented Jan 1, 2017 reply Follow flag In the above diagram, Do both the 7's need the support of the stack. If one is calculated and saved in the table, then other can be used. Do both the 5's and both the 3's need stack support ? Is there any need to make a stack frame of the duplicates ? 0 0 replyShare dd commented Jan 1, 2017 reply Follow flag @kapil your QS might be...why call $7$ again from f(9) ?? My answer is: to check $7$ in the table f(7) must be invoked first and pushed on to the stack. After that check for $7$ and found. SO no further call from f(7) function frame. If this is not the reason.. then please mention. 0 0 replyShare dd commented Jan 1, 2017 reply Follow flag srry I was writing a comment and posted ! 0 0 replyShare dd commented Jan 1, 2017 reply Follow flag then you are asking check for $7$ in the f(9) frame ??? 0 0 replyShare Kapil commented Jan 1, 2017 reply Follow flag T(9) = T(7) + T(6) T(7) checks the table first, if its value is already computed it retrieves the value from table. Otherwise it calls a recursive function call. When T(7) is already computed from T(10), then getting T(7) again, which has to be checked first and then called(This is function call), right ? 0 0 replyShare dd commented Jan 1, 2017 reply Follow flag cache[N+1]; // memoization table// type function(int n) { if(cache[n]!= -1) { return cache[n]; }else { cache[n] = function(n-2) + function(n-3); } return cache[n]; } int main() { /* code */ for(int i=0;i<=N;i++) { cache[i] = -1; // initialize with something that // will not be used } // base cases // neglect -ve index for time being cache[0] = base_value1; cache[1] = base_value2; // call now function(n); /* code */ } 0 0 replyShare dd commented Jan 1, 2017 reply Follow flag When T(7) is already computed from T(10), then getting T(7) again T(7) is already computed from T(10). how to know that ? we will know that after getting inside a new f(7) frame again and check for n = 7 in the table or not ? then getting T(7) again When we know f(7) is coming again ? in f(9) ? NO in f(9) we will check for $n = 9$ only. $9$ is not in the table. so we will expand $7$ and $6$ 1 1 replyShare Kapil commented Jan 1, 2017 reply Follow flag Simply tell me one thing. What is the answer if the question says unique function calls ? 0 0 replyShare dd commented Jan 1, 2017 reply Follow flag possible distinct values of n 0 0 replyShare dd commented Jan 1, 2017 reply Follow flag A similar example of a look-up table in CLRS in matrix chain multiplication: 0 0 replyShare Arjun commented Jan 19, 2017 reply Follow flag @Debashish "T(7) is already computed from T(10). how to know that ?" it can known during execution. "The number of function calls that need the support of stack to complete the execution of the function" This is a bit ambiguous- but in all probability should be referring to number of function where a function call is made during runtime. 3 3 replyShare dd commented Jan 19, 2017 reply Follow flag Yes sir .. But, I followed what QS says - Assume for every function call T(i) it checks the table first, if its value is already computed it retrieves the value from table. Otherwise it calls a recursive function call to compute its return value. In a call to f(9): table[9] is searched if valid data found in table[9] , return table[9] if NO valid data found in table[9] Then, call recursive function f(7) and f(6) Sir, If we need to avoid calling again f(7) inside f(9),something needs to added in between step$3$ and step$4$. I think the steps will be like this. In a call to f(9): flag1 = 0,flag2 = 0; table[9] is searched if valid data found in table[9] , return table[9] if NO valid data found in table[9],Then check table[7] : if no valid data found set flag1 check table[6] : if no valid data found set flag2 if(flag1) call f(7) if(falg2) call f(6) Or, any other way to avoid calling f(7) in runtime ? 3 3 replyShare Kapil commented Jan 21, 2017 reply Follow flag @Arjun Sir, So shouldn't this be only distinct function calls that we make ? 0 0 replyShare Arjun commented Jan 21, 2017 reply Follow flag @Debashish you are correct. @Kapil, see the last comment by Debashish 1 1 replyShare Neha Shikha commented Feb 5, 2017 reply Follow flag @Arjun sir then sir what is the answer 10 or 11. 0 0 replyShare Please log in or register to add a comment.