简体   繁体   English

使用networkx计算有向图的传递闭包

[英]compute transitive closure of directed graph with networkx

My efforts to find how to compute transitive closure of directed graph with networkx have come up surprisingly empty. 我发现如何使用networkx计算有向图的传递闭合的努力出乎意料地空了。 It seems unlikely that this is not in networkx, so ... where is it? 这似乎不太可能不在networkx中,所以...它在哪里? (I am aware that Sage includes this functionality.) (我知道Sage包含此功能。)

There is no transitive closure function in NetworkX. NetworkX中没有传递闭包功能。 But I think you can do it in one line :-). 但我认为您可以在一行中做到这一点:-)。 (untested) (另)

In [1]: import networkx as nx

In [2]: G = nx.DiGraph([(1, 2), (2, 3), (3, 2), (3, 4)])

In [3]: H = nx.DiGraph([(u,v,{'d':l}) for u,adj in nx.floyd_warshall(G).items() for v,l in adj.items() if l > 0 and l < float('inf')])

In [4]: H.adj
Out[4]: 
{1: {2: {'d': 1.0}, 3: {'d': 2.0}, 4: {'d': 3.0}},
 2: {3: {'d': 1.0}, 4: {'d': 2.0}},
 3: {2: {'d': 1.0}, 4: {'d': 1.0}},
 4: {}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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