这是python中用于浏览罗马尼亚城市的搜索算法。
class GraphTree:
graph = {
'Oradea': set(['Zerind','Sibiu']),
'Zerind': set(['Arad','Oradea']),
'Sibiu': set(['Arad','Rimnicu Vilcea','Fagaras','Oradea']),
'Arad': set(['Timisoara','Zerind','Sibiu']),
'Timisoara': set(['Lugoj']),
'Lugoj': set(['Mehadia']),
'Mehadia': set(['Drobeta']),
'Drobeta': set(['Craiova']),
'Rimnicu Vilcea': set(['Craiova','Pitesti','Sibiu']),
'Craiova': set(['Drobeta','Rimnicu Vilcea']),
'Fagaras': set(['Bucharest','Sibiu']),
'Pitesti': set(['Bucharest','Rimnicu Vilcea']),
'Bucharest': set(['Giurgiu','Urziceni','Pitesti','Fagaras']),
'Giurgiu': set(['Bucharest']),
'Urziceni': set(['Hirsova','Vaslui','Bucharest']),
'Hirsova': set(['Eforia','Urziceni']),
'Eforia': set(['Hirsova']),
'Vaslui': set(['Iasi','Urziceni']),
'Iasi': set(['Neamt','Vaslui']),
'Neamt': set(['Iasi'])}
def bfs(graph, start, end):
queue = [(start, [start])]
while queue:
(vertex, path) = queue.pop(0)
for next in graph[vertex] - set(path):
if next == end:
yield path + [next]
else:
queue.append((next, path + [next]))
def dfs(graph, start, goal):
queue = []
queue.append([start])
while queue:
path = queue.pop(0)
node = path[-1]
if node == end:
return path
for adjacent in graph.get(node,[]):
new_path = list(path)
new_path.append(adjacent)
queue.append(new_path)
print('bfs')
bfs(graph, 'Oradea', 'Neamt')
print('dfs')
dfs(graph, 'Oradea', 'Neamt')
运行算法时,我不断收到此错误:
---> 1 class GraphTree:
2
3 graph = {
4 'Oradea': set(['Zerind','Sibiu']),
5 'Zerind': set(['Arad','Oradea']),
另一位于: 49 bfs(图表,' Oradea',' Neamt') 50打印(' dfs') - > 51 dfs(图,'奥拉迪亚',' Neamt')
最后:
39 path = queue.pop(0)
40 node = path[-1]
- > 41如果node == end: 42返回路径 43为graph.get(node,[])中的相邻:
NameError:name' end'未定义
该算法似乎逻辑正确,条件和声明正常。 为什么这种搜索算法不起作用?
该算法应该能够导航从一个城市到另一个城市的地图并使用广度优先(bfs)和深度优先(dfs)搜索返回路径。
答案 0 :(得分:1)
你有
bfs(graph, start, end)
和
dfs(graph, start, goal)
^^^^
您的代码的其他一些注释:
考虑到所有这些,这是另一个版本:
graph = {
'Oradea': set(['Zerind','Sibiu']),
'Zerind': set(['Arad','Oradea']),
'Sibiu': set(['Arad','Rimnicu Vilcea','Fagaras','Oradea']),
'Arad': set(['Timisoara','Zerind','Sibiu']),
'Timisoara': set(['Lugoj']),
'Lugoj': set(['Mehadia']),
'Mehadia': set(['Drobeta']),
'Drobeta': set(['Craiova']),
'Rimnicu Vilcea': set(['Craiova','Pitesti','Sibiu']),
'Craiova': set(['Drobeta','Rimnicu Vilcea']),
'Fagaras': set(['Bucharest','Sibiu']),
'Pitesti': set(['Bucharest','Rimnicu Vilcea']),
'Bucharest': set(['Giurgiu','Urziceni','Pitesti','Fagaras']),
'Giurgiu': set(['Bucharest']),
'Urziceni': set(['Hirsova','Vaslui','Bucharest']),
'Hirsova': set(['Eforia','Urziceni']),
'Eforia': set(['Hirsova']),
'Vaslui': set(['Iasi','Urziceni']),
'Iasi': set(['Neamt','Vaslui']),
'Neamt': set(['Iasi'])}
def bfs(graph, start, end):
queue = [(start, [start])]
while queue:
(vertex, path) = queue.pop(0)
for next in graph[vertex] - set(path):
if next == end:
yield path + [next]
else:
queue.append((next, path + [next]))
def dfs(graph, start, end):
queue = []
queue.append([start])
while queue:
path = queue.pop(0)
node = path[-1]
if node == end:
return path
for adjacent in graph.get(node,[]):
new_path = list(path)
new_path.append(adjacent)
queue.append(new_path)
print('bfs')
print(list(bfs(graph, 'Oradea', 'Neamt')))
print('dfs')
print(dfs(graph, 'Oradea', 'Neamt'))
当我运行它时,输出为:
bfs
[['Oradea', 'Sibiu', 'Fagaras', 'Bucharest', 'Urziceni', 'Vaslui', 'Iasi', 'Neamt'], ['Oradea', 'Sibiu', 'Rimnicu Vilcea', 'Pitesti', 'Bucharest', 'Urziceni', 'Vaslui', 'Iasi', 'Neamt'], ['Oradea', 'Zerind', 'Arad', 'Sibiu', 'Fagaras', 'Bucharest', 'Urziceni', 'Vaslui', 'Iasi', 'Neamt'], ['Oradea', 'Zerind', 'Arad', 'Sibiu', 'Rimnicu Vilcea', 'Pitesti', 'Bucharest', 'Urziceni', 'Vaslui', 'Iasi', 'Neamt'], ['Oradea', 'Zerind', 'Arad', 'Timisoara', 'Lugoj', 'Mehadia', 'Drobeta', 'Craiova', 'Rimnicu Vilcea', 'Pitesti', 'Bucharest', 'Urziceni', 'Vaslui', 'Iasi', 'Neamt'], ['Oradea', 'Sibiu', 'Arad', 'Timisoara', 'Lugoj', 'Mehadia', 'Drobeta', 'Craiova', 'Rimnicu Vilcea', 'Pitesti', 'Bucharest', 'Urziceni', 'Vaslui', 'Iasi', 'Neamt'], ['Oradea', 'Zerind', 'Arad', 'Timisoara', 'Lugoj', 'Mehadia', 'Drobeta', 'Craiova', 'Rimnicu Vilcea', 'Sibiu', 'Fagaras', 'Bucharest', 'Urziceni', 'Vaslui', 'Iasi', 'Neamt']]
dfs
['Oradea', 'Sibiu', 'Fagaras', 'Bucharest', 'Urziceni', 'Vaslui', 'Iasi', 'Neamt']