python 当前路径 搜索路径图某一个不包含_python使用字典定义一个图并查找路径...

对于错误的情况,当到达'14'时,可用的路由显示为'1'和{},而不是'15'。您应该为每个set设置一个列表,以便可以迭代实际值。在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('')}

利用上面的调整矩阵,它给出了正确的结果。在

这是你和我的调试器的函数。您可以看到代码是如何从终端流出的:

^{pr2}$

使用错误的adj_矩阵,它会给出如下控制台输出:(当遇到'14'时,请仔细观察)C:\Users\rapac\Desktop\stackoverflow>python adjgraph.py

st:3, end:15, path:['3']

available routes: {'5', '4'}

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: {'13', '12'}

next node: 13

st:13, end:15, path:['3', '5', '6', '7', '8', '9', '11', '13']

available routes: set()

next node: 12

st:12, end:15, path:['3', '5', '6', '7', '8', '9', '11', '12']

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: {'5', '1'} < - Here it is! We expect '15' here.

next node: 1

st:1, end:15, path:['3', '5', '6', '7', '8', '14', '1']

available routes: {'2'}

next node: 2

st:2, end:15, path:['3', '5', '6', '7', '8', '14', '1', '2']

available routes: {'3'}

next node: 4

st:4, end:15, path:['3', '4']

available routes: set()

None

最后,以下是使用正确版本的adj_matrix时的输出: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']

现在代码的行为与您预期的一样。希望有帮助。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值