# For every node in graph
for node in G.nodes():
# We look for adjacent nodes
for adj_node in G[node]:
# If adjacent node has an edge to the first node
# Or our graph have several edges from the first to the adjacent node
if node in G[adj_node] or len(G[node][adj_node]) > 1:
# DO MAGIC!!
print(node, adj_node)
据我所知,这个问题没有内置的
networkx
函数。但是networkx
将图形节点和边存储在iterable结构中,因此您可以像这样逐个处理它们:我认为这是最能解决您问题的
networkx
式代码。注意,图越稀疏,它的工作速度就越快。在最坏的情况下,完整图的复杂性是O(n^2)。在最好的情况下-非常稀疏的图-O(n)。在相关问题 更多 >
编程相关推荐