python recursion over 10000 nodes

2019-08-31 08:09发布

I have a list of over 10000 items and using recursion i want to iterate over all the possible combinations of their selection starting from 1st item for both its selection and not selection ( 2 branches ) then deciding on 2nd item for each branch and so on till last item in DFS way. I am using this code for optimization and most of the branches are not iterated over but atleast 1 leaf will be reached as to find the best

the problem is I made a recursion code in python and it works fine for 1000 elements after changing recursion limit to 10000 but i cant get it work for 10000 elements no matter how much i increase the recursion limit.( though i read its not suggested )

I am new to python and kind of feel like I am making some space assignment fault in my code that its not working . Please help me in any fixes or alternatives

Here is my recursion function

 

def rep(i): global taken global Btaken global Tvalue global Tweight global Bestimate global Bvalue global count global Mcount count+=1 Tweight -= items[i].weight if Tweight>=0 : taken[items[i].index-1] = 1 Tvalue += items[i].value if i < len(items)-1: rep(i+1) else : Btaken=taken*1 Bvalue = Tvalue Tvalue -= items[i].value taken[items[i].index-1]=0 Tweight += items[i].weight Bestimate = best(i+1,Tweight) if Bestimate+Tvalue > Bvalue : if i < len(items)-1: rep(i+1) else : Btaken=taken*1 Bvalue = Tvalue if Mcount<count : Mcount=count count-=1

The Mcount value is also correct now showing the number of functions in the call ( or hold) currently (according to dfs the height of tree ) then why when i increase the recursion limit to over 15000 still i cant use it to interate for 10000 items, I need some guidance on python memory limits .

0条回答
登录 后发表回答