13

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

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 a list and then i can work on one of the permutation.

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

Is there any simpler way in implementation via which i can generate all the combinations of the vertices and not have this error.

  • 3
    As a sidebar, the reason for the memory error is this: http://www.wolframalpha.com/input/?i=40%21+bytes+in+gigabyte – Jasmijn Jun 28 '11 at 08:10
  • 6
    `perm` would contain 815915283247897734345611269596115894272000000000 (40!) lists of 40 items. – Dan D. Jun 28 '11 at 08:10
  • 1
    Do you realize how many combinations of vertices there are? What are you going to do with all the combinations? You can avoid storing them all at once, but if you really need to consider every combination, the Universe is not guaranteed to exist by the time you are done... switching to C will not help, either. – Karl Knechtel Jun 28 '11 at 08:11
  • 3
    What is the real algorithm - what are you trying to do? "exponential" time normally means O(2^N); here we have O(N!) which is much worse. – Karl Knechtel Jun 28 '11 at 08:12
  • Yeah so i was working on some ordering problem which has no other solution till now :) ... anyways thanks a lots –  Jul 11 '11 at 08:46

2 Answers2

30

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....

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
4

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)

Jasmijn
  • 9,370
  • 2
  • 29
  • 43