简体   繁体   English

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

[英]How to get triad census in undirected graph using networkx in python

I have an undirected networkx graph as follows and I want to print triad census of the graph. 我有一个如下所示的无向networkx图,并且我想打印该图的triad census However, nx.triadic_census(G) does not support undirected graphs. 但是, 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')])

Error: NetworkXNotImplemented: not implemented for undirected type 错误: NetworkXNotImplemented: not implemented for undirected type

I am aware that there is only 4 isomorphic classes for undirected graphs (not 16 as directed graphs). 我知道,无向图只有4个同构类(没有16个有向图)。 Is there a way of getting the count of these 4 isomorphic classes using networkx? 是否可以使用networkx获取这4个同构类的计数?

I am not limited to networkx and happy to receive answers using other libraries or other languages . 我不仅限于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. 由于类仅是三个节点之间的边数,因此请为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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM