python-使用字典定义图形并查找路径
我已将图表定义为以下内容:python-使用字典定义图形并查找路径,python,dictionary,graph,path,Python,Dictionary,Graph,Path,我已将图表定义为以下内容: adj_matrix = {'1': set('2'), '2': set('3'), '3': set(['4', '5']), '4': set(''), '5': set('6'), '6': set('7'), '7': set('8'), '8': set(['9'
adj_matrix = {'1': set('2'),
'2': set('3'),
'3': set(['4', '5']),
'4': set(''),
'5': set('6'),
'6': set('7'),
'7': set('8'),
'8': set(['9', '14']),
'9': set(['10', '11']),
'10': set(''),
'11': set(['12', '13']),
'12': set(''),
'13': set(''),
'14': set('15'),
'15': set('16'),
'16': set('17'),
'17': set(['18', '19']),
'18': set(''),
'19': set('')}
我使用以下方法查找不同节点之间的路径:
def find_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if start not in graph:
return None
for node in graph[start]:
if node not in path:
newpath = find_path(graph, node, end, path)
if newpath: return newpath
return None
问题是该函数仅适用于节点
1
到14
。比如当我调用p=find_path(adj_matrix,'3','14')
时,它就像一个符咒,给我['3','5','6','7','8','14']
作为答案,但当我尝试p=find_path(adj_matrix,'3','15')
时,它返回无。你知道发生了什么事以及我如何解决这个问题吗?对于错误的情况,当你到达'14'
时,可用的路线显示为'1'
和'5'
,而不是'15'
。您应该为每个集合设置一个列表,以便可以迭代实际值
adj_matrix = {'1': set(['2']),
'2': set(['3']),
'3': set(['4', '5']),
'4': set(''),
'5': set(['6']),
'6': set(['7']),
'7': set(['8']),
'8': set(['9', '14']),
'9': set(['10', '11']),
'10': set(''),
'11': set(['12', '13']),
'12': set(''),
'13': set(''),
'14': set(['15']),
'15': set(['16']),
'16': set(['17']),
'17': set(['18', '19']),
'18': set(''),
'19': set('')}
使用上面的adj_矩阵,它给出了正确的结果
这是我的调试器的函数。您可以看到代码如何从终端流出:
def find_path(graph, start, end, path=[]):
path = path + [start]
print("st:%s, end:%s, path:%s" % (start,end,path))
if start == end:
return path
if start not in graph:
return None
print("available routes: %s" % graph[start])
for node in graph[start]:
if node not in path:
print("next node: %s" % node)
newpath = find_path(graph, node, end, path)
if newpath: return newpath
return None
使用错误的adj_矩阵,它给出如下控制台输出:(当涉及到'14'
时,请仔细观察)
现在,代码的行为与您预期的一样。希望有帮助。如果您将输入更改为
adj_matrix = {'1': set('2'),
'2': set('3'),
'3': set(['4', '5']),
'4': set(''),
'5': set('6'),
'6': set('7'),
'7': set('8'),
'8': set(['9', '14']),
'9': set(['10', '11']),
'10': set(''),
'11': set(['12', '13']),
'12': set(''),
'13': set(''),
'14': set(['15']),
'15': set(['16']),
'16': set(['17']),
'17': set(['18', '19']),
'18': set(''),
'19': set('')}
(我刚刚添加了“15”、“16”和“17”在列表中。)
然后它就起作用了。您的问题与这样一个事实有关:当两位数字在集合中单独存在时,它们被解释为图形[start]:
中节点的行中的字符串,然后循环['1',5']
另外请注意,您也可以简单地使用整数,每个数字周围不带引号,我认为这也可以解决这个问题。set('15')
不是您所认为的,它实际上是{'1',5'}
。您需要set(['15'])
或{'15'}
谢谢您的详细解释!
C:\Users\rapac\Desktop\stackoverflow>python adjgraph.py
st:3, end:15, path:['3']
available routes: {'4', '5'}
next node: 4
st:4, end:15, path:['3', '4']
available routes: set()
next node: 5
st:5, end:15, path:['3', '5']
available routes: {'6'}
next node: 6
st:6, end:15, path:['3', '5', '6']
available routes: {'7'}
next node: 7
st:7, end:15, path:['3', '5', '6', '7']
available routes: {'8'}
next node: 8
st:8, end:15, path:['3', '5', '6', '7', '8']
available routes: {'9', '14'}
next node: 9
st:9, end:15, path:['3', '5', '6', '7', '8', '9']
available routes: {'11', '10'}
next node: 11
st:11, end:15, path:['3', '5', '6', '7', '8', '9', '11']
available routes: {'12', '13'}
next node: 12
st:12, end:15, path:['3', '5', '6', '7', '8', '9', '11', '12']
available routes: set()
next node: 13
st:13, end:15, path:['3', '5', '6', '7', '8', '9', '11', '13']
available routes: set()
next node: 10
st:10, end:15, path:['3', '5', '6', '7', '8', '9', '10']
available routes: set()
next node: 14
st:14, end:15, path:['3', '5', '6', '7', '8', '14']
available routes: {'15'} <-- Now we're good.
next node: 15
st:15, end:15, path:['3', '5', '6', '7', '8', '14', '15']
['3', '5', '6', '7', '8', '14', '15']
adj_matrix = {'1': set('2'),
'2': set('3'),
'3': set(['4', '5']),
'4': set(''),
'5': set('6'),
'6': set('7'),
'7': set('8'),
'8': set(['9', '14']),
'9': set(['10', '11']),
'10': set(''),
'11': set(['12', '13']),
'12': set(''),
'13': set(''),
'14': set(['15']),
'15': set(['16']),
'16': set(['17']),
'17': set(['18', '19']),
'18': set(''),
'19': set('')}