问题
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