I really like the connected_subgraph_iterator
, but it always lists all connected subgraphs or those with at most $k$ vertices .
What if I only want to obtain connected subgraphs with exactly $k$ vertices? We can, of course, produce connected subgraphs with no more than $k$ vertices and then pick them, but when $k$ is close than the odder of input graph, it seems like a lot of work.
def connected_subgraphs_with_k_vertices(G, k):
for subgraph in G.connected_subgraph_iterator(k):
if subgraph.order() ==k:
yield subgraph
# Example usage
G = graphs.CubeGraph(8)
k=8
subgraphs = list(connected_subgraphs_with_k_vertices(G, k))
graphs_list.show_graphs(subgraphs)