"ulimit -s" has no effect?
Josiah Carlson
jcarlson at nospam.uci.edu
Fri Feb 6 23:43:42 EST 2004
>>This entire thread begs the question: Why are you recursing this deep?
>>It would be faster to iterate.
>
>
> The algorithm calls for it: Tarjan's algorithm for finding the
> Strongly-Connected Component (SCC) of a graph. If there is an equally
> efficient iterative approach, I'd like to hear of it.
>
> I don't think I need to recurse to depth 1,000,000 , but definitely higher
> than 7k.
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
- Josiah
More information about the Python-list
mailing list