python recursion over 10000 nodes

蓝咒 提交于 2019-12-25 04:30:27

问题


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 .

来源:https://stackoverflow.com/questions/22344612/python-recursion-over-10000-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!