如何在python中使用networkx在无向图中进行三合会普查

问题描述:

我有一个如下所示的无向networkx图,我想打印该图的triad census.但是,nx.triadic_census(G)不支持无向图.

I have an undirected networkx graph as follows and I want to print triad census of the graph. However, nx.triadic_census(G) does not support undirected graphs.

import networkx as nx
G = nx.Graph()
G.add_edges_from(
    [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
     ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])

错误:NetworkXNotImplemented: not implemented for undirected type

我知道,无向图只有4个同构类(没有16个有向图).是否可以使用networkx获取这4个同构类的计数?

I am aware that there is only 4 isomorphic classes for undirected graphs (not 16 as directed graphs). Is there a way of getting the count of these 4 isomorphic classes using networkx?

我不仅限于networkx,并且很乐意使用其他库或其他语言来接收答案.

如果需要,我很乐意提供更多详细信息.

I am happy to provide more details if needed.

与您以前的

A similar solution to your previous post: iterate over all triads and identify the class it belongs to. Since the classes are just the number of edges between the three nodes, count the number of edges for each combination of 3 nodes.

from itertools import combinations

triad_class = {}
for nodes in combinations(G.nodes, 3):
    n_edges = G.subgraph(nodes).number_of_edges()
    triad_class.setdefault(n_edges, []).append(nodes)