查找网络x中最长的圆圈方式



我正在尝试在 networkx 中找到从节点 A 到节点 A 的最长方式,仅使用完全连接的图中的其他 10 个节点,如下所示。可能吗?

G = nx.Graph()
for i in range(len(hashlist)):
for j in range(len(hashlist)):
if i!=j:
connection=(i,j)
size=hashlist[i]-hashlist[j]
G.add_edge(i, j, weight=size)

这将找到有向图中最长的周期。

import networkx as nx
G = nx.Graph([(0, 0), (0, 1), (0, 2), (1, 2), (2, 0), (2, 1), (2, 2)]).to_directed()
a = sorted(list(nx.simple_cycles(G)), key = lambda s: len(s)) 
print(a[-1])

同样的方法也适用于加权图。

import networkx as nx
G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
G.add_weighted_edges_from([(0, 1, 3.0), (1, 2, 7.5), (2,3, 5), (1,3, 2.4)])
G = G.to_directed()
a = sorted(list(nx.simple_cycles(G)), key = lambda s: len(s)) 
print(a[-1])

最新更新