0

i am trying to make a for loop that will loop in python this method for all node in graph G that will return set/list of subgraphs.

H=G.subgraph(nodes_in_triangle(G, n))

thank you.

1 Answers1

0

To find all triangles in the graph you can use the function enumerate_all_cliques(), which returns all cliques in the graph. You can filter out all triangles by the number of nodes in cliques.

import networkx as nx

G = nx.house_x_graph()

%matplotlib inline # jupyter notebook
nx.draw(G, with_labels = True, node_color='pink', node_size=1000)

tri = filter(lambda x: len(x) == 3, nx.enumerate_all_cliques(G))
tri_subraphs = [G.subgraph(nodes) for nodes in tri]

for graph in tri_subraphs:
    print(graph.nodes())

Output:

[0, 1, 2]
[0, 1, 3]
[0, 2, 3]
[1, 2, 3]
[2, 3, 4]

enter image description here

Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73