I have a recursive function. The recursion forms a tree with many (millions) terminal nodes. The function must output a value (say, a number or a vector) from each terminal node. At the end, when all recursion is done, we want to collect all the output values. How can that be done cleanly and efficiently? A prototype example is a recursive function that generates all permutations of a vector (or simply of 1:n). One permutation is generated at each terminal node. Terminal nodes occur at various depths in the tree. We don't need to pass anything up the tree, just output from the terminal nodes. Is there a way to create a global data structure and have a function tack data onto that structure? sort of like the C++ pushback()? One solution is to output values to a file, and at the end read the file back. This worked ok, but is there a better way? _________________________________________________________________ Live.
James Muller
2008-Sep-26 20:22 UTC
[R] Collecting output from terminal nodes in a recursion tree
Why not have the return values of each instance of the function be: a) list containing only the current value if terminal node b) concatenated list of returned values from functions if not terminal node (and hence call other functions) My thought is the way you've set this up won't be efficient in R. If you've got time, try to think about how you'd calculate permutations in a single (non-recursive) function with while loops. Also there are functions in R that return permutations of values (can't remember what they are). Quick to do what you want if you have a list of all permutations of c(1:N). An alternative, append each value to globally-defined list. Hope this isn't as vague as I suspect... Cheers On Fri, Sep 26, 2008 at 4:10 PM, B Q <bald___eagle at hotmail.com> wrote:> > > I have a recursive function. The recursion forms a tree with many > (millions) terminal nodes. The function must output a value (say, a > number or a vector) from each terminal node. At the end, when all > recursion is done, we want to collect all the output values. How can > that be done cleanly and efficiently? > > A prototype example is a recursive function that generates all > permutations of a vector (or simply of 1:n). One permutation is > generated at each terminal node. > > Terminal nodes occur at various depths in the tree. We don't need to > pass anything up the tree, just output from the terminal nodes. > > Is there a way to create a global data structure and have a function > tack data onto that structure? sort of like the C++ pushback()? > > One solution is to output values to a file, and at the end read the > file back. > This worked ok, but is there a better way? > > _________________________________________________________________ > > Live. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >