sp需要什么格式才能与nx.barycenter一起使用?
我正在使用networkx来分析一个加权的无向图,但遇到了一个问题,就是如何确定重心。我的图不是一个整体,而是由三个相互连接的部分组成。因此,我对每个子图单独使用这个函数。
当我用图的权重运行nx.barycenter时,它能正常工作,但如果我用预先计算的最短路径来运行,就不行了。在这种情况下,我得到了一个错误信息:NetworkXNoPath: 输入图有6个节点和6条边,但它是断开的,所以每个诱导子图的重心都是无限的。
显然,这个问题和我给函数的shortest_paths
有关。
文档上说,我需要的是所有节点对之间的最短路径长度,格式是字典的字典
,但是我用dict(nx.shortest_path_length(graph, weight = "weight"))
得到的结果却不行。
我需要用其他方式来计算吗?
我现在的代码是:
import networkx as nx
def components(graph):
connected_comps = sorted(list(nx.connected_components(graph)), key=len, reverse=True)
return connected_comps
def subgraphs(graph):
subgraphs = []
connected_comps = components(graph)
for i, comp in enumerate(connected_comps):
subgraph = graph.subgraph(comp)
subgraphs.append(subgraph)
return subgraphs
graph = nx.Graph()
component1 = [387522, 7189, 3337, 5071]
component2 = [1, 2, 3, 4]
component3 = [2861, 3103, 7188, 387511, 387521, 387531]
graph.add_nodes_from(component1)
graph.add_nodes_from(component2)
graph.add_nodes_from(component3)
graph.add_edge(387522, 7189, weight = 0.55)
graph.add_edge(387522, 3337, weight = 0.55)
graph.add_edge(7189, 3337, weight = 0.86)
graph.add_edge(5071, 7189, weight = 0.72)
graph.add_edge(5071, 3337, weight = 0.72)
graph.add_edge(5071, 387522, weight = 0.72)
graph.add_edge(1, 2, weight = 0.78)
graph.add_edge(1, 3, weight = 0.78)
graph.add_edge(1, 4, weight = 0.78)
graph.add_edge(2, 3, weight = 0.78)
graph.add_edge(2, 4, weight = 0.78)
graph.add_edge(3, 4, weight = 0.78)
graph.add_edge(2861, 3103, weight = 0.78)
graph.add_edge(7188, 2861, weight = 0.86)
graph.add_edge(2861, 387511, weight = 0.86)
graph.add_edge(7188, 387521, weight = 0.86)
graph.add_edge(7188, 3103, weight = 0.86)
graph.add_edge(3103, 387531, weight = 0.86)
p = dict(nx.shortest_path_length(graph, weight = "weight"))
shortest_paths = {
'387522': {'387522': 0, '7189': 0.55, '3337': 0.55, '5071': 0.72},
'7189': {'7189': 0, '387522': 0.55, '3337': 0.86, '5071': 0.72},
'3337': {'3337': 0, '387522': 0.55, '7189': 0.86, '5071': 0.72},
'5071': {'5071': 0, '7189': 0.72, '3337': 0.72, '387522': 0.72},
'1': {'1': 0, '2': 0.78, '3': 0.78, '4': 0.78},
'2': {'2': 0, '1': 0.78, '3': 0.78, '4': 0.78},
'3': {'3': 0, '1': 0.78, '2': 0.78, '4': 0.78},
'4': {'4': 0, '1': 0.78, '2': 0.78, '3': 0.78},
'2861': {'2861': 0, '3103': 0.78, '7188': 0.86, '387511': 0.86, '387531': 1.64, '387521': 1.72},
'3103': {'3103': 0, '2861': 0.78, '7188': 0.86, '387531': 0.86, '387511': 1.64, '387521': 1.72},
'7188': {'7188': 0, '2861': 0.86, '387521': 0.86, '3103': 0.86, '387511': 1.72, '387531': 1.72},
'387511': {'387511': 0, '2861': 0.86, '3103': 1.64, '7188': 1.72, '387531': 2.5, '387521': 2.58},
'387521': {'387521': 0, '7188': 0.86, '2861': 1.72, '3103': 1.72, '387511': 2.58, '387531': 2.58},
'387531': {'387531': 0, '3103': 0.86, '2861': 1.64, '7188': 1.72, '387511': 2.5, '387521': 2.58}
}
def find_bary_center(graph, sp=None):
centers = []
for subgraph in subgraphs(graph):
if nx.is_connected(subgraph):
print("Connected", subgraph)
center = nx.barycenter(subgraph, sp=sp)
centers.append(center)
return centers
bary = find_bary_center(graph, p)
#print(p)
在这种情况下,p
的格式和shortest_paths
是一样的,但两者都不工作。它们都是字典的字典,看起来应该可以用,但实际上不行。
1 个回答
1
看起来,最短路径的字典只包含那些也在传给 barycenter
的图中的节点。
def find_bary_center(graph, sp=None):
centers = []
for subgraph in subgraphs(graph):
if nx.is_connected(subgraph):
print("Connected", subgraph)
if sp is not None:
center = nx.barycenter(subgraph, sp={node : data for node, data in sp.items() if node in subgraph})
else:
center = nx.barycenter(subgraph)
centers.append(center)
return centers
bary = find_bary_center(graph, p)
# [[2861, 3103], [387522], [1, 2, 3, 4]]