简体   繁体   中英

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. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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