0

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.

EmJ
  • 4,398
  • 9
  • 44
  • 105
  • 1
    This is kind of a silly solution, but you could transform your graph into a directed graph in which each connection is reciprocated, then apply the method in networkx. You should be able to extract what you need from the appropriate isomorphic classes. – Johannes Wachs Feb 17 '19 at 15:03
  • Is there any reason you want all four classes? The number of complete triangles and 2-sided triangles can be found from the clustering coefficient. – ComplexGates Feb 17 '19 at 22:57
  • @JohannesWachs Thanks for the comment. But I think that is a great idea. I would appreciate if you could post it as an answer. Looking forward to hearing from you :) – EmJ Feb 18 '19 at 04:20

1 Answers1

2

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)
busybear
  • 10,194
  • 1
  • 25
  • 42
  • Thanks a lot for the answer. However, I get an error in `triad_classG`. Can you please tell me how to fix this? :) – EmJ Feb 18 '19 at 06:45
  • 1
    Oh that would be a typo. There is no `triad_classG`. Just `G`. Updated post. – busybear Feb 18 '19 at 06:52