Change a Recursive function that has a for loop in it into an iterative function?

Posted by Bill on Stack Overflow See other posts from Stack Overflow or by Bill
Published on 2009-11-23T06:33:54Z Indexed on 2010/05/01 8:07 UTC
Read the original article Hit count: 215

Filed under:
|
|
|

So I have this function that I'm trying to convert from a recursive algorithm to an iterative algorithm. I'm not even sure if I have the right subproblems but this seems to determined what I need in the correct way, but recursion can't be used you need to use dynamic programming so I need to change it to iterative bottom up or top down dynamic programming.

The basic recursive function looks like this:

Recursion(i,j)
{
if(i>j)
return 0;
else
{
 //This finds the maximum value for all possible subproblems and returns that for this problem
 for(int x = i; x < j; x++)
 {
    if(some subsection i to x plus recursion(x+1,j) is > current max)
        max = some subsection i to x plus recursion(x+1,j)
 }  
}
}

This is the general idea, but since recursions typically don't have for loops in them I'm not sure exactly how I would convert this to iterative. Does anyone have any ideas?

© Stack Overflow or respective owner

Related posts about recursion

Related posts about iterative