"ulimit -s" has no effect?
Maciej Kalisiak
mac at dgp.toronto.edu
Sat Feb 7 10:39:00 EST 2004
Josiah Carlson <jcarlson at nospam.uci.edu> writes:
> Any recursive algorithm can be unrolled into an iterative version. The below
> is fairly generic, and relies on locals() being sane.
>
> def blah():
> stack = []
> #set up initial local variables
> a = dict(locals())
> del a['a']
> del a['stack']
> stack.append(a)
> while stack:
> locals().update(stack.pop())
> #do what you need for this loop
> if need_to_recurse:
> #save previous state
> a = dict(locals())
> del a['a']
> del a['stack']
> stack.append(a)
> #save new local state
> a = dict(locals())
> del a['a']
> del a['stack']
> stack.append(a)
> #recursion will happen automatically
Interesting. Alas, I don't think I can use this. First, this seems to only
apply to tail recursion, no? In the Tarjan's algorithm recursion happens at
the beginning, on the children of a given node, and then the results of that
recursion are used in the computation for the current node. Also, I think my
locals() won't be "sane"... it contains a huge graph; from what I understand
from the above, there would be multiple copies of it on `stack', and that's
just not feasible due to memory constraints.
More information about the Python-list
mailing list