NetworkX: list of directed edges in a triad census
Question:
In NetworkX, I can use the nx.triadic_census(g)
to give me a list of triads in my network.
Out:
{'003': 1434217,
'012': 282025,
'102': 32640,
'021D': 21,
'021U': 267246,
'021C': 445}
I wanted to know how I can list the directed edges belonging to a triad. For example, in Pyspark, to list the nodes/edges in triad “102” : g.find("(a)-[e]->(b); (b)-[e2]->(a)").show()
and it will give you
| a| e| b| e2|
+--------------------+-------------+--------------------+--------------+
|[US, United State...| [US, AZ, 0]| [AZ, Azerbaijan]| [AZ, US, 637]|
| [LU, Luxembourg]|[LU, BE, 213]| [BE, Belgium]|[BE, LU, 1470]|
| [FI, Finland]| [FI, CZ, 24]|[CZ, Czech Republic]| [CZ, FI, 51]|
| [HU, Hungary]| [HU, PL, 0]| [PL, Poland]| [PL, HU, 231]|
|[RU, Russian Fede...| [RU, UA, 0]| [UA, Ukraine]| [UA, RU, 0]|
Is there a way for me to do this in NetworkX?
Answers:
There is no function in networkx
that allow you to do it, so you should implement it manually. I modified the networkx.algorithms.triads
code to return triad nodes, not their count. You can find it here. It can be modified with replacing tuple in census['...'].add(tuple(sorted([u, v, w])))
lines to add edges instead of nodes.
i want to extrcat the triads-300 in large weighted directed networks with networkx you can find all possible kinds of triads here
here is my codes
network_data = nx.read_gexf(file_path)
G = nx.DiGraph()
G_contain_triple = nx.triads_by_type(network_data)
for x, y in G_contain_triple.items():
if x == "300":
for tt in y:
print(tt.edges.data("weight"))
G.add_weighted_edges_from(tt.edges.data("weight"))
if len(G.nodes()) > 0:
nx.write_gexf(G, os.path.join(output_path, year + 'tri.gexf'))
the networkx will find all 16 possible types of triads together with nx.triads_by_type(network_data), this will cost very very very much time. but what i need is only the 300-triads(last one in the image)
i wonder if there is any solution to find only the 300-triads without others and save my time you can find the test file 2021_DiGraph_weighted.gexf below test file https://github.com/koko12131/patentee
In NetworkX, I can use the nx.triadic_census(g)
to give me a list of triads in my network.
Out:
{'003': 1434217,
'012': 282025,
'102': 32640,
'021D': 21,
'021U': 267246,
'021C': 445}
I wanted to know how I can list the directed edges belonging to a triad. For example, in Pyspark, to list the nodes/edges in triad “102” : g.find("(a)-[e]->(b); (b)-[e2]->(a)").show()
and it will give you
| a| e| b| e2|
+--------------------+-------------+--------------------+--------------+
|[US, United State...| [US, AZ, 0]| [AZ, Azerbaijan]| [AZ, US, 637]|
| [LU, Luxembourg]|[LU, BE, 213]| [BE, Belgium]|[BE, LU, 1470]|
| [FI, Finland]| [FI, CZ, 24]|[CZ, Czech Republic]| [CZ, FI, 51]|
| [HU, Hungary]| [HU, PL, 0]| [PL, Poland]| [PL, HU, 231]|
|[RU, Russian Fede...| [RU, UA, 0]| [UA, Ukraine]| [UA, RU, 0]|
Is there a way for me to do this in NetworkX?
There is no function in networkx
that allow you to do it, so you should implement it manually. I modified the networkx.algorithms.triads
code to return triad nodes, not their count. You can find it here. It can be modified with replacing tuple in census['...'].add(tuple(sorted([u, v, w])))
lines to add edges instead of nodes.
i want to extrcat the triads-300 in large weighted directed networks with networkx you can find all possible kinds of triads here
here is my codes
network_data = nx.read_gexf(file_path)
G = nx.DiGraph()
G_contain_triple = nx.triads_by_type(network_data)
for x, y in G_contain_triple.items():
if x == "300":
for tt in y:
print(tt.edges.data("weight"))
G.add_weighted_edges_from(tt.edges.data("weight"))
if len(G.nodes()) > 0:
nx.write_gexf(G, os.path.join(output_path, year + 'tri.gexf'))
the networkx will find all 16 possible types of triads together with nx.triads_by_type(network_data), this will cost very very very much time. but what i need is only the 300-triads(last one in the image)
i wonder if there is any solution to find only the 300-triads without others and save my time you can find the test file 2021_DiGraph_weighted.gexf below test file https://github.com/koko12131/patentee