如何在 python 中使用 networkx 在无向图中获取黑社会人口普查

How to get triad census in undirected graph using networkx in python

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

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 个同构 类 的计数?

我不限于 networkx 并且 很高兴收到使用其他库或其他语言的答案

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

与您之前的 类似的解决方案:遍历所有三元组并确定它所属的 class。由于classes只是三个节点之间的边数,因此计算每个3个节点组合的边数。

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)