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

a 夏天 提交于 2019-12-08 13:22:15

问题


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')])

Error: NetworkXNotImplemented: not implemented for undirected type

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?

I am not limited to networkx and happy to receive answers using other libraries or other languages.

I am happy to provide more details if needed.


回答1:


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)


来源:https://stackoverflow.com/questions/54730863/how-to-get-triad-census-in-undirected-graph-using-networkx-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!