Python 3.x 如何转换基于";的Dijsktra算法的Python实现;无”;到“的距离”;“无限”;距离 有两个Dijkstra算法(方法),我在Python中实现的,我从这个源中获取的第一个方法,第二个方法是由我创建的,它更适合C++风格(带有检查和放松)——我更喜欢的方法。第一个Dijkstra方法有效,但第二个dijkstra2始终返回1e9。第二种方法有什么问题 from heapq import * def Dijkstra(graph, source): dist = [None] * len(graph) queue = [(0, source)] while queue: c_dist, u = heappop(queue) if dist[u] is None: dist[u] = c_dist for v, length in graph[u].items(): if dist[v] is None: heappush(queue, (c_dist + length, v)) return [-1 if x is None else x for x in dist] def dijkstra2( graph, source): dist = [1e9] * len(graph) queue = [(0, source)] while queue: c_dist, u = heappop(queue) if c_dist > dist[u]: continue for v, length in graph[u].items(): if dist[v] > dist[u] + length: dist[v] = dist[u] + length return [-1 if x is 1e9 else x for x in dist] graph = { 0: { 1:2, 2:4, 3:1 }, 1: { 2:1, 3:3 }, 2: { 4: 7}, 3: { 2: 2 }, 4: { 0:2, 3:3 }, 5: {} } source = 0 print (Dijkstra(graph, source))
您的代码中有3个问题:Python 3.x 如何转换基于";的Dijsktra算法的Python实现;无”;到“的距离”;“无限”;距离 有两个Dijkstra算法(方法),我在Python中实现的,我从这个源中获取的第一个方法,第二个方法是由我创建的,它更适合C++风格(带有检查和放松)——我更喜欢的方法。第一个Dijkstra方法有效,但第二个dijkstra2始终返回1e9。第二种方法有什么问题 from heapq import * def Dijkstra(graph, source): dist = [None] * len(graph) queue = [(0, source)] while queue: c_dist, u = heappop(queue) if dist[u] is None: dist[u] = c_dist for v, length in graph[u].items(): if dist[v] is None: heappush(queue, (c_dist + length, v)) return [-1 if x is None else x for x in dist] def dijkstra2( graph, source): dist = [1e9] * len(graph) queue = [(0, source)] while queue: c_dist, u = heappop(queue) if c_dist > dist[u]: continue for v, length in graph[u].items(): if dist[v] > dist[u] + length: dist[v] = dist[u] + length return [-1 if x is 1e9 else x for x in dist] graph = { 0: { 1:2, 2:4, 3:1 }, 1: { 2:1, 3:3 }, 2: { 4: 7}, 3: { 2: 2 }, 4: { 0:2, 3:3 }, 5: {} } source = 0 print (Dijkstra(graph, source)),python-3.x,algorithm,graph,dijkstra,Python 3.x,Algorithm,Graph,Dijkstra,您的代码中有3个问题: 正如chrisz已经指出的,您需要将v添加到队列中,否则您将在循环中只进行一次传递 由于dist中的值是在将节点放入队列时更新的,而不是在弹出节点时更新的,因此需要在开始时更改源的距离 由于需要使用x==1e9而不是x is 1e9,因此不会在末尾执行1e9和-1之间的转换 您可以在任何python控制台中检查: x=1e9 x is 1e9 返回False 以下是完整的工作代码: def dijkstra2( graph, source): INFI
- 正如chrisz已经指出的,您需要将
添加到队列中,否则您将在循环中只进行一次传递v
- 由于dist中的值是在将节点放入队列时更新的,而不是在弹出节点时更新的,因此需要在开始时更改源的距离
- 由于需要使用
而不是x==1e9
,因此不会在末尾执行x is 1e9
和1e9
之间的转换-1
x=1e9
x is 1e9
返回False
以下是完整的工作代码:
def dijkstra2( graph, source):
INFINITY = 1e9
dist = [INFINITY] * len(graph)
queue = [(0, source)]
dist[source]= 0
while queue:
c_dist, u = heappop(queue)
if c_dist > dist[u]:
continue
for v, length in graph[u].items():
if dist[v] > dist[u] + length:
dist[v] = dist[u] + length
heappush(queue, (dist[v], v))
return [-1 if x==INFINITY else x for x in dist]
您的代码中有3个问题:
- 正如chrisz已经指出的,您需要将
添加到队列中,否则您将在循环中只进行一次传递v
- 由于dist中的值是在将节点放入队列时更新的,而不是在弹出节点时更新的,因此需要在开始时更改源的距离
- 由于需要使用
而不是x==1e9
,因此不会在末尾执行x is 1e9
和1e9
之间的转换-1
x=1e9
x is 1e9
返回False
以下是完整的工作代码:
def dijkstra2( graph, source):
INFINITY = 1e9
dist = [INFINITY] * len(graph)
queue = [(0, source)]
dist[source]= 0
while queue:
c_dist, u = heappop(queue)
if c_dist > dist[u]:
continue
for v, length in graph[u].items():
if dist[v] > dist[u] + length:
dist[v] = dist[u] + length
heappush(queue, (dist[v], v))
return [-1 if x==INFINITY else x for x in dist]
在您的实现中,您曾在何处向队列添加任何内容?抱歉的实现是这样吗?我知道了,但在添加heappush(queue,(dist[v],v])后仍然不起作用。您曾在实现中向队列添加任何内容吗?抱歉的实现是这样吗?我知道了,但在添加heappush(queue,(dist[v],v))后仍然不起作用