Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

generate all (not necessarily induced) subgraphs of a graph

I want to, given a Sage graph G, generate all subgraphs of G and check them for some property, and store in a list those which satisfy the propery. What I'm doing currently seems very inefficient:

graphs_i_want=[]
for g in graphs.nauty_geng(str(G.order())):
    if G.subgraph_search(g):
        if check(g):
            graphs_i_want.append(g.graph6_string())

check(g) checks that g satisfies the properties I want, and it's done. Actually the code above works completely - it gives a list of the graph6 strings for every subgraph of G. However, I think it's overkill to check through all graphs, and it's also pretty slow for graphs larger than 6 vertices or so. I need it to work for more, up to maybe 15.

An iterator would be fine. I just need to check each of the graphs for some property. The code could be something like this:

graphs_i_want=[]
for g in all_subsets_generator(G):
    if check(g):
        graphs_i_want.append(g.graph6_string())

where all_subsets_generator(G) iterates over all subsets of G.

Thanks for any advice. Let me know if more information is needed.