我试图找到一条穿过一组节点[4,7,9](不需要保留顺序)然后返回到原点(节点1)的最短路径。我有一组边缘:
E = [(1,10),(1,11),(2,3),(2,10),(3,2),(3,12),(4,5),(4 ,12),(5,4),(5,14),(6,7),(6,11),(7,6),(7,13),(8,9),(8,13 ),(9、8),(9、15),(10、1),(10、11),(10、2),(11、1),(11、10),(11、6), (12,13),(12,3),(12,4),(13,12),(13,7),(13,8),(14,15),(14,5),(15 ,14),(15,9)]
我尝试调整How can I use BFS to get a path containing some given nodes in order?的答案,但产生了错误:
Traceback (most recent call last):
File "C:/Users/../rough-work.py", line 41, in <module>
graph[edge[0]].link(graph[edge[-1]])
KeyError: 15
我改编的代码如下:
class Node:
def __init__(self, name):
self.name = name
self.neighbors = []
def link(self, node):
# The edge is undirected: implement it as two directed edges
self.neighbors.append(node)
node.neighbors.append(self)
def shortestPathTo(self, target):
# A BFS implementation which retains the paths
queue = [[self]]
visited = set()
while len(queue):
path = queue.pop(0) # Get next path from queue (FIFO)
node = path[-1] # Get last node in that path
for neighbor in node.neighbors:
if neighbor == target:
# Found the target node. Return the path to it
return path + [target]
# Avoid visiting a node that was already visited
if not neighbor in visited:
visited.add(neighbor)
queue.append(path + [neighbor])
###
n = 15
nodes = list(range(1,n))
E = [(1, 10), (1, 11), (2, 3), (2, 10), (3, 2), (3, 12), (4, 5), (4, 12), (5, 4), (5, 14), (6, 7), (6, 11), (7, 6), (7, 13), (8, 9), (8, 13), (9, 8), (9, 15), (10, 1), (10, 11), (10, 2), (11, 1), (11, 10), (11, 6), (12, 13), (12, 3), (12, 4), (13, 12), (13, 7), (13, 8), (14, 15), (14, 5), (15, 14), (15, 9)]
# Create the nodes of the graph (indexed by their names)
graph = {}
for letter in nodes:
graph[letter] = Node(letter)
print(graph)
# Create the undirected edges
for edge in E:
graph[edge[0]].link(graph[edge[-1]])
# Concatenate the shortest paths between each of the required node pairs
start = 1
path = [graph[1]]
for end in [4,7,9,1]:
path.extend( graph[start].shortestPathTo(graph[end])[1:] )
start = end
# Print result: the names of the nodes on the path
print([node.name for node in path])
代码可能是什么问题?我想将图形扩展到任意数量的大于26的节点-字母的数量(因为我推断以前的实现仅适用于基于字符的节点)。或者,如果有更直接的方法可以做到这一点!
非常感谢,并提供了一些帮助!
答案 0 :(得分:1)
KeyError: 15
和行print(graph)
应该为您提供了线索:后者表明您的graph
字典仅包含14个条目,而您在E
中的边很清楚参考15个单独的索引。
将n = 15
更改为n = 16
,它会起作用:
[1, 10, 2, 3, 12, 4, 12, 13, 7, 13, 8, 9, 8, 13, 7, 6, 11, 1]
请记住:
>>> len(list(range(1,16)))
15