Memory error prevention in itertools.permutation

Firstly, I would like to mention that I have a 3 GB drum.

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

perm = list( itertools.permutations(list(graph.Nodes))) # graph.Nodes is a tuple of 1 , 2 , ... n integers

which generates all the combinations of vertices in the list, and then I can work on one of the permutations.

However, when I run the program for 40 vertices, it gives a memory error.

Is there an easier way in the implementation through which I can generate all combinations of vertices and not have this error.

+5
source share
2 answers

Try using an iterator generated by permutations instead of recreating a list with it:

perm_iterator = itertools.permutations(list(graph.Nodes))

for item in perm_iterator:
   do_the_stuff(item)

, python , ( , ;))

, , ....

+16
+4

All Articles