在 python 中使用 BFS 的未加权单源最短路径
Unweighted-Single-Source-Shortest-Path using BFS in python
我是 python 的新人。我尝试使用 BFS 获取 Unweighted-Single-Source-Shortest-Path。
from queue import *
def ussp(graph, s):
len_graph = len(graph)
prev = [[]*len_graph for i in range(len_graph)]
visited = [False for i in range(len_graph)]
queue2 = Queue
dist = [[float('inf')] for i in range(len_graph)]
queue2.put_nowait(graph[s], 0) # starting with s
visited[s] = True
dist[s] = 0
# modified BFS alg.
while queue2.empty() == False:
h = queue2.get_nowait()
for i in len(graph[h]):
if visited[graph[h][i]] == False:
visited[graph[h][i]] = True
dist[graph[h][i]] = dist[h] + 1
prev[graph[h][i]] = h
queue2.put_nowait(graph[h][i], 0)
print(dist)
graph2 = {1: [2, 3, 5], 2: [4, 6, 1], 3: [5, 1], 4: [6], 5: [2], 6: [1, 7], 7: [2]}
ussp(graph2, 1)
这就是我现在得到的。我很确定它应该工作,但它根本没有。它甚至没有被编译。我对 python 中的列表、数组和队列也很陌生。如果你能帮助我,我将不胜感激。提前致谢
这一行是您出错的原因:
queue2 = Queue
您将 queue2
设置为等于 Queue
class,而不是 Queue
class.
的实例
将该行更改为:
queue2 = Queue()
首先,我在您的函数签名中添加了一个目标参数。假设您想找到从节点 1 到节点 7 的最短路径,下面的程序可以运行。
我还添加了一些 python 样板,因为你说你是 python.
的新手
import sys
from queue import Queue as Q
def ussp(graph, s, d):
len_graph = len(graph)
prev = [ -1 for i in range(len_graph)]
visited = [False for i in range(len_graph)]
q = Q()
dist = [sys.maxsize for i in range(len_graph)]
q.put(s, False)
visited[s-1] = True
dist[s-1] = 0
# modified BFS alg.
while q.empty() == False:
h = q.get_nowait()
for i in range(len(graph[h])):
if visited[graph[h][i]-1] == False:
visited[graph[h][i]-1] = True
dist[graph[h][i]-1] = dist[h-1] + 1
prev[graph[h][i]-1] = h
q.put_nowait(graph[h][i])
path = []
crawl = d # destination
path.append(crawl)
while (prev[crawl-1] != -1):
path.append(prev[crawl-1])
crawl = prev[crawl-1]
print(list(reversed(path)))
def main():
graph2 = {1: [2, 3, 5], 2: [4, 6, 1], 3: [5, 1], 4: [6], 5: [2], 6: [1, 7], 7: [2]}
ussp(graph2, 1, 7)
if __name__ == '__main__':
main()
我是 python 的新人。我尝试使用 BFS 获取 Unweighted-Single-Source-Shortest-Path。
from queue import *
def ussp(graph, s):
len_graph = len(graph)
prev = [[]*len_graph for i in range(len_graph)]
visited = [False for i in range(len_graph)]
queue2 = Queue
dist = [[float('inf')] for i in range(len_graph)]
queue2.put_nowait(graph[s], 0) # starting with s
visited[s] = True
dist[s] = 0
# modified BFS alg.
while queue2.empty() == False:
h = queue2.get_nowait()
for i in len(graph[h]):
if visited[graph[h][i]] == False:
visited[graph[h][i]] = True
dist[graph[h][i]] = dist[h] + 1
prev[graph[h][i]] = h
queue2.put_nowait(graph[h][i], 0)
print(dist)
graph2 = {1: [2, 3, 5], 2: [4, 6, 1], 3: [5, 1], 4: [6], 5: [2], 6: [1, 7], 7: [2]}
ussp(graph2, 1)
这就是我现在得到的。我很确定它应该工作,但它根本没有。它甚至没有被编译。我对 python 中的列表、数组和队列也很陌生。如果你能帮助我,我将不胜感激。提前致谢
这一行是您出错的原因:
queue2 = Queue
您将 queue2
设置为等于 Queue
class,而不是 Queue
class.
将该行更改为:
queue2 = Queue()
首先,我在您的函数签名中添加了一个目标参数。假设您想找到从节点 1 到节点 7 的最短路径,下面的程序可以运行。 我还添加了一些 python 样板,因为你说你是 python.
的新手import sys
from queue import Queue as Q
def ussp(graph, s, d):
len_graph = len(graph)
prev = [ -1 for i in range(len_graph)]
visited = [False for i in range(len_graph)]
q = Q()
dist = [sys.maxsize for i in range(len_graph)]
q.put(s, False)
visited[s-1] = True
dist[s-1] = 0
# modified BFS alg.
while q.empty() == False:
h = q.get_nowait()
for i in range(len(graph[h])):
if visited[graph[h][i]-1] == False:
visited[graph[h][i]-1] = True
dist[graph[h][i]-1] = dist[h-1] + 1
prev[graph[h][i]-1] = h
q.put_nowait(graph[h][i])
path = []
crawl = d # destination
path.append(crawl)
while (prev[crawl-1] != -1):
path.append(prev[crawl-1])
crawl = prev[crawl-1]
print(list(reversed(path)))
def main():
graph2 = {1: [2, 3, 5], 2: [4, 6, 1], 3: [5, 1], 4: [6], 5: [2], 6: [1, 7], 7: [2]}
ussp(graph2, 1, 7)
if __name__ == '__main__':
main()