gpt4 book ai didi

python - Networkx:寻找到图中多个节点之一的最短路径

转载 作者:太空狗 更新时间:2023-10-30 01:04:33 68 4
gpt4 key购买 nike

我有一张不同位置的图表:

import networkx as nx

G = nx.Graph()
for edge in Edge.objects.all():
G.add_edge(edge.from_location, edge.to_location, weight=edge.distance)

位置(节点)有不同的类型(厕所、建筑物入口等)我需要找到从某个给定位置到特定类型的任何位置的最短路径。 (例如:找到距离给定节点最近的入口。)

Networkx 库中是否有一些方法可以解决没有循环的问题?像这样的东西:

nx.shortest_path(
G,
source=start_location,
target=[first_location, second_location],
weight='weight'
)

如果两个位置的类型相同,则结果将是到 first_location 或 second_location 的最短路径。

是否有一些方法也可以返回路径长度?

最佳答案

我们将分三步完成。

  • 第 1 步:让我们创建一个虚拟图表来说明
  • 第 2 步:绘制图形和颜色节点以指示边长和特殊节点类型(厕所、入口等)
  • 第 3 步:从任何给定节点(源)计算到所有可达节点的最短路径,然后子集化为感兴趣的节点类型并选择长度最短的路径。

下面的代码肯定可以优化,但这可能更容易理解。

第 1 步:创建图表

edge_objects = [(1,2, 0.4), (1, 3, 1.7), (2, 4, 1.2), (3, 4, 0.3), (4 , 5, 1.9), 
(4 ,6, 0.6), (1,7, 0.4), (3,5, 1.7), (2, 6, 1.2), (6, 7, 0.3),
(6, 8, 1.9), (8,9, 0.6)]

toilets = [5,9] # Mark two nodes (5 & 9) to be toilets
entrances = [2,7] # Mark two nodes (2 & 7) to be Entrances
common_nodes = [1,3,4,6,8] #all the other nodes

node_types = [(9, 'toilet'), (5, 'toilet'),
(7, 'entrance'), (2, 'entrance')]

#create the networkx Graph with node types and specifying edge distances
G = nx.Graph()

for n,typ in node_types:
G.add_node(n, type=typ) #add each node to the graph

for from_loc, to_loc, dist in edge_objects:
G.add_edge(from_loc, to_loc, distance=dist) #add all the edges

第 2 步:绘制图形

#Draw the graph (optional step)
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True)
edge_labels = nx.get_edge_attributes(G,'distance')
nx.draw_networkx_edge_labels(G, pos, edge_labels = edge_labels)
nx.draw_networkx_nodes(G, pos, nodelist=toilets, node_color='b')
nx.draw_networkx_nodes(G, pos, nodelist=entrances, node_color='g')
nx.draw_networkx_nodes(G, pos, nodelist=common_nodes, node_color='r')
plt.show()

enter image description here

第 3 步:创建小函数以找到到节点类型的最短路径

def subset_typeofnode(G, typestr):
'''return those nodes in graph G that match type = typestr.'''
return [name for name, d in G.nodes(data=True)
if 'type' in d and (d['type'] ==typestr)]

#All computations happen in this function
def find_nearest(typeofnode, fromnode):

#Calculate the length of paths from fromnode to all other nodes
lengths=nx.single_source_dijkstra_path_length(G, fromnode, weight='distance')
paths = nx.single_source_dijkstra_path(G, fromnode)

#We are only interested in a particular type of node
subnodes = subset_typeofnode(G, typeofnode)
subdict = {k: v for k, v in lengths.items() if k in subnodes}

#return the smallest of all lengths to get to typeofnode
if subdict: #dict of shortest paths to all entrances/toilets
nearest = min(subdict, key=subdict.get) #shortest value among all the keys
return(nearest, subdict[nearest], paths[nearest])
else: #not found, no path from source to typeofnode
return(None, None, None)

测试:

 find_nearest('entrance', fromnode=5)

产生:

 (7, 2.8, [5, 4, 6, 7])

含义:距离 5 最近的“入口”节点是 7,路径长度是 2.8,完整路径是:[5, 4, 6, 7]。希望这可以帮助您前进。有什么不明白的请追问。

关于python - Networkx:寻找到图中多个节点之一的最短路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50723854/

68 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com