0

I am currently using igraph to get the traid census of a given directed graph usingtriad_census(g). This returns the count of triads in each of the 16 classes.

e.g., 16 3 0 10 1 0 0 0 0 0 0 0 0 0 0 0

However, I would like to know more details of the triads than these summary statistics.

i.e. given that the network has 16 of 003, what they are? given that the network has 3 012, what they are?

Example: The 3 traids of 012 are (john -> emi, jenne), (cena -> ally, john), (emi -> peter, david)

Is there a way of doing this in r or python?

MWE

Graph data: http://docs.google.com/viewer?a=v&pid=sites&srcid=ZGVmYXVsdGRvbWFpbnxkYWlzaGl6dWthfGd4OmFmZTI0NjhlMjQ0ZDQ5MQ

Code:

library(igraph)
#import the sample_dw_adj.csv file:
dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=FALSE) # read .csv file
m=as.matrix(dat)
net=graph.adjacency(m,mode="directed",weighted=TRUE,diag=FALSE)
plot.igraph(net,vertex.label=V(net)$name,layout=layout.fruchterman.reingold, vertex.label.color="black",edge.color="black",edge.width=E(net)$weight/3, edge.arrow.size=1.5)

So, my actual graph would look like as follows. enter image description here

I am happy to provide more details if needed.

EmJ
  • 4,398
  • 9
  • 44
  • 105
  • Can you please provide a reproducible example? You can see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) if you need any tips on how to do that. – Roman Luštrik Feb 15 '19 at 10:18
  • @RomanLuštrik Sure, I will update the question. I saw your comment just now :) – EmJ Feb 15 '19 at 11:06

1 Answers1

1

There doesn't seem to be a builtin method to accomplish what you want with Networkx. However, you can manually go through each triad and define which class it belongs to:

from itertools import combinations

triad_class = {}
for nodes in combinations(G.nodes, 3):
    triad_class[nodes] = [k for k, v in nx.triads.triadic_census(G.subgraph(nodes)).items() if v][0]

If you'd rather have a dictionary with the classes as the keys, you can try something like this:

from itertools import combinations

triad_class = {}
for nodes in combinations(G.nodes, 3):
    tc = [k for k, v in nx.triads.triadic_census(G.subgraph(nodes)).items() if v][0]
    triad_class.setdefault(tc, []).append(nodes)
busybear
  • 10,194
  • 1
  • 25
  • 42
  • 1
    Hi, I checked your code, and that worked. Thank you once again :) – EmJ Feb 16 '19 at 08:34
  • Please let me know if you know an answer for this: https://stackoverflow.com/questions/54730863/how-to-get-triad-census-in-undirected-graph-using-networkx-in-python Thank you :) – EmJ Feb 17 '19 at 07:02