Prevent memory error in itertools.permutation

后端 未结 2 1035
说谎
说谎 2020-12-01 15:16

Firstly I would like to mention that i have a 3 gb ram.

I am working on an algorithm that is exponential in time on the nodes so for it I have in the code

相关标签:
2条回答
  • 2020-12-01 15:48

    Try to use the iterator generated by the permutations instead of recreating a list with it :

    perm_iterator = itertools.permutations(list(graph.Nodes))
    
    for item in perm_iterator:
       do_the_stuff(item)
    

    by doing this, python will keep in memory only the currently used permutation, not all the permutations (in term of memory usage, it is really better ;) )

    On the other side, once the memory problem solved, the time to treat all the permutations will be growing exponentially with the number of vertices....

    0 讨论(0)
  • 2020-12-01 15:48

    It won't work. Looping over an iterator won't work either. You see, if the code in the for-loop takes 1 microsecond to run, it will take 2.587×10^34 years to run completely. (See http://www.wolframalpha.com/input/?i=40%21+microseconds+in+years)

    0 讨论(0)
提交回复
热议问题