I am wondering whether can I use networkx to extract all possible induced subgraphs (graphlets) with specific number of nodes in the subgraphs from an input large graph, or is there another package that can do the job? For example, if I have a large graph, which is illustrated in networkx adjacency list format,
graph G:
1 2 3 7
2 1 4
3 1 4 6 5
4 2 3 5
5 3 4 6
6 3 5 7
7 1 6
which will be look like
if I want to extract graphlet with 3 nodes the algorithm should return me
subgraph1:
1 2 3
2 1
3 1
[(1,2),(1,3)] subgraph2:
1 3 7
3 1
7 1
[(1,3),(1,7)] subgraph3:
3 4 5
4 3 5
5 3 4
[(3,4),(3,5),(4,5)]
subgraph4,subgraph5,subgraph6...
The following is the code of the question suggested by @Hooked. Let's say n=3
import itertools
target = nx.complete_graph(3)
for sub_nodes in itertools.combinations(g.nodes(),len(target.nodes())):
subg = g.subgraph(sub_nodes)
if nx.is_connected(subg):
print subg.edges()
the the output will look like
[(1, 2), (1, 3)]
[(1, 2), (2, 4)]
[(1, 2), (1, 7)]
[(1, 3), (3, 4)]
[(1, 3), (3, 5)]
[(1, 3), (3, 6)]
[(1, 3), (1, 7)]
[(1, 7), (6, 7)]
[(2, 4), (3, 4)]
[(2, 4), (4, 5)]
[(3, 4), (3, 5), (4, 5)]
[(3, 4), (3, 6)]
[(3, 5), (3, 6), (5, 6)]
[(3, 6), (6, 7)]
[(4, 5), (5, 6)]
[(5, 6), (6, 7)]
itertools.combinations
does. – Percheron