Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why do you need list of all graphs? This is what takes huge amount of memory.

Also, you are collecting canonical labels in a list, but there will be quite a few of duplicates, and so set is more preferable here. The code then becomes:

def iso_graphs(n):
    '''returns all isomorphism classes of simple graphs on n edges on 2*n vertices'''
    mygraphs = graphs(2 * n, loops=False, size=n)
    return { G.canonical_label().copy(immutable=True) for G in mygraphs if G.size() == n }

Why do you need list of all graphs? This is what takes huge amount of memory.

Also, you are collecting canonical labels in a list, but there will be quite a few of duplicates, and so set is more preferable here.

Also, you don't need to check G.size() == n since you have specifically requested such graphs from graphs() generator.

The improved code then becomes:

def iso_graphs(n):
    '''returns all isomorphism classes of simple graphs on n edges on 2*n vertices'''
    mygraphs = graphs(2 * n, loops=False, size=n)
    return { G.canonical_label().copy(immutable=True) for G in mygraphs if G.size() == n }

Why do Do you really need a list of all graphs? This is what takes huge amount of memory.

Also, If you are collecting need a generator of canonical labels in a list, but there will then it can be quite a few of duplicates, and so set is more preferable here.

Also, you don't need to check G.size() == n since you have specifically requested such graphs from graphs() generator.

The improved code then becomes:obtained as

def iso_graphs(n):
gen_cl = ( G.canonical_label().copy(immutable=True) for G in graphs(2 * n, loops=False, size=n) )

It will generate you canonical labels one at a time - like:

for mycl in gen_cl:
 '''returns all isomorphism classes of simple graphs on n edges on 2*n vertices'''
    mygraphs = graphs(2 * n, loops=False, size=n)
    return { G.canonical_label().copy(immutable=True) for G in mygraphs }
print( mycl.graph6_string() )

PS. It may be the case that graphs generated by graphs() are already canonically labeled, and if so .canonical_label() is not needed.