如何在python中使用networkx在无向图中进行三合会普查 [英] How to get triad census in undirected graph using networkx in python

查看:959
本文介绍了如何在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)

这篇关于如何在python中使用networkx在无向图中进行三合会普查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆