이것은 루마니아 도시를 탐색하는 파이썬의 검색 알고리즘입니다.정의되지 않은 오류가 계속 발생하는 이유는 무엇입니까?
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 (그래프, '오라', '암츠') (50) 인쇄 ('DFS') -> 51 DFS (그래프 '오라', '암츠')
그리고 마지막 :
39 path = queue.pop(0)
40 node = path[-1]
,745,151 5,
-> 41의 경우 노드 == 단부 : graph.get 인접위한 42 복로 43 (노드 [])
NameError: name 'end' is not defined
알고리즘이 확인을 조건으로 선언 정확한 논리적으로 보인다. 이 검색 알고리즘이 작동하지 않는 이유는 무엇입니까?
The algorithm should be able to navigate the map going from one city to the other and returning the path using both breadth first (bfs) and depth first (dfs) searches.
'노드 == 최종 경우 : '어디 이 방법으로 정의가 끝났습니까? –
관련된 줄 번호와 함께 전체 오류 메시지 (컴파일러에서 제공 한대로)를 제공하십시오. 그러면 StackOverflow 사용자가보다 쉽게 도움을받을 수 있습니다. 또한, 어떤 경우에는 그 라인을 직접 검사하면 문제를 즉시 알 수 있습니다. –
'dfs()'매개 변수에서'end' 대신'goal'을 사용했지만'if node == end :'를 시도합니다. –